Build Your First Agent with the Vercel AI SDK
The Vercel AI SDK is a TypeScript toolkit that gives you one set of functions for many model providers. generateText makes a single call, a tool-enabled call can run several steps, and streamText streams the output. You can build a complete tool-using agent in a single file. This post uses the AI SDK 5 style, where tools take an inputSchema; older 4.x versions called this field parameters.
Install
mkdir ai-agent && cd ai-agent
npm init -y
npm install ai @ai-sdk/anthropic zod
npm install -D tsx typescript
The ai package holds the core functions. Each provider has its own package; @ai-sdk/anthropic reads ANTHROPIC_API_KEY from the environment. zod describes tool inputs. tsx lets you run TypeScript files directly.
A plain text call
import { generateText } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
const { text } = await generateText({
model: anthropic('claude-sonnet-5-5'),
prompt: 'Explain what a webhook is in two sentences.',
});
console.log(text);
Save this as index.ts and run npx tsx index.ts (use "type": "module" in package.json so top-level await works). It prints a short explanation. The model is created by calling the provider function with a model name, so changing providers means changing that one line.
Add a tool and allow several steps
import { generateText, tool, stepCountIs } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
import { z } from 'zod';
const getWeather = tool({
description: 'Get the current temperature in Celsius for a city',
inputSchema: z.object({
city: z.string().describe('City name, for example Paris'),
}),
execute: async ({ city }) => {
const data: Record<string, number> = { Paris: 18, Tokyo: 25 };
return { city, celsius: data[city] ?? null };
},
});
const result = await generateText({
model: anthropic('claude-sonnet-5-5'),
tools: { getWeather },
stopWhen: stepCountIs(5),
prompt: 'Is it warmer in Paris or Tokyo right now?',
});
console.log(result.text);
console.log(result.steps.length, 'steps');
By default generateText stops after the first model response, even if it requested a tool. The stopWhen option with stepCountIs(5) allows up to five steps, so the SDK runs the tool, sends the result back and lets the model continue. This is the agent loop. Here the model calls the tool twice, once per city, then answers that Tokyo is warmer. result.steps records every step, which is useful for debugging.
Why zod?
The schema is used twice. It is converted to JSON Schema so that the model knows the argument shape, and it validates the arguments at run time so that execute receives correctly typed values. The .describe() calls become argument descriptions that the model reads, so write them carefully.
Common mistakes
- Forgetting
stopWhenand wondering why the final text is empty after a tool call. - Mixing AI SDK versions: examples for 4.x use
parametersandmaxSteps, while 5.x usesinputSchemaandstopWhen. Check the version inpackage.json. - Throwing errors from
executefor expected cases; return a result object that describes the problem instead. - Hard-coding model names across the codebase; keep them in one config file.
The Vercel AI SDK is lightweight: it gives you primitives and leaves structure to you. Later posts add streaming to a chat interface, structured output with generateObject and provider switching.
Reading the steps
When a run uses tools, result.steps is an array with one entry per model call. Each step holds the text produced, the tool calls requested, and the tool results returned. Logging them during development is the quickest debugging aid you have. If the model keeps calling the same tool with the same arguments, you will see it immediately and can fix the tool description or the prompt. The result also reports token usage, which you should record for cost tracking from the first day.
Choosing a model and keeping it swappable
The AI SDK treats models as values. A tiny module that exports the chosen model lets the rest of your code stay unaware of the provider:
// model.ts
import { anthropic } from '@ai-sdk/anthropic';
export const chatModel = anthropic(process.env.CHAT_MODEL ?? 'claude-sonnet-5-5');
Elsewhere you import chatModel and pass it to generateText. When you want to try another provider, you install its package, change this one file and rerun your tests. Because tool definitions use zod and plain functions, they do not change at all.
Where to go from here
With one tool and a step limit you already have an agent. Add a second tool and watch whether the model chooses correctly, then add a system prompt with the system option to set tone and limits. When the code runs well in a script, move it into a route handler so a web page can call it, which is where streamText replaces generateText. Keep your tools in a separate file so that the same definitions can be reused by a script, a test and a route.
Comments
No comments yet. Have a question or a tip? Be the first to share it.