Build AI agents
import { ToolLoopAgent, tool } from 'ai';
import { z } from 'zod';
const agent = new ToolLoopAgent({
model: 'anthropic/claude-fable-5.1',
tools: {
getWeather: tool({
description: 'Get the current weather for a location',
inputSchema: z.object({ location: z.string() }),
execute: async ({ location }) => ({
location,
temperature: 72,
condition: 'sunny',
}),
}),
},
});
const { text } = await agent.generate({
prompt: "What's the weather in Tokyo?",
});
console.log(text);import { generateText } from 'ai';
const { text } = await generateText({
model: 'openai/gpt-6-astra',
prompt: 'Explain quantum entanglement in one paragraph.',
});
console.log(text);import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: 'openai/gpt-6-astra',
schema: z.object({
sentiment: z.enum(['positive', 'neutral', 'negative']),
}),
prompt: 'Classify the sentiment: I love this product.',
});
console.log(object);- Unified provider API. Switch between models by changing two lines of code
- Structured outputs. Generate type-safe JSON with
generateObjectandstreamObject - Tool calling. Let models interact with external systems
- Streaming first. Stream text, objects, and UI to your frontend
- Framework support. Works with React, Next.js, Vue, Svelte, and Node.js
At the center of the AI SDK is AI SDK Core, which provides a unified API to call any LLM.
The following example shows how to generate text with the AI SDK using OpenAI's GPT-6 Astra:
import { generateText } from 'ai';
const { text } = await generateText({
model: 'openai/gpt-6-astra',
prompt: 'Explain the concept of quantum entanglement.',
});The unified interface lets you switch providers by changing the model string. For example, to use Anthropic's Claude Fable 5.1:
import { generateText } from 'ai';
const { text } = await generateText({
model: 'anthropic/claude-fable-5.1',
prompt: 'How many people will live in the world in 2040?',
});While text generation can be useful, you might want to generate structured JSON data. For example, you might want to extract information from text, classify data, or generate synthetic data. AI SDK Core provides two functions (generateObject and streamObject) to generate structured data, allowing you to constrain model outputs to a specific schema.
The following example shows how to generate a type-safe recipe that conforms to a zod schema:
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: 'openai/gpt-6-astra',
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});The AI SDK supports tool calling out of the box, allowing it to interact with external systems and perform discrete tasks. The following example shows how to use tool calling with the AI SDK:
import { generateText, tool } from 'ai';
import { z } from 'zod';
const { text } = await generateText({
model: 'openai/gpt-6-astra',
prompt: 'What is the weather like today in San Francisco?',
tools: {
getWeather: tool({
description: 'Get the weather in a location',
inputSchema: z.object({
location: z.string().describe('The location to get the weather for'),
}),
execute: async ({ location }) => ({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
}),
},
});The AI SDK is available as a package. To install it, run the following command:
pnpm i aiyarn add ainpm i aibun add aiSee the AI SDK Getting Started guide for more information on how to get started with the AI SDK.
Follow Build AI agents with AI Gateway and AI SDK to set up authentication, stream responses, and add tools and model fallbacks.
Vercel Connect supplies short-lived OAuth tokens to MCP clients built with AI
SDK, so models can call tools from services such as Linear without storing
provider credentials in your application. The connectAuthProvider() helper
requests credentials and supports user consent flows.
Follow the AI SDK and MCP tutorial to configure Vercel Connect, make MCP tools available to an AI SDK application, and handle tool approval.
Was this helpful?