Source: AI SDK documentation (Vercel)
Official AI SDK docs: Core (generateText/streamText, Output, tools, stopWhen/prepareStep), ToolLoopAgent, middleware, the provider specification, and AI SDK UI's useChat and stream protocol.
Contents
- What: official AI SDK docs pages: Introduction, Generating Text, Generating Structured Data, Tools and Tool Calling, Agents Overview, Loop Control, Language Model Middleware, Providers and Models, Chatbot (
useChat), Stream Protocols - Publisher: Vercel
- Date: living docs, read 2026-11-14; npm
ailatest was 7.0.172 at that date (7.0.0 published 2026-06-25; 6.0.0 on 2025-12-22; 5.0.0 on 2025-07-31) - Original: ai-sdk.dev/docs (every page also served as Markdown by appending
.md)
Summary
The AI SDK is a TypeScript toolkit with three surfaces: Core (a unified API for text, structured output, tool calls and agents across providers), UI (framework-agnostic hooks such as useChat for chat and generative UIs), and Harnesses (running Claude Code, Codex or Pi behind the same agent interface). Providers implement a shared language-model specification, so switching models means changing the model reference.
Key claims
generateTextreturns a complete result;streamTextstreams immediately, applies backpressure (tokens are generated only as consumed, so the stream must be consumed to finish), and turns errors into stream parts instead of throwing; log them withonError. Errors thrown insidegenerateText’s experimental lifecycle callbacks are silently caught.- Structured output is the
outputoption ongenerateText/streamText:Output.text()(default),Output.object(),Output.array(),Output.choice(),Output.json(), with Zod, Valibot or JSON Schema. It can be combined with tools, and it counts as a loop step.partialOutputStreamand theuseObjecthook stream partial objects. - Multi-step tool use is controlled by
stopWhen:isStepCount(n)(defaultisStepCount(1)forgenerateText/streamText),hasToolCall(...),isLoopFinished(), arrays of conditions (stop when any is met), or custom predicates. Conditions are evaluated only when the last step contains tool results. ToolLoopAgentis the recommended agent class; it defaults toisStepCount(20)and warns if that default cuts off a loop.WorkflowAgent.stream()has no default step limit;WorkflowAgent.generate()stops after 20 steps by default.- The loop also stops when a called tool has no
executefunction (client-side tools) or needs approval. prepareStepruns before each step and can switch model, tools,toolChoice, sampling settings, or rewritemessages(the rewritten list persists for later steps), which supports compaction viapruneMessages.- Multiple tool calls in one step run concurrently by default;
toolCallConcurrency: 1forces sequential order. - Tool errors are typed (
NoSuchToolError,InvalidToolInputError,ToolCallRepairError) and there is a tool-call repair hook. Tool approval moved fromneedsApproval(deprecated) totoolApproval. runtimeContextcarries shared server-side state through the loop;toolsContextgives each tool only its own typed context.wrapLanguageModelapplies middleware (guardrails, RAG, caching, logging) to any model.useChatmanages message state and streaming; render messages by theirparts(text, tool invocations, results). The UI message stream is SSE with anx-vercel-ai-ui-message-stream: v1header; a plain text-stream protocol exists for text-only use. The protocol is documented so non-JS backends (e.g. FastAPI) can serveuseChat.