Essential patterns for building AI-powered applications with the Vercel AI SDK.
Source: mattpocock/ai-sdk-tips
Message Types
Understanding LLM conversation structure:
User & Assistant Messages
// User message: from you
{ role: "user", content: "What is 2 + 2?" }
// Assistant message: from the LLM
{ role: "assistant", content: "2 + 2 equals 4." }
System Prompts
Messages at conversation start, visible to LLM but not user:
const result = await generateText({
model: openai("gpt-4"),
system: "You are a helpful math tutor. Always show your work.",
messages: [
{ role: "user", content: "What is 2 + 2?" }
],
});
System prompts take priority over user messages in conflicts.
Reasoning Tokens
Some models "think" before responding:
const result = await generateText({
model: anthropic("claude-3-opus"),
messages: [{ role: "user", content: "Solve this logic puzzle..." }],
});
// result includes reasoning tokens in thinking models
Multi-part Messages
Messages can contain multiple parts (text, files, tool calls):
{
role: "user",
content: [
{ type: "text", text: "Summarize this PDF" },
{ type: "file", data: pdfBuffer, mimeType: "application/pdf" }
]
}
Tools
Extend LLM capabilities with callable functions:
Tool Definition
const tools = {
writeFile: {
description: "Write content to a file",
parameters: z.object({
path: z.string().describe("File path"),
content: z.string().describe("File content"),
}),
execute: async ({ path, content }) => {
await fs.writeFile(path, content);
return `Wrote ${content.length} bytes to ${path}`;
},
},
};
Tool Call Flow
- LLM receives tool definitions in system prompt
- User asks: "Write a todo.md file"
- LLM returns tool call:
{ tool: "writeFile", args: { path: "todo.md", content: "..." } } - You execute the tool
- Send tool result back to LLM
- LLM summarizes what happened
const result = await generateText({
model: openai("gpt-4"),
tools,
messages: [{ role: "user", content: "Write a todo.md file" }],
});
// result.toolCalls contains the calls to execute
Guardrails Pattern
Validate requests before expensive LLM calls:
const checkIsRequestSafe = async (messages: ModelMessage[]) => {
// Use a FAST model for validation
const result = await generateText({
model: google("gemini-2.0-flash-lite"),
system: `You are a safety checker. Respond with only:
1 - if the request is safe
0 - if the request is unsafe`,
messages,
});
return result.text.trim() === "1";
};
// In your API route:
const isSafe = await checkIsRequestSafe(messages);
if (!isSafe) {
return new Response("Request rejected", { status: 400 });
}
// Continue with main LLM call...
Key insight: Single-token responses are extremely fast. Use them for routing and validation.
Applications
- Safety checks: Block harmful requests
- Smart routing: Route complex queries to powerful models, simple ones to fast models
- Cost optimization: Filter before expensive inference
Token Optimization
Understanding Tokens
- ~4 characters or ~0.75 words per token
- Input tokens: what you send (cheaper)
- Output tokens: what LLM generates (more expensive)
Efficient Data Formatting
// Bad: Verbose JSON (many tokens)
const data = JSON.stringify(users, null, 2);
// Good: Compact format (fewer tokens)
const data = users.map(u => `${u.id}|${u.name}|${u.email}`).join("\n");
// Better: Only include needed fields
const data = users.map(u => u.name).join(", ");
Streaming for UX
const streamTextToWriter = (writer: UIMessageStreamWriter, message: string) => {
const id = crypto.randomUUID();
writer.write({ type: "text-start", id });
writer.write({ type: "text-delta", id, delta: message });
writer.write({ type: "text-end", id });
};
Common Mistakes
1. Too Many Tools
Problem: LLMs perform worse with many tool definitions.
Why: Tool definitions consume context window space. More tools = more "lost in the middle" issues.
Solution:
// Bad: 50 tools always available
const tools = { ...allTools };
// Good: Only relevant tools per request
const tools = selectToolsForContext(userRequest);
Rule of thumb: 6-8 tools max per request.
2. Unwrapped Data in System Prompt
Problem: Injecting user data directly into prompts.
// Bad: Injection risk
const system = `Help the user with this data: ${userData}`;
// Good: Wrap external data
const system = `Help the user with this data:
<user_data>
${userData}
</user_data>`;
3. Ignoring Context Window Limits
Problem: Stuffing too much into context.
// Bad: Dump entire codebase
const context = await readEntireRepo();
// Good: Relevant snippets only
const context = await searchRelevantFiles(query);
4. No Idle Timeouts
Problem: Runaway agents consuming resources.
// Always set timeouts
const result = await generateText({
model: openai("gpt-4"),
messages,
maxTokens: 4096,
// Add application-level timeouts too
});
Agents vs Workflows
Workflows
Fixed sequence of LLM calls:
// Step 1: Extract info
const extracted = await generateObject({ ... });
// Step 2: Validate
const validated = await generateObject({ ... });
// Step 3: Format output
const output = await generateText({ ... });
Predictable, easier to debug, lower cost.
Agents
LLM decides what to do next:
while (!done) {
const result = await generateText({
model,
tools,
messages,
});
if (result.toolCalls.length === 0) {
done = true;
} else {
// Execute tools, add results to messages
}
}
Flexible, powerful, but harder to predict and control.
Choose workflows when: Steps are known, predictability matters. Choose agents when: Problem requires exploration, steps aren't predetermined.
Best Practices Summary
- Validate fast — Use cheap models for guardrails
- Limit tools — 6-8 max per request
- Wrap external data — Prevent prompt injection
- Stream responses — Better UX for long outputs
- Optimize tokens — Compact data formats
- Set timeouts — Prevent runaway costs
- Prefer workflows — When steps are predictable