AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
The 'ai' package is Vercel's AI SDK, a TypeScript-first toolkit designed to simplify building AI-powered applications in JavaScript environments. It provides a unified, provider-agnostic interface for working with large language models from OpenAI, Anthropic, Google, and other providers. Instead of learning different APIs for each provider, developers use consistent methods like generateText() and streamText() that work across all supported models.
The SDK emerged from Vercel's need to standardize AI integration patterns in modern web frameworks, particularly for streaming responses in edge runtimes and server components. It abstracts away the complexity of handling streaming tokens, managing conversation state, and implementing tool calling (function execution). With over 7 million weekly downloads, it's become a standard choice for production AI features in Next.js applications and beyond.
The package is split into modular components: the core 'ai' package for streaming and UI integration, provider packages like '@ai-sdk/openai' for model access, and framework-specific bindings. It requires Node.js 18+ and works in serverless/edge environments. Developers can switch from one model provider to another by changing a single function call, making it trivial to compare models or implement fallback strategies without rewriting application logic.
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
// Server-side streaming text generation
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('gpt-4-turbo'),
messages,
system: 'You are a helpful assistant that provides concise answers.',
maxTokens: 500,
temperature: 0.7,
tools: {
getWeather: {
description: 'Get current weather for a location',
parameters: z.object({
location: z.string().describe('City name')
}),
execute: async ({ location }) => {
return { temp: 72, condition: 'sunny', location };
}
}
}
});
return result.toDataStreamResponse();
}
// Client-side React hook usage
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat({
api: '/api/chat'
});
return (
<div>
{messages.map(m => (
<div key={m.id}>
<strong>{m.role}:</strong> {m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit">Send</button>
</form>
</div>
);
}Real-time chatbots: Build conversational interfaces with streaming responses that render tokens as they arrive, improving perceived performance. The useChat() hook manages message history and UI state automatically.
Structured data extraction: Use generateObject() with Zod schemas to parse unstructured text into typed TypeScript objects, ideal for extracting contact information from emails or parsing receipts.
Agentic workflows with tool calling: Implement AI agents that can execute functions autonomously. Define tools as TypeScript functions and let the model decide when to call them, enabling multi-step reasoning tasks like booking appointments or querying databases.
Multi-modal applications: Process images alongside text prompts using vision models. Upload a screenshot and ask questions about it, or generate descriptions for accessibility features.
Server-side content generation: Generate SEO-optimized content, email templates, or report summaries in API routes or serverless functions with simple async/await syntax and built-in error handling.
npm install aipnpm add aibun add ai