Vercel AI SDK: Tool Calling for Agents
Updated 2026-11-17
The Vercel AI SDK (ai on npm) runs a tool-calling loop in TypeScript: you define tools with Zod schemas, pass them to generateText, and the SDK executes execute functions until a stop condition fires. This guide matches the current AI SDK Core docs. For Python agents, see OpenAI Agents SDK function tools or Build your first AI agent in Python. Last tested 2026-11-17.
Docs used:
Prerequisites
- Node.js 18+ (22+ recommended)
- An API key for your model provider, or Vercel AI Gateway credentials
Steps
1. Create a project and install packages
mkdir ai-sdk-agent && cd ai-sdk-agent
npm init -y
npm install ai zod @ai-sdk/openai
Use @ai-sdk/anthropic, @ai-sdk/google, or another provider package if you are not using OpenAI.
2. Define a tool with tool() and Zod
The SDK infers parameter types from inputSchema. The description helps the model choose the tool.
// agent.ts
import { generateText, tool, isStepCount } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const getOrderStatus = tool({
description: "Look up shipping status by order ID",
inputSchema: z.object({
orderId: z.string().describe("Customer order id"),
}),
execute: async ({ orderId }) => {
const db: Record<string, string> = {
A100: "shipped — arrives Friday",
A101: "processing",
};
return db[orderId] ?? "order not found";
},
});
3. Run the multi-step tool loop
Pass tools as a record keyed by tool name. Use stopWhen: isStepCount(5) to cap agent steps per the AI SDK docs.
const result = await generateText({
model: openai("gpt-4.1-mini"),
tools: { getOrderStatus },
stopWhen: isStepCount(5),
prompt: "Where is order A100?",
});
console.log(result.text);
When the model emits a tool call, the SDK runs execute and feeds the result back into the model.
4. Enable strict tool schemas (optional)
On providers that support it, set strict: true on a tool so arguments must validate against inputSchema:
const weather = tool({
description: "Get weather for a city",
inputSchema: z.object({ city: z.string() }),
strict: true,
execute: async ({ city }) => ({ city, tempF: 72 }),
});
See Strict mode in the AI SDK docs.
5. Stream partial results
For interactive UIs, use streamText with the same tools and stopWhen pattern so tokens and tool events arrive incrementally. See Streaming.
6. Point models through AI Gateway (optional)
If you use Gateway, pass a gateway-qualified model id (as in AI SDK docs) instead of wiring each provider key in development. Store keys in environment variables only.
Common errors
Invalid tool arguments: Zod rejected the model’s JSON. Tighten descriptions, enable strict where supported, or simplify the schema.
Infinite tool loop: Raise the step limit only after fixing prompts; compare with Fix: AI agent stuck in a loop.
Provider does not support tools: Pick a model that supports tool calling in your provider’s matrix. The AI SDK surfaces an error when the model cannot emit tool calls.
Missing execute: You can omit execute to forward tool calls to the client (human-in-the-loop). This guide uses server-side execute for a complete agent.
FAQ
How is this different from the OpenAI Agents SDK?
The Vercel AI SDK targets TypeScript services and UI (including React). The OpenAI Agents SDK targets Python with @function_tool and Runner.
Can I call MCP tools from the AI SDK?
Use a bridge in your runtime (stdio or HTTP MCP client) and wrap MCP invocations inside execute. Start with Connect an AI agent to an MCP server on the protocol side.
Do I need to add the ai package to my Astro site?
No. This guide’s dependencies apply to agent apps you build beside the site. The aiagenthowto.com repo does not need them unless you embed an agent in the product.