← Protocols
Vercel AI SDK
AI Agent / Web3·off-chain (any)

Vercel AI SDK

01Description

TypeScript-first toolkit for building AI apps and agents with a unified API across 100+ models. Provides streamText, generateText, generateObject, tools, and the Agent abstraction (AI SDK 6).

02Best for
  • 01TypeScript LLM apps
  • 02streaming chat UIs
  • 03structured outputs with Zod
  • 04tool-calling agents
  • 05Next.js / React Server Components
03Install
  • pnpm add ai @ai-sdk/openai @ai-sdk/anthropic zod
04Environment variables
VariableScopeDescription
OPENAI_API_KEYServerProvider key (OpenAI shown; @ai-sdk/anthropic, @ai-sdk/google, etc. each have their own).
05Prompt snippet
Use the Vercel AI SDK for streaming and tool-using agents. `import { streamText, tool, Agent } from 'ai'; import { openai } from '@ai-sdk/openai';` For chat: `const result = streamText({ model: openai('gpt-5'), messages, tools: { swap: tool({ description: '...', inputSchema: z.object({...}), execute: async (args) => {...} }) } }); return result.toUIMessageStreamResponse();` For agents (SDK 6): `const agent = new Agent({ model: openai('gpt-5'), instructions: '...', tools: {...} }); await agent.generate({ prompt: '...' });`. `generateObject` returns Zod-typed structured data in a single call.
06Gotchas
  • API keys must stay server-side — call `streamText` from a Route Handler or Server Action, never from a Client Component.
  • Tool `inputSchema` (v5+) was renamed from `parameters` (v3/v4) — old examples will fail to type-check; check your AI SDK major version before copy-pasting.
  • Multi-step tool loops require `maxSteps` (or `stopWhen`) — without it the agent stops after a single tool call and never continues to a final answer.
  • Streaming with React Server Components needs `result.toUIMessageStreamResponse()` (or the legacy `toDataStreamResponse`); returning the raw stream breaks the `useChat` client hook.
  • Provider packages (`@ai-sdk/openai`, `@ai-sdk/anthropic`) ship independently and may lag the core `ai` package for new features — pin compatible majors.
  • Cost runs hot with multi-step agents; log `result.usage` and set per-route token budgets in production.
07Alternatives