Vercel AI SDK
Type: full-code · Vendor: Vercel · Language: TypeScript · License: Apache-2.0 · Status: active · Status in practice: mature · First released: 2023-05-23
Provide a free, TypeScript-first toolkit that standardises calls to any model provider with a single API for text generation, structured-object generation, streaming UIs, tool-calling, and multi-step agent loops in TypeScript/JavaScript runtimes including browser, Node.js, and Vercel Edge.
Description. The Vercel AI SDK is the Apache-2.0 TypeScript toolkit from Vercel split into AI SDK Core ('A unified API for generating text, structured objects, tool calls, and building agents with LLMs') and AI SDK UI ('A set of framework-agnostic hooks for quickly building chat and generative user interface'). Core exposes generateText, streamText, and the structured-output siblings generateObject / streamObject driven by Zod, Valibot, or JSON Schema. The agent loop is opt-in via stopWhen (default stepCountIs(20)). UI layer ships useChat and useCompletion hooks for React (plus Vue/Svelte equivalents). Designed for edge runtimes alongside Node.js.
Agent loop shape. Imperative TypeScript loop. Caller invokes generateText({ model, tools, stopWhen, ... }) or streamText(...). In each step the model either generates text (loop ends) or calls a tool (executed in-process, observation feeds back in for another generation). Loop terminates when stopWhen is satisfied; default cap stepCountIs(20). No graph DSL, no class hierarchy — just async functions, plain objects, and AbortSignal.
Primary use cases
- TypeScript agents over multiple LLM providers behind one API
- streaming chat UIs via useChat / streamText
- Zod-typed structured outputs
- MCP tool integration in TypeScript
Key concepts
- generateText / streamText (docs) — Core agent-loop entry points; streamText returns a token stream.
- tools → tool-use (docs) — A tool consists of three properties: description, inputSchema (Zod or JSON schema), and execute.
- stopWhen / stepCountIs → step-budget (docs) — Loop-termination predicate; stepCountIs(20) is the default safety cap.
- generateObject + Zod → structured-output (docs) — Structured-output generation with schema validation.
- Providers (language model spec) (docs) — Standardised interface abstracting provider differences (OpenAI / Anthropic / Google / xAI / Bedrock / Vertex / Gateway).
- useChat hook (docs) — React UI hook for conversational interfaces (framework-agnostic equivalents for Vue/Svelte).
- AbortSignal / onAbort → stop-cancel (docs) — Cancellation lifecycle — onAbort fires when streams are aborted via AbortSignal; onFinish is skipped.
Patterns this full-code implements —
- ★★Citation Streaming
Sources surface as typed source stream-parts (sourceType: 'url'); each entry has id, url, title, providerMetadata. Upstream uses the word 'sources' rather than 'citations'.
- ★★Model Context Protocol
Three transports — HTTP (recommended), SSE, stdio. client.tools() adapts MCP tool definitions to AI SDK tool definitions.
- ★★Step Budget
Default stepCountIs(20); stopWhen accepts any predicate (stepCountIs, hasToolCall, custom).
- ★★Stop / Cancel
Streams accept AbortSignal; onAbort callback fires on abort (onFinish skipped). useChat exposes a stop() helper.
- ★★Structured Output
generateObject / streamObject accept Zod, Valibot, or JSON Schema; schemas drive both generation and validation. Composable with tool calling.
- ★★Tool Use
Tools are objects with description, inputSchema (Zod/JSON Schema), execute; passed to generateText / streamText via tools param.
- ★★ReAct
The agent loop IS the ReAct shape (reason → act → observe → repeat), but upstream docs never use the term 'ReAct'. Downgraded from first-class to supported because the pattern is implemented but not…
Neighbourhood
Click any neighbour to follow the lineage. Scroll to zoom, drag to pan.