AI SDK
Use this skill for Vercel AI SDK 7.x (ai@latest, currently 7.0.83). Dist-tags ai-v6 and ai-v5 pin previous majors. Node ≥22. ESM only. Peer zod ^3.25.76 || ^4.1.8.
Do not trust training data or older snippets for this SDK. APIs rename across majors. Verify against the installed ai version, bundled node_modules/ai/docs/ / node_modules/ai/src/ when present, or current ai-sdk.dev Markdown pages (append .md). Snapshot and lookup: source-map.md.
Python ai on PyPI is a separate product. This skill is TypeScript npm ai.
Workflow
- Inspect the local surface:
- Exact versions / dist-tags for
ai,@ai-sdk/react(or vue/svelte/angular), provider packages,@ai-sdk/mcp,@ai-sdk/gateway,@ai-sdk/otel,@ai-sdk/devtools, harness/workflow packages. - Runtime: Node version, ESM vs leftover CJS, framework (Next App Router, TanStack Start, Nuxt, SvelteKit, Expo, Node HTTP, none).
- Existing call sites:
generateText/streamText/ToolLoopAgent/useChat/ RSC / gateway strings vs dedicated providers.
- Exact versions / dist-tags for
- Refresh docs when the installed major differs from this snapshot, or the task touches agents, UI streams, MCP, harnesses, or a migration. Start from source-map.md.
- Route by concern (load only what the task needs):
- Install, packages, Node/ESM, choosing a provider: setup-packages.md
generateText/streamText, prompts, settings, reasoning, lifecycle: core-generate.mdOutput.*, schemas: structured-output.mdtool/dynamicTool,stopWhen, MCP, approvals, context: tools.mdToolLoopAgent, memory, subagents, WorkflowAgent, TUI: agents.mdHarnessAgent, adapters, sandbox, harness skills: harnesses.mduseChat/useCompletion/useObject, transports, frameworks: ui-chat.mdUIMessage/ModelMessage, parts, stream protocol: ui-messages.md- Embed, rerank, image, speech, transcription, video, files, realtime: multimodal.md
- Gateway, registry, middleware, telemetry, testing, errors: providers-middleware.md
- v4→v7 rename table and codemods: migration.md
- Copy-paste wrong→right API pairs: common-errors.md
- Match the project's provider choice (gateway string vs
@ai-sdk/<provider>). Do not force AI Gateway when the repo already uses dedicated providers. - Typecheck after changes. Grep common-errors.md for the failing symbol before inventing APIs.
Core Judgment
- Default loop is one step.
generateText/streamTextusestopWhen: isStepCount(1)unless raised.ToolLoopAgentdefaults toisStepCount(20). - Tools + structured output need extra steps. Generating
Output.object()counts as a step. RaisestopWhen. - Prompts: top-level
instructions(notsystem). Do not putrole: 'system'inmessagesunlessallowSystemInMessages: true(trusted histories only). - Do not mix
promptandmessages. Convert UI history withawait convertToModelMessages(messages)beforestreamText. - Chat HTTP (v7):
createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) }). Do not usetoDataStreamResponse.result.toUIMessageStreamResponse()is deprecated. - Consume
streamText. Unconsumed streams stall.textStreamhides errors — setonErroror iterateresult.stream. Abort skipsonEnd; useonAbort. - Render
message.parts. Nevermessage.content. Tool parts aretool-<name>(ordynamic-tool), statesinput-streaming→output-available(plus approval states). useChat:transport: new DefaultChatTransport({ api }),sendMessage({ text }),status === 'submitted' | 'streaming' | 'ready' | 'error'. Own the input withuseState.- Agents:
ToolLoopAgentfor in-process ReAct.WorkflowAgent(@ai-sdk/workflow) for durable Workflow DevKit runs.HarnessAgent(@ai-sdk/harness) for Claude Code / Codex / Pi-style runtimes. Do not hand-roll a tool loop whenToolLoopAgentfits. - MCP lives in
@ai-sdk/mcp, notai. Close the client. HTTPredirectdefaults to'error'(SSRF). - RSC (
@ai-sdk/rsc) is experimental. Prefer AI SDK UI for production. - Model IDs go stale. Do not invent “latest” IDs from memory. Prefer the project's existing IDs, provider docs, or Gateway model list. Gateway strings are
provider/model. - Telemetry is opt-out once
registerTelemetry(...)is called. OTel is@ai-sdk/otel, not built intoai. - Do not over-specify. Only set options that differ from defaults. Confirm defaults in docs/source rather than guessing.
Quick routing
| Need | Use |
|---|---|
| One-shot text | generateText |
| Streaming text / chat backend | streamText |
| JSON / objects | generateText/streamText + Output.object (not generateObject) |
| Tool loop in-process | ToolLoopAgent or stopWhen on generate/stream |
| Durable HITL / crash-safe | WorkflowAgent |
| Claude Code / Codex / Pi | HarnessAgent |
| Chat UI | useChat + DefaultChatTransport |
| Completion / object UI | useCompletion / useObject |
| RAG vectors | embed / embedMany + cosineSimilarity / rerank |
| Dedicated image model | generateImage |
| Images from a language model | generateText → result.files |
| MCP tools | createMCPClient from @ai-sdk/mcp |
Verification
Prefer repository-owned commands. For meaningful AI SDK work, cover the relevant subset:
- Typecheck
generateText/streamTextoptions,Outputtypes, toolinput/output,UIMessagegenerics,InferAgentUIMessage. - Confirm
stopWhenallows tool steps plus structured output if both are used. - Chat: send a message, stream tokens, tool part states, stop/abort, error status.
- Persistence/resume paths if those files were touched.
- After migrations: grep common-errors.md symbols (
maxSteps,generateObject,system:,handleSubmit,toDataStreamResponse,stepCountIs,CoreMessage).
Report which checks ran and which ai major was assumed.