Core Functions
generateText — Simple Completion
const { text } = await generateText({
model: anthropic("claude-sonnet-4-20250514"),
prompt: "Write a haiku about coding",
});streamText — Streaming Response
const result = streamText({
model: anthropic("claude-sonnet-4-20250514"),
messages: [{ role: "user", content: "Tell me a story" }],
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}generateObject — Structured Output
import { z } from "zod";
const { object } = await generateObject({
model: anthropic("claude-sonnet-4-20250514"),
schema: z.object({
recipe: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
prompt: "Generate a recipe for chocolate cake",
});
// object.recipe, object.ingredients, object.steps — fully typed!Tool Use
const result = await generateText({
model: anthropic("claude-sonnet-4-20250514"),
tools: {
getWeather: {
description: "Get weather for a location",
parameters: z.object({ city: z.string() }),
execute: async ({ city }) => ({ temp: 22, condition: "sunny" }),
},
},
prompt: "What is the weather in Tokyo?",
});React Hooks (useChat)
"use client";
import { useChat } from "ai/react";
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map(m => <div key={m.id}>{m.role}: {m.content}</div>)}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
</form>
</div>
);
}Provider Adapters
| Provider | Package |
|---|---|
| Anthropic | @ai-sdk/anthropic |
| OpenAI | @ai-sdk/openai |
@ai-sdk/google |
|
| Mistral | @ai-sdk/mistral |
| Cohere | @ai-sdk/cohere |
| Amazon Bedrock | @ai-sdk/amazon-bedrock |
Key Stats
- 12,000+ GitHub stars
- By Vercel
- 20+ provider adapters
- Streaming, tools, structured output
- React/Svelte/Vue hooks
FAQ
Q: What is Vercel AI SDK? A: A TypeScript SDK providing a unified API for building AI features with streaming, tool use, and structured output across 20+ model providers.
Q: Is it free? A: Yes, open-source under Apache 2.0. You provide your own API keys.
Q: Do I need Next.js? A: No, the core SDK works with any Node.js project. The React hooks work with any React app.