vercel-ai-sdk

Comprehensive guide to Vercel AI SDK for building AI-powered applications with text generation, streaming, tool calling, autonomous agents, and React UI integration. Use when working with AI SDK, Vercel AI, useChat, streamText, generateText, generateObject, tool calling, LLM tools, AI agents, streaming chat, chatbots, AI Elements, PromptInput, Message components, streamUI, or building conversational interfaces with language models. Focuses on Anthropic Claude with extended thinking, prompt caching, and code execution.

16 stars

Best use case

vercel-ai-sdk is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Comprehensive guide to Vercel AI SDK for building AI-powered applications with text generation, streaming, tool calling, autonomous agents, and React UI integration. Use when working with AI SDK, Vercel AI, useChat, streamText, generateText, generateObject, tool calling, LLM tools, AI agents, streaming chat, chatbots, AI Elements, PromptInput, Message components, streamUI, or building conversational interfaces with language models. Focuses on Anthropic Claude with extended thinking, prompt caching, and code execution.

Teams using vercel-ai-sdk should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/vercel-ai-sdk/SKILL.md --create-dirs "https://raw.githubusercontent.com/diegosouzapw/awesome-omni-skill/main/skills/ai-agents/vercel-ai-sdk/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/vercel-ai-sdk/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How vercel-ai-sdk Compares

Feature / Agentvercel-ai-sdkStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Comprehensive guide to Vercel AI SDK for building AI-powered applications with text generation, streaming, tool calling, autonomous agents, and React UI integration. Use when working with AI SDK, Vercel AI, useChat, streamText, generateText, generateObject, tool calling, LLM tools, AI agents, streaming chat, chatbots, AI Elements, PromptInput, Message components, streamUI, or building conversational interfaces with language models. Focuses on Anthropic Claude with extended thinking, prompt caching, and code execution.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# Vercel AI SDK Expert

Build production-ready AI applications with streaming, tool calling, autonomous agents, and polished UI components. This skill covers the AI SDK Core (text generation, structured output, tools), AI SDK UI (React hooks), AI SDK RSC (experimental server components), and AI Elements (pre-built UI components).

## Installation

```bash
# Core SDK + Anthropic provider (recommended)
npm install ai @ai-sdk/anthropic zod

# React hooks for UI
npm install @ai-sdk/react

# AI Elements components (requires shadcn/ui)
npx ai-elements@latest add message prompt-input conversation
```

**Prerequisites:**

- Node.js 18+
- Next.js (App Router recommended)
- Anthropic API key: `ANTHROPIC_API_KEY` in `.env.local`
- For AI Elements: shadcn/ui, Tailwind CSS 4, React 19

## Quick Start: Chat with Tools

```typescript
// app/api/chat/route.ts
import { anthropic } from "@ai-sdk/anthropic";
import { streamText, tool } from "ai";
import { z } from "zod";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({
    model: anthropic("claude-sonnet-4-5"),
    messages,
    tools: {
      getWeather: tool({
        description: "Get weather for a location",
        parameters: z.object({
          location: z.string(),
        }),
        execute: async ({ location }) => ({
          temperature: 72,
          condition: "sunny",
        }),
      }),
    },
  });

  return result.toDataStreamResponse();
}
```

```typescript
// app/page.tsx
'use client';
import { useChat } from '@ai-sdk/react';
import { Message, PromptInput } from '@/components/ai-elements';

export default function Chat() {
  const { messages, sendMessage, status } = useChat();

  return (
    <div>
      {messages.map(msg => (
        <Message key={msg.id} from={msg.role}>
          {msg.content}
        </Message>
      ))}
      <PromptInput
        onSubmit={({ text }) => sendMessage({ content: text })}
        disabled={status === 'streaming'}
      />
    </div>
  );
}
```

## Core Concepts

### 1. Text Generation

**`streamText()`** - Stream responses for interactive UIs

- Real-time token streaming to client
- Tool calling with automatic execution
- Callbacks: `onChunk`, `onFinish`, `onStepFinish`
- Returns: `toDataStreamResponse()`, `textStream`, `fullStream`

**`generateText()`** - Synchronous generation for non-interactive tasks

- Await full completion (drafts, summaries, agents)
- Tool calling with multiple rounds
- Returns: `text`, `toolCalls`, `toolResults`, `usage`, `steps`

**`generateObject()`** - Structured output with Zod schema validation

- Extract typed data from unstructured input
- Returns: `object`, `usage`, `warnings`

See [references/core-api.md](references/core-api.md) for full API reference.

### 2. Tool Calling

Define tools with `tool()` helper:

```typescript
tool({
  description: "Present options to user as buttons",
  parameters: z.object({
    question: z.string(),
    options: z.array(z.string()),
  }),
  execute: async ({ question, options }) => {
    return { selected: null }; // UI-only tool
  },
});
```

**Key Patterns:**

- **Multi-step**: Use `maxSteps` or `stopWhen` for tool loops
- **UI-only tools**: Return minimal data, render in client
- **Programmatic tools** (Anthropic): Tools callable from code execution

See [references/agents.md](references/agents.md) for workflow patterns.

### 3. Autonomous Agents

**`ToolLoopAgent`** - Multi-step autonomous agent

```typescript
import { ToolLoopAgent } from "ai/agents";

const agent = new ToolLoopAgent({
  model: anthropic("claude-sonnet-4-5"),
  instructions: systemPrompt,
  tools: { searchDocs, updateSpec, scheduleCall },
  stopWhen: (result) => {
    // Stop when UI-only tool called
    return result.steps.some(
      (step) =>
        "toolCalls" in step &&
        step.toolCalls.some((tc) => tc.toolName === "scheduleCall"),
    );
  },
});

const result = await agent.execute({ messages });
return createAgentUIStreamResponse({ result }).toUIMessageStreamResponse();
```

See [references/agents.md](references/agents.md) for loop control and oyster patterns.

### 4. React Hooks (AI SDK UI)

**`useChat()`** - Complete chat interface state management

- **Messages**: Array of `UIMessage` with parts (text, tool-call, tool-result)
- **`sendMessage()`**: Send with optional files, metadata
- **Status**: `'ready' | 'submitted' | 'streaming' | 'error'`
- **Transports**: DefaultChatTransport (HTTP), DirectChatTransport (in-process)

```typescript
const { messages, sendMessage, status, stop, reload } = useChat({
  api: "/api/chat",
  onToolCall: ({ toolCall }) => {
    if (toolCall.toolName === "updateSpec") {
      setSpec(toolCall.input);
    }
  },
});
```

See [references/ui-hooks.md](references/ui-hooks.md) for full hook APIs.

### 5. AI Elements Components

Pre-built, customizable components built on shadcn/ui:

- **`<Message>`** - Container with role-based styling (`from="user" | "assistant"`)
- **`<MessageResponse>`** - Markdown rendering with syntax highlighting
- **`<PromptInput>`** - Input with file upload, voice, submit button
- **`<Conversation>`** - Auto-scrolling container with empty states
- **`<Reasoning>`** - Collapsible thinking/reasoning display
- **`<Tool>`** - Tool execution visualization

Install: `npx ai-elements@latest add <component-name>`

See [references/elements-components.md](references/elements-components.md) for full component APIs.

### 6. React Server Components (Experimental)

**⚠️ Not production-ready** - Use AI SDK UI for production apps.

**`streamUI()`** - Stream React components from server

```typescript
const result = streamUI({
  model: anthropic('claude-sonnet-4-5'),
  prompt: 'Show me a chart',
  tools: {
    showChart: tool({
      parameters: z.object({ data: z.array(z.number()) }),
      generate: function* ({ data }) {
        yield <Spinner />;
        return <Chart data={data} />;
      },
    }),
  },
});

return result.value; // ReactNode
```

See [references/rsc-api.md](references/rsc-api.md) for RSC APIs.

## Anthropic-Specific Features

While AI SDK is provider-agnostic, Anthropic Claude offers unique capabilities:

### Extended Thinking

Models reason through complex problems before responding:

```typescript
streamText({
  model: anthropic("claude-sonnet-4"),
  providerOptions: {
    anthropic: {
      thinking: { type: "enabled", budgetTokens: 12000 },
    },
  },
});
```

Render in UI with `<Reasoning>` component or access `reasoningText` in results.

### Prompt Caching

Cache system prompts, long contexts, or tool definitions:

```typescript
const systemMessage: CoreSystemMessage = {
  role: "system",
  content: largeContext,
  experimental_providerMetadata: {
    anthropic: { cacheControl: { type: "ephemeral", ttl: "1h" } },
  },
};
```

Reduces cost and latency for repeated contexts (>1024 tokens).

### Code Execution

Run Python in sandboxed containers:

```typescript
import { anthropic } from "@ai-sdk/anthropic";

streamText({
  model: anthropic("claude-sonnet-4-5"),
  tools: {
    codeExecution: anthropic.tools.codeExecution_20250825(),
    // Programmatic tools - callable from code
    searchDocs: tool({
      parameters: z.object({ query: z.string() }),
      execute: async ({ query }) => searchResults,
      experimental_allowedCallers: ["code_execution_20250825"],
    }),
  },
  providerOptions: {
    anthropic: {
      forwardAnthropicContainerIdFromLastStep: true, // Persist container
    },
  },
});
```

See [references/anthropic-features.md](references/anthropic-features.md) for provider-defined tools, MCP connectors, context management, and more.

## Error Handling

AI SDK provides robust error handling patterns:

```typescript
try {
  const result = await streamText({ model, messages });
} catch (error) {
  if (error instanceof APICallError) {
    console.error("API Error:", error.statusCode, error.message);
  } else if (error instanceof InvalidToolArgumentsError) {
    console.error("Tool Error:", error.toolName, error.cause);
  }
}
```

**Patterns:**

- **Retries**: Wrap calls with exponential backoff
- **Partial results**: Handle `toolResults` even on errors
- **Tool fallbacks**: Catch in `execute()`, return error state
- **Stream interruption**: Use `stop()` from useChat

See [references/error-handling.md](references/error-handling.md) for comprehensive patterns.

## Common Workflows

1. **Simple Chat**: `useChat()` + `streamText()` with message API
2. **Chat with Tools**: Add `tools` object, render with custom UI
3. **Autonomous Agent**: `ToolLoopAgent` with `stopWhen` control
4. **Structured Extraction**: `generateObject()` with Zod schema
5. **UI Streaming**: `createAgentUIStreamResponse()` for tool streaming
6. **File Upload**: `PromptInput` with file handling, multi-modal messages

## Architecture Patterns

### Server-Side (API Route)

```typescript
// app/api/chat/route.ts
import { anthropic } from "@ai-sdk/anthropic";
import { streamText } from "ai";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = streamText({
    model: anthropic("claude-sonnet-4-5"),
    messages,
    system: buildSystemPrompt(),
    tools: createTools(),
  });

  return result.toDataStreamResponse();
}
```

### Client-Side (React)

```typescript
// components/chat.tsx
'use client';
import { useChat } from '@ai-sdk/react';
import { Message, PromptInput, Conversation } from '@/components/ai-elements';

export function Chat() {
  const { messages, sendMessage, status } = useChat({ api: '/api/chat' });

  return (
    <Conversation>
      {messages.map(msg => (
        <Message key={msg.id} from={msg.role}>
          <MessageResponse>{msg.content}</MessageResponse>
        </Message>
      ))}
      <PromptInput onSubmit={({ text }) => sendMessage({ content: text })} />
    </Conversation>
  );
}
```

## Progressive Disclosure

- **Start here**: Quick start example, core concepts
- **Deep dive**: Reference files for specific APIs
- **Real examples**: `examples/` folder with production patterns
- **Anthropic features**: When you need advanced capabilities

## Reference Files

Load as needed for detailed documentation:

- [core-api.md](references/core-api.md) - generateText, streamText, generateObject, tool definitions
- [ui-hooks.md](references/ui-hooks.md) - useChat, useObject, transports, message handling
- [agents.md](references/agents.md) - ToolLoopAgent, stopWhen patterns, oyster workflows
- [rsc-api.md](references/rsc-api.md) - streamUI, state management (experimental)
- [elements-components.md](references/elements-components.md) - Full component prop tables
- [anthropic-features.md](references/anthropic-features.md) - Thinking, caching, code execution, provider tools
- [error-handling.md](references/error-handling.md) - Retry patterns, fallbacks, validation

## Examples

See [examples/](examples/) for complete implementations:

- [tool-registry.tsx](examples/tool-registry.tsx) - Custom tool UI rendering
- [chat-with-tools.tsx](examples/chat-with-tools.tsx) - Full chat with tool calling
- [streaming-object.tsx](examples/streaming-object.tsx) - Structured data extraction
- [elements-chat.tsx](examples/elements-chat.tsx) - Chat with AI Elements
- [anthropic-thinking.tsx](examples/anthropic-thinking.tsx) - Reasoning display
- [agent-workflow.tsx](examples/agent-workflow.tsx) - Multi-step autonomous agent

## Best Practices

1. **Use `streamText()` for UIs**, `generateText()` for non-interactive tasks
2. **Define tools clearly** - Descriptive names, precise parameter schemas
3. **Control loops** - Use `stopWhen` or `maxSteps` to prevent runaway agents
4. **Cache aggressively** - System prompts, tool definitions, long contexts (Anthropic)
5. **Handle errors gracefully** - Retries, fallbacks, partial results
6. **Throttle UI updates** - `experimental_throttle` for smooth rendering
7. **Type everything** - Use Zod for tool parameters and structured output
8. **Test tools independently** - Mock `execute` functions for reliability
9. **Monitor usage** - Track tokens, costs, errors in callbacks
10. **Progressive enhancement** - Start simple, add tools/agents as needed

## Resources

- **Docs**: https://ai-sdk.dev/docs
- **AI Elements**: https://ai-sdk.dev/elements
- **Examples**: https://github.com/vercel/ai
- **Anthropic**: https://docs.anthropic.com

Related Skills

vercel-automation

16
from diegosouzapw/awesome-omni-skill

Automate Vercel tasks via Rube MCP (Composio): manage deployments, domains, DNS, env vars, projects, and teams. Always search tools first for current schemas.

vercel-secure-deploy

16
from diegosouzapw/awesome-omni-skill

将 Google AI Studio 项目部署到 Vercel。当用户提到"部署"、"上线"、"发布网站"、"Vercel"、"保护 API 密钥"时触发。

vercel-kv-database-rules

16
from diegosouzapw/awesome-omni-skill

Defines how to interact with Vercel's KV database for storing and retrieving session and application data.

how-to-use-vercel-skills

16
from diegosouzapw/awesome-omni-skill

How to create, structure, install, and publish agent skills compatible with the Vercel skills ecosystem and skills.sh. Use when the user wants to build a new skill, structure a SKILL.md file, add install commands to a website or README, or publish skills for distribution across 40+ AI agents.

agent-builder-vercel-sdk

16
from diegosouzapw/awesome-omni-skill

Build conversational AI agents using Vercel AI SDK + OpenRouter. Use when creating Next.js frontends with streaming UI, tool calling, and multi-provider support.

bgo

10
from diegosouzapw/awesome-omni-skill

Automates the complete Blender build-go workflow, from building and packaging your extension/add-on to removing old versions, installing, enabling, and launching Blender for quick testing and iteration.

Coding & Development

moai-lang-r

16
from diegosouzapw/awesome-omni-skill

R 4.4+ best practices with testthat 3.2, lintr 3.2, and data analysis patterns.

moai-lang-python

16
from diegosouzapw/awesome-omni-skill

Python 3.13+ development specialist covering FastAPI, Django, async patterns, data science, testing with pytest, and modern Python features. Use when developing Python APIs, web applications, data pipelines, or writing tests.

moai-icons-vector

16
from diegosouzapw/awesome-omni-skill

Vector icon libraries ecosystem guide covering 10+ major libraries with 200K+ icons, including React Icons (35K+), Lucide (1000+), Tabler Icons (5900+), Iconify (200K+), Heroicons, Phosphor, and Radix Icons with implementation patterns, decision trees, and best practices.

moai-foundation-trust

16
from diegosouzapw/awesome-omni-skill

Complete TRUST 4 principles guide covering Test First, Readable, Unified, Secured. Validation methods, enterprise quality gates, metrics, and November 2025 standards. Enterprise v4.0 with 50+ software quality standards references.

moai-foundation-memory

16
from diegosouzapw/awesome-omni-skill

Persistent memory across sessions using MCP Memory Server for user preferences, project context, and learned patterns

moai-foundation-core

16
from diegosouzapw/awesome-omni-skill

MoAI-ADK's foundational principles - TRUST 5, SPEC-First TDD, delegation patterns, token optimization, progressive disclosure, modular architecture, agent catalog, command reference, and execution rules for building AI-powered development workflows