
The Complete Guide to the Vercel AI SDK
A comprehensive guide to building AI-powered applications and agents with the Vercel AI SDK (v6) using TypeScript.
You can read the contents and the first 2,000 characters.
Free accounts get the whole book.
- By Matan Marudi
- 29 chapters
- Updated Nov 3, 2026
- 26 readers
What’s inside
- Navigation — Decision Matrix
- What the Vercel AI SDK Is and Why It Exists
- The Library Landscape & Getting Started
- Generating and Streaming Text
- Generating Structured Data
- Tool Calling
From the first page
The Vercel AI SDK is a provider-agnostic TypeScript toolkit for building AI-powered applications and agents. It runs everywhere modern JavaScript runs — React, Next.js, Vue, Svelte, Node.js, and the edge — and it has become one of the most widely used ways to talk to large language models from JavaScript, with millions of weekly downloads.
Its promise is simple: write your AI logic once, against one unified API, and swap models and providers with a string. Underneath that promise is a deep, fast-moving toolkit covering text and structured generation, tool calling, autonomous agents, multimodal input and output, streaming chat interfaces, and a model gateway. This book is the map.
Preview · 2,000 of 554,363 characters
Contents
- 01The Complete Guide to the Vercel AI SDKp.1
- Prefacep.1
- Table of Contentsp.1
- 02Navigation — Decision Matrixp.2
- 03What the Vercel AI SDK Is and Why It Existsp.3
- The problem: provider lock-in and fragmented APIsp.3
- What the AI SDK solvesp.3
- The two layers: AI SDK Core and AI SDK UIp.3
- Supported runtimes and frameworksp.3
- What's new in AI SDK 5 and 6p.3
- Key takeawaysp.3
- 04The Library Landscape & Getting Startedp.4
- The package ecosystemp.4
- The AI Gateway as the default providerp.4
- Installationp.4
- Environment and API-key setupp.4
- Your first `generateText` callp.4
- A minimal Next.js streaming routep.4
- Streaming from non-Next.js serversp.4
- How the pieces fit together and what to installp.4
- +1 more section
- 05Generating and Streaming Textp.5
- `generateText` — single-shot generationp.5
- `streamText` — incremental generationp.5
- Handling long generationsp.5
- When to stream and when not top.5
- Key takeawaysp.5
- 06Generating Structured Datap.6
- Generating a structured objectp.6
- Output strategiesp.6
- Improving accuracy: property descriptions, names, and reasoningp.6
- Streaming structured outputp.6
- Streaming objects to the client with `useObject`p.6
- Structured output via tools vs. `output`p.6
- Validating and repairing output, and error handlingp.6
- Key takeawaysp.6
- 07Tool Callingp.7
- What a tool isp.7
- How the model decides to call a toolp.7
- Single vs. multi-step calls with `stopWhen`p.7
- Accessing tool calls and resultsp.7
- Response messagesp.7
- `toolChoice`p.7
- Tool execution optionsp.7
- Streaming tool callsp.7
- +10 more sections
- 08Model Context Protocol (MCP) and Tool Contextp.8
- What MCP isp.8
- Initializing an MCP clientp.8
- Using MCP toolsp.8
- Closing the clientp.8
- Resources, prompts, and elicitationp.8
- MCP Appsp.8
- Runtime and tool contextp.8
- Best practices and security for remote toolsp.8
- +1 more section
See all 29 parts
- 09Prompt Engineering, Settings, and Reasoningp.9
- Prompt vs. Messagesp.9
- The Message Formatp.9
- Prompt Engineering for the SDKp.9
- Common Call Settingsp.9
- Reasoning Modelsp.9
- Key takeawaysp.9
- 10Embeddings and Rerankingp.10
- Embedding a Single Valuep.10
- Embedding Many Valuesp.10
- Token Usagep.10
- Cosine Similarityp.10
- Settingsp.10
- Embedding Providers and Modelsp.10
- Building a Semantic-Search / RAG Embedding Pipelinep.10
- Rerankingp.10
- +1 more section
- 11Multimodal Generation: Images, Speech, Transcription, Video, and Filesp.11
- Image Generationp.11
- Realtime (Experimental)p.11
- Transcription (Experimental)p.11
- Speech (Experimental)p.11
- Video Generation (Experimental)p.11
- File Uploadsp.11
- Key takeawaysp.11
- 12Provider and Model Managementp.12
- Why centralize provider configurationp.12
- How provider packages workp.12
- Custom providers with `customProvider`p.12
- The provider registryp.12
- Combining custom providers, the registry, and middlewarep.12
- The AI Gateway as the default providerp.12
- Choosing and swapping models cleanlyp.12
- Key takeawaysp.12
- 13Language Model Middlewarep.13
- Applying middleware with `wrapLanguageModel`p.13
- Built-in middlewarep.13
- The `LanguageModelMiddleware` interfacep.13
- Per-request custom metadatap.13
- Community middlewarep.13
- Skill Uploadsp.13
- Key takeawaysp.13
- 14Error Handling, Testing, Telemetry, and DevToolsp.14
- The error modelp.14
- Handling streaming errorsp.14
- Retries and `maxRetries`p.14
- Testing with mock providersp.14
- Telemetryp.14
- DevToolsp.14
- Event callbacks as cross-cutting observability hooksp.14
- Key takeawaysp.14
- 15Agent Fundamentals: The Agent Loop and Loop Controlp.15
- What Is an Agent in the SDK?p.15
- Creating an Agentp.15
- Configuration Optionsp.15
- Define Agent Behavior with System Instructionsp.15
- Using an Agentp.15
- End-to-End Type Safetyp.15
- The Agent Loopp.15
- Loop Controlp.15
- +2 more sections
- 16Workflow Patternsp.16
- Deterministic vs. Agentic Controlp.16
- Sequential Processing (Chains)p.16
- Routing (Classification)p.16
- Parallel Processingp.16
- Orchestrator-Workerp.16
- Evaluator-Optimizerp.16
- When to Hand Control to the Model vs. Encode It Yourselfp.16
- Key Takeawaysp.16
- 17Advanced Agents: Memory, Subagents, Approvals, Skills, and DurableAgent (Workflow DevKit)p.17
- Memoryp.17
- Subagentsp.17
- Tool Approvals (Human-in-the-Loop)p.17
- Compacting Agent Contextp.17
- Skills: Adding Specialized Knowledge on Demandp.17
- DurableAgent: Durable, Resumable Agents (Workflow DevKit, v6)p.17
- Key Takeawaysp.17
- 18Building Chatbots with useChatp.18
- 16.1 The minimal chatbotp.18
- 16.2 The UIMessage model and rendering partsp.18
- 16.3 The UIMessage-vs-ModelMessage split on the serverp.18
- 16.4 Status: driving the UI through the request lifecyclep.18
- 16.5 Stopping and regeneratingp.18
- 16.6 Error handlingp.18
- 16.7 Directly modifying messagesp.18
- 16.8 Event callbacksp.18
- +5 more sections
- 19Persistence, Resumable Streams, and Transportsp.19
- 17.1 The persistence modelp.19
- 17.2 Starting and loading a chatp.19
- 17.3 Saving on the serverp.19
- 17.4 Message IDs and server-side generationp.19
- 17.5 Sending only the last messagep.19
- 17.6 Handling client disconnectsp.19
- 17.7 Resumable streamsp.19
- 17.8 Stopping a resumable streamp.19
- +2 more sections
- 20Tool Usage and Human-in-the-Loop in the UIp.20
- 18.1 The three tool execution patternsp.20
- 18.2 Tool part statesp.20
- 18.3 Rendering tool partsp.20
- 18.4 Streaming tool inputsp.20
- 18.5 Client-side tool executionp.20
- 18.6 Human-in-the-loop: confirmation toolsp.20
- 18.7 Human-in-the-loop: server-side tool approvalp.20
- 18.8 Dynamic tools and step boundariesp.20
- +2 more sections
- 21Generative User Interfacesp.21
- How Generative UI Worksp.21
- Building a Generative UI Chat Interfacep.21
- Defining a Toolp.21
- The Display Componentp.21
- Mapping Tool Parts to Componentsp.21
- Adding More Tools and Componentsp.21
- Streaming Structured UI State with Data Partsp.21
- Rendering Persistent Data Partsp.21
- +3 more sections
- 22Completion, Object Generation, and Custom Data Streamingp.22
- useCompletion: Single-Prompt Completionsp.22
- useObject: Streaming Structured Objectsp.22
- Streaming Custom Datap.22
- UI Error Handling and Warningsp.22
- Reading UI Message Streamsp.22
- Message Metadatap.22
- Key takeawaysp.22
- 23Framework and Server Integrationp.23
- The Two Families of Response Helpersp.23
- Next.js Route Handlers (Recap)p.23
- Node.js HTTP Serverp.23
- Expressp.23
- Honop.23
- Fastifyp.23
- Nest.jsp.23
- Streaming Considerations Across Frameworksp.23
- +2 more sections
- 24The Vercel AI Gatewayp.24
- Why route through a Gatewayp.24
- Basic usage: plain model stringsp.24
- The `gateway` provider instancep.24
- Authentication: API key, access token, or OIDCp.24
- The model id format and model routingp.24
- Automatic fallbacks and failoverp.24
- Per-provider options through the Gatewayp.24
- Provider-executed and Gateway-executed toolsp.24
- +6 more sections
- 25Provider Deep-Divesp.25
- How provider packages are structuredp.25
- OpenAIp.25
- Anthropicp.25
- Google Generative AI and Vertexp.25
- Amazon Bedrockp.25
- Groqp.25
- Mistralp.25
- xAI Grokp.25
- +5 more sections
- 26Production Best Practicesp.26
- 24.1 Architecture and configurationp.26
- 24.2 Streaming and UXp.26
- 24.3 Tool calling and agentsp.26
- 24.4 Structured outputp.26
- 24.5 Cost and performancep.26
- 24.6 Securityp.26
- 24.7 Reliability and observabilityp.26
- 24.8 Production deploymentp.26
- +1 more section
- 27End-to-End Projectsp.27
- Project 1 — RAG Chatbotp.27
- Project 2 — Natural-Language-to-SQL over Postgresp.27
- Project 3 — Multi-Modal Agentp.27
- Project 4 — Slack AI Agentp.27
- Project 5 — Computer Usep.27
- Key takeawaysp.27
- 28Appendix A — Error Referencep.28
- Quick referencep.28
- Call and transport errorsp.28
- Generation result errorsp.28
- Tool-calling errorsp.28
- Configuration and lookup errorsp.28
- Validation and parsing errorsp.28
- Streaming and message errorsp.28
- Embedding errorsp.28
- +1 more section
- 29Appendix B — Migration Guide (v3 → v6)p.29
- B.1 v3 → v4p.29
- B.2 v4 → v5 — the big rebuildp.29
- B.3 v5 → v6 — renames and new defaultsp.29
- B.4 A pragmatic upgrade checklistp.29
- Key takeawaysp.29



