Vercel AI SDK - Provider-Agnostic TypeScript Toolkit

repository·main·Indexed Apr 17, 2026

https://github.com/vercel/ai

The Vercel AI SDK is a provider-agnostic TypeScript toolkit for building AI-powered applications. It offers unified APIs for text generation, structured data output, image generation, speech synthesis, and video generation across frameworks like React, Svelte, Vue, and Angular. The SDK supports major model providers including OpenAI, xAI Grok, Vercel v0, Anthropic, Google Vertex, Baseten, Alibaba Cloud (Qwen), and Amazon Bedrock. Key features include the `generateText` function for text generation, `Output` helper for Zod-based structured output, `ToolLoopAgent` for building agents with tools, and React hooks like `useChat` for UI integration. It enables developers to create chat interfaces, generate structured data, and build agents using React Server Components and Server Actions.

Tokens
845.9K
Snippets
2.3K
Records
2.5K
Agent score
99%

What's inside Vercel AI SDK

  1. Overview of AI SDK Core for LLM integration

    main

    AI SDK Core provides a standardized way to integrate Large Language Models (LLMs) into your applications, allowing you to focus on building AI features rather than handling technical integration details. It offers a unified API for text generation, structured data output, and tool usage across different model providers.

    The core functions are:

    • generateText: Generates text and tool calls. Ideal for non-interactive automation tasks like drafting emails or summarizing content.
    • streamText: Streams text and tool calls. Designed for interactive use cases like chat bots and content streaming.

    Both functions support structured output via the output property (e.g., Output.object(), Output.array()), enabling typed, schema-validated data generation for extraction, classification, and streaming UIs.

    // Example: Using generateText for automation
    import { generateText } from 'ai';
    
    const { text, toolCalls } = await generateText({
      model: myModel,
      prompt: 'Summarize this article: ...',
      output: 'object',
      schema: z.object({ summary: z.string() })
    });
    
    // Example: Using streamText for interactive chat
    import { streamText } from 'ai';
    
    const stream = await streamText({
      model: myModel,
      prompt: 'Hello, how can I help?',
    });
    
    for await (const chunk of stream.textStream) {
      console.log(chunk);
    }
  2. Overview of AI SDK RSC and migration guidance

    main

    AI SDK RSC is an experimental feature for building AI-powered applications using React Server Components. It provides utilities for streaming React components from the server to the client, managing AI state, and calling server actions from client components.

    Warning: AI SDK RSC is currently experimental. For production applications, the documentation recommends using AI SDK UI instead. If you are currently using RSC, refer to the migration guide for instructions on moving to AI SDK UI.

  3. Overview of AI SDK UI hooks and utilities

    main

    AI SDK UI is a framework-agnostic toolkit designed to help you build interactive chat, completion, and assistant applications. It provides a set of hooks and utilities for integrating advanced AI functionalities into your applications.

    The core hooks available are:

    • useChat: Interact with language models in a chat interface.
    • useCompletion: Interact with language models in a completion interface.
    • useObject: Consume streamed JSON objects.

    Additional utilities include:

    • convertToModelMessages: Convert useChat messages to ModelMessage format for use with AI functions.
    • pruneMessages: Prune model messages from a list to optimize context.
    • createUIMessageStream: Create a stream to send additional data to the client.
    • createUIMessageStreamResponse: Create a response object to stream UI messages to the client.
    • pipeUIMessageStreamToResponse: Pipe a UI message stream to a Node.js ServerResponse object.
    • readUIMessageStream: Transform a stream of UIMessageChunk objects into an AsyncIterableStream of UIMessage objects.
  4. Overview of building agents with the AI SDK

    main

    Agents are systems where large language models (LLMs) use tools in a loop to accomplish tasks. The AI SDK provides two main approaches for building agents:

    1. ToolLoopAgent: An in-memory agent that handles the agent loop automatically. Ideal for standard use cases where durability across restarts is not required.
    2. WorkflowAgent: A durable agent that runs inside Vercel Workflows. Each tool call is a durable workflow step with automatic retries and persistence. Use this for long-running tasks or when you need resumable streaming.

    The SDK supports advanced patterns including:

    • Sequential processing chains
    • Parallel processing for independent subtasks
    • Orchestrator-worker patterns
    • Evaluator-optimizer feedback loops
    • Subagents for delegating context-heavy tasks
    • Dynamic configuration via call options
    • Persistent memory using providers like Letta, Mem0, and Supermemory
  5. Overview of AI SDK UI hooks and framework support

    main

    AI SDK UI is a framework-agnostic toolkit for building interactive chat, completion, and assistant applications. It provides three main hooks to simplify state management and UI updates:

    • useChat: Handles real-time streaming of chat messages, managing inputs, messages, loading states, and errors. Ideal for chatbot interfaces.
    • useCompletion: Manages text completions, handling prompt inputs and automatically updating the UI as new text is streamed.
    • useObject: Consumes streamed JSON objects, allowing you to handle and display structured data (like generated objects) in your application.

    Supported Frameworks:

    • React (@ai-sdk/react)
    • Vue.js (@ai-sdk/vue)
    • Svelte (@ai-sdk/svelte)
    • Angular (@ai-sdk/angular)
    • SolidJS (community package)

    All three hooks are available in React, Vue, and Svelte. Svelte and Angular use slightly different naming conventions for the object hook (StructuredObject vs useObject).

  6. Overview of AI SDK Adapters for UI integration

    main

    Adapters are lightweight integrations that enable you to use the AI SDK UI functions (useChat and useCompletion) with third-party libraries. This allows you to leverage existing AI frameworks while using the AI SDK's UI hooks for building chat interfaces and completion forms.

    Currently available adapters include:

    • LangChain: Integrate with LangChain models and agents
    • LlamaIndex: Integrate with LlamaIndex for retrieval-augmented generation
  7. Overview of AI SDK providers and model support

    main

    The AI SDK includes built-in providers for interacting with various language models, image generators, video generators, and speech synthesis services. These providers offer a unified interface across different vendors. The SDK also supports community-created providers that follow the Language Model Specification.

    To see which models are available, refer to the official model cards and community model cards sections in the documentation. Not all providers support all features; check the provider comparison to understand capabilities for specific models before implementation.

  8. Overview of the AI SDK by Vercel

    main

    The AI SDK is a TypeScript toolkit for building AI-powered applications and agents with React, Next.js, Vue, Svelte, Node.js, and more. It standardizes integrating LLMs across supported providers, allowing developers to focus on application logic rather than provider-specific details.

    The SDK consists of two main libraries:

    • AI SDK Core: A unified API for generating text, structured objects, tool calls, and building agents with LLMs.
    • AI SDK UI: A set of framework-agnostic hooks for quickly building chat and generative user interfaces.

    The SDK supports multiple model providers. You can explore templates and starter kits for different use cases, providers, and frameworks on the Vercel Templates page.

  9. Overview of AI SDK Core functions and utilities

    main

    AI SDK Core provides a unified set of functions for interacting with AI models across different providers. It enables text generation, tool calling, structured output, embeddings, image generation, video generation, transcription, and speech synthesis.

    Main Functions:

    • generateText(): Generate text and call tools from a language model.
    • streamText(): Stream text and call tools from a language model.
    • embed(): Generate an embedding for a single value using an embedding model.
    • embedMany(): Generate embeddings for several values using an embedding model (batch embedding).
    • generateImage(): Generate images based on a given prompt using an image model.
    • experimental_generateVideo(): Generate videos based on a given prompt using a video model.
    • experimental_transcribe(): Generate a transcript from an audio file.
    • experimental_generateSpeech(): Generate speech audio from text.
    • uploadFile(): Upload a file to a provider and get a provider reference.
    • uploadSkill(): Upload a skill to a provider and get a provider reference.

    Helper Functions:

    • tool(): Type inference helper function for tools.
    • experimental_filterActiveTools(): Filters a tool set to only the currently active tools.
    • createMCPClient(): Creates a client for connecting to MCP servers.
    • jsonSchema(): Creates AI SDK compatible JSON schema objects.
    • zodSchema(): Creates AI SDK compatible Zod schema objects.
    • createProviderRegistry(): Creates a registry for using models from multiple providers.
    • cosineSimilarity(): Calculates the cosine similarity between two vectors, e.g. embeddings.
    • simulateReadableStream(): Creates a ReadableStream that emits values with configurable delays.
    • wrapLanguageModel(): Wraps a language model with middleware.
    • wrapImageModel(): Wraps an image model with middleware.
    • extractReasoningMiddleware(): Extracts reasoning from the generated text and exposes it as a reasoning property on the result.
    • extractJsonMiddleware(): Extracts JSON from text content by stripping markdown code fences.
    • isStepCount(): Creates a stop condition that triggers after a specified number of steps.
    • hasToolCall(): Creates a stop condition that triggers when any specified tool is called.
    • isLoopFinished(): Creates a stop condition that lets the agent loop run until it naturally finishes.
    • simulateStreamingMiddleware(): Simulates streaming behavior with responses from non-streaming language models.
    • defaultSettingsMiddleware(): Applies default settings to a language model.
    • smoothStream(): Smooths text and reasoning streaming output.
    • generateId(): Helper function for generating unique IDs.
    • createIdGenerator(): Creates an ID generator.
  10. AI SDK UI documentation overview

    main
    The AI SDK UI documentation provides guides for building AI-powered interfaces including chatbots, text completion, object generation, and custom data streaming. Key topics include integrating chat interfaces with tool calling, persisting chat messages, handling errors, and understanding the stream protocol for data transmission between backend and frontend.
  11. Understand foundational AI concepts for the AI SDK

    main

    Before building with the AI SDK, understand these core concepts:

    • Generative AI: Models that predict and generate outputs (text, images, audio) based on patterns learned from training data. Examples include generating captions from photos or transcriptions from audio.
    • Large Language Models (LLMs): A subset of generative models focused on text. LLMs take a sequence of words as input and predict the most likely next sequence. They are trained on massive text collections, making them better suited for some use cases than others. Be aware of limitations like "hallucinations" (making up information) when asked about data not well-represented in their training.
    • Embedding Models: Models that convert complex data (words, images) into dense vector representations (lists of numbers). Unlike generative models, they do not generate new content but provide semantic and syntactic relationship representations useful for other NLP tasks.

    For practical implementation, skip to the quickstarts or review supported models and providers.

  12. Use React Server Components with AI SDK

    main

    The AI SDK provides React Server Components (RSC) support, allowing you to build AI-powered features directly on the server. This enables efficient data fetching and model interactions without client-side overhead. Import the RSC utilities from the @ai-sdk/rsc package to generate text, stream responses, or handle structured outputs in server components.

    import { createAI, createStreamableValue } from '@ai-sdk/rsc';
    
    // Define your AI actions in server components
    async function generateText(prompt: string) {
      const stream = createStreamableValue();
      // ... implementation using AI SDK
      return stream.value;
    }