vercel-ai-sdk-development
Use when building AI-powered applications with the Vercel AI SDK (V6+). Covers agents (ToolLoopAgent), tool design with execution approval and strict mode, MCP client integration, structured output with tool calling, streaming patterns, DevTools debugging, reranking, provider-specific tools, and UI integration with React/Next.js.
Best use case
vercel-ai-sdk-development is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Use when building AI-powered applications with the Vercel AI SDK (V6+). Covers agents (ToolLoopAgent), tool design with execution approval and strict mode, MCP client integration, structured output with tool calling, streaming patterns, DevTools debugging, reranking, provider-specific tools, and UI integration with React/Next.js.
Teams using vercel-ai-sdk-development 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
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/vercel-ai-sdk-development/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How vercel-ai-sdk-development Compares
| Feature / Agent | vercel-ai-sdk-development | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Use when building AI-powered applications with the Vercel AI SDK (V6+). Covers agents (ToolLoopAgent), tool design with execution approval and strict mode, MCP client integration, structured output with tool calling, streaming patterns, DevTools debugging, reranking, provider-specific tools, and UI integration with React/Next.js.
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 V6 Development
Guidance for building production-grade AI applications using the Vercel AI SDK V6+. This skill covers the agent abstraction, tool ecosystem, MCP integration, structured output, streaming, and UI patterns.
## When to Use
- Building any AI/LLM-powered feature in a TypeScript project
- Creating agents with multi-step tool calling loops
- Integrating LLM capabilities into Next.js, React, Svelte, Vue, or Node.js applications
- Designing tools with human-in-the-loop approval workflows
- Connecting to MCP servers from application code
- Generating structured output from LLM calls
- Implementing RAG pipelines with reranking
- Debugging AI call chains with DevTools
## Core Concepts
### Model Gateway
Use the AI SDK gateway for provider-agnostic model references. This decouples your code from specific providers and enables easy model switching:
```typescript
import { gateway } from 'ai';
const model = gateway('anthropic/claude-sonnet-4.5');
```
### Agents with ToolLoopAgent
Prefer `ToolLoopAgent` for reusable agent definitions. Define the agent once with its model, instructions, and tools, then use it across API routes, background jobs, and UI streams:
```typescript
import { ToolLoopAgent } from 'ai';
export const myAgent = new ToolLoopAgent({
model: 'anthropic/claude-sonnet-4.5',
instructions: 'You are a helpful assistant.',
tools: { /* tools here */ },
});
const result = await myAgent.generate({ prompt: '...' });
```
Key agent patterns:
- Use `callOptionsSchema` with `prepareCall` to inject per-request context (user ID, feature flags, retrieved documents) in a type-safe way.
- Set `stopWhen: stepCountIs(N)` to cap the tool loop (default is 20 steps).
- Export `InferAgentUIMessage<typeof agent>` for end-to-end type safety from agent definition to UI components.
- For custom agent behavior, implement the `Agent` interface directly instead of extending `ToolLoopAgent`.
### Low-Level Functions
Use `generateText` / `streamText` when you need full control over a single LLM call without the agent loop, or when composing custom orchestration logic:
```typescript
import { generateText, streamText } from 'ai';
const { text } = await generateText({
model: 'anthropic/claude-sonnet-4.5',
prompt: '...',
});
```
## Tool Design Best Practices
### Structure
Define tools in dedicated files under a `tools/` directory. Each tool should have a clear `description`, a typed `inputSchema` using Zod, and an `execute` function:
```typescript
import { tool } from 'ai';
import { z } from 'zod';
export const weatherTool = tool({
description: 'Get the current weather for a city',
inputSchema: z.object({
city: z.string().describe('City name'),
}),
execute: async ({ city }) => {
// implementation
},
});
```
### Execution Approval (Human-in-the-Loop)
Set `needsApproval: true` on tools that perform destructive or sensitive actions (deleting data, processing payments, modifying production resources). Use a function for conditional approval based on input:
```typescript
export const deleteRecord = tool({
description: 'Delete a database record',
inputSchema: z.object({ id: z.string() }),
needsApproval: true,
execute: async ({ id }) => { /* ... */ },
});
```
### Strict Mode
Enable `strict: true` on tools with simple, provider-compatible schemas to guarantee input validation. Leave it off for tools with complex schemas that may not be supported in strict mode by all providers:
```typescript
export const simpleTool = tool({
description: '...',
inputSchema: z.object({ query: z.string() }),
strict: true,
execute: async ({ query }) => { /* ... */ },
});
```
### Input Examples
Provide `inputExamples` for tools with complex or domain-specific input patterns. This helps models generate correctly structured inputs:
```typescript
export const searchTool = tool({
description: 'Search documents with filters',
inputSchema: z.object({
query: z.string(),
dateRange: z.string().describe('ISO date range, e.g. 2025-01-01..2025-12-31'),
}),
inputExamples: [
{ input: { query: 'quarterly report', dateRange: '2025-01-01..2025-03-31' } },
],
execute: async (input) => { /* ... */ },
});
```
### Custom Model Output with toModelOutput
Use `toModelOutput` to control what tokens go back to the model, independent of what `execute` returns. This reduces token usage when tools return large payloads:
```typescript
export const docSearchTool = tool({
description: 'Search documents',
inputSchema: z.object({ query: z.string() }),
execute: async ({ query }) => {
return { fullDocument: '...very long content...' };
},
toModelOutput: async ({ input, output }) => ({
type: 'text',
value: `Found document matching "${input.query}": ${output.fullDocument.slice(0, 200)}...`,
}),
});
```
## Structured Output with Tool Calling
Combine multi-step tool calling with structured output generation using the `output` option. Use `Output.object()`, `Output.array()`, `Output.choice()`, `Output.json()`, or `Output.text()`:
```typescript
import { Output, ToolLoopAgent } from 'ai';
import { z } from 'zod';
const agent = new ToolLoopAgent({
model: 'anthropic/claude-sonnet-4.5',
tools: { /* ... */ },
output: Output.object({
schema: z.object({
summary: z.string(),
confidence: z.number(),
recommendations: z.array(z.string()),
}),
}),
});
const { output } = await agent.generate({ prompt: '...' });
```
## MCP Client Integration
Use `@ai-sdk/mcp` to connect to remote MCP servers and use their tools:
```typescript
import { createMCPClient } from '@ai-sdk/mcp';
const mcpClient = await createMCPClient({
transport: {
type: 'http',
url: 'https://your-server.com/mcp',
headers: { Authorization: 'Bearer ...' },
},
});
const tools = await mcpClient.tools();
```
For servers requiring OAuth, use the `auth` helper with an `OAuthClientProvider` to handle PKCE, token refresh, and dynamic client registration automatically.
Access MCP resources and prompts:
```typescript
const resources = await mcpClient.listResources();
const data = await mcpClient.readResource({ uri: 'file:///path' });
const prompts = await mcpClient.experimental_listPrompts();
const prompt = await mcpClient.experimental_getPrompt({
name: 'code_review',
arguments: { code: '...' },
});
```
## Provider-Specific Tools
Leverage provider-native tools when the platform offers optimized capabilities:
- **Anthropic:** Memory tool, tool search (regex/BM25), code execution, programmatic tool calling via `allowedCallers`
- **OpenAI:** Shell tool, apply-patch tool, MCP tool
- **Google:** Google Maps grounding, Vertex RAG Store, file search
- **xAI:** Web search, X search, code execution, image/video analysis
Prefer provider tools over custom implementations when they offer platform-optimized model integration.
## Reranking for RAG
Use the `rerank` function to reorder retrieved documents by relevance before passing them to the model. This reduces noise and improves response quality:
```typescript
import { rerank } from 'ai';
import { cohere } from '@ai-sdk/cohere';
const { rerankedDocuments } = await rerank({
model: cohere.reranking('rerank-v3.5'),
documents,
query: 'user question',
topN: 5,
});
```
Supports both plain-text and structured document reranking. Currently available with Cohere, Amazon Bedrock, and Together.ai.
## UI Integration (React / Next.js)
### Streaming with useChat
Use `useChat` on the client with typed messages from your agent definition:
```typescript
import { useChat } from '@ai-sdk/react';
import type { MyAgentUIMessage } from '@/agents/my-agent';
const { messages, sendMessage } = useChat<MyAgentUIMessage>();
```
### API Route Pattern
Expose agents via API routes using `createAgentUIStreamResponse`:
```typescript
import { createAgentUIStreamResponse } from 'ai';
import { myAgent } from '@/agents/my-agent';
export async function POST(request: Request) {
const { messages } = await request.json();
return createAgentUIStreamResponse({
agent: myAgent,
uiMessages: messages,
});
}
```
### Tool Approval in UI
Handle `needsApproval` tools by checking `invocation.state === 'approval-requested'` and calling `addToolApprovalResponse` with the approval decision.
## DevTools for Debugging
Wrap models with `devToolsMiddleware` to get full visibility into LLM calls during development:
```typescript
import { wrapLanguageModel } from 'ai';
import { devToolsMiddleware } from '@ai-sdk/devtools';
const debugModel = wrapLanguageModel({
model: gateway('anthropic/claude-sonnet-4.5'),
middleware: devToolsMiddleware(),
});
```
Run `npx @ai-sdk/devtools` and open `http://localhost:4983` to inspect input/output, token usage, timing, and raw provider payloads. Use this during development to trace multi-step agent behavior.
## Project Organization
Recommended file layout for AI SDK projects:
```
src/
├── agents/ # Agent definitions (one file per agent)
├── tools/ # Tool definitions (one file per tool)
├── api/ # API routes exposing agents
├── components/ # UI components for tool views
└── lib/ # Shared utilities, MCP clients, provider config
```
## Key Rules
- **Always use AI SDK V6+** — Do not use deprecated V4/V5 patterns. Use `ToolLoopAgent` instead of manual `while` loops around `generateText`.
- **Gateway for model references** — Use `gateway('provider/model')` for provider-agnostic code. Avoid hardcoding provider-specific client imports unless using provider tools.
- **Define agents, not inline configs** — Create reusable agent files rather than repeating model/tools/instructions at every call site.
- **Tools in dedicated files** — Keep tool definitions in `tools/` for reuse across agents and type sharing with UI.
- **Approval on destructive actions** — Always set `needsApproval` on tools that modify, delete, or spend resources.
- **Control model output size** — Use `toModelOutput` on tools that return large payloads to avoid wasting context window tokens.
- **Structured output over parsing** — Use `Output.object()` with a Zod schema instead of manually parsing LLM text output.
- **Rerank before stuffing context** — When doing RAG, rerank retrieved documents and pass only the top-N to the model.
- **DevTools in development** — Always enable `devToolsMiddleware` during development for visibility into multi-step agent behavior.
- **Consult latest docs** — Use Context7 MCP (or equivalent) to verify current AI SDK API surfaces, as the SDK evolves rapidly.Related Skills
vercel
Deploys applications to Vercel including serverless functions, edge functions, environment variables, and CI/CD. Use when deploying Next.js applications, frontend projects, or serverless APIs.
vercel-workflow-sdk
write code that uses https://useworkflow.dev/ on Vercel
vercel-web-design-guidelines
Reviews UI code for compliance with web interface best practices. Use when auditing accessibility, reviewing UI/UX patterns, checking performance, or improving web design quality. Triggers on "check my site", "audit design", "accessibility review", "UX best practices", or UI code review tasks. Covers 100+ rules for accessibility, performance, and user experience.
vercel-react-best-practices
React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
vercel-deployment
Vercel deployment patterns and best practices. Use when deploying frontend applications, configuring edge functions, setting up preview deployments, or optimizing Next.js applications.
vercel-deploy
Deploy applications and websites to Vercel. Use this skill when the user requests deployment actions such as "Deploy my app", "Deploy this to production", "Create a preview deployment", "Deploy and give me the link", or "Push this live". No authentication required - returns preview URL and claimable deployment link.
vercel-deploy-claimable
Deploy applications and websites to Vercel. Use this skill when the user requests deployment actions such as 'Deploy my app', 'Deploy this to production', 'Create a preview deployment', 'Deploy and...
vercel-composition-patterns
React composition patterns that scale. Use when refactoring components with boolean prop proliferation, building flexible component libraries, or designing reusable APIs. Triggers on tasks involving compound components, render props, context providers, or component architecture. Includes React 19 API changes.
vercel-ai-sdk-best-practices
Best practices for using the Vercel AI SDK in Next.js 15 applications with React Server Components and streaming capabilities.
ui-development
Generate production-ready Next.js projects with TypeScript, Tailwind CSS, shadcn/ui, and API integration. Use when the user asks to build, create, develop, or scaffold a Next.js application, web app, full-stack project, or frontend with backend integration. Prioritizes modern stack (Next.js 14+, TypeScript, shadcn/ui, axios, react-query) and best practices. Also triggers on requests to add features, integrate APIs, or extend existing Next.js projects.
u01934-handoff-contracting-for-research-and-development-labs
Operate the "Handoff Contracting for research and development labs" capability in production for research and development labs workflows. Use when mission execution explicitly requires this capability and outcomes must be reproducible, policy-gated, and handoff-ready.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code