# Vercel AI Sdk

> Vercel AI SDK for React/Next.js -- useChat, streamText, generateText, tool calling with Zod schemas, multi-step agents, and human-in-the-loop approval.

- Skill: `leolin990405/vercel-ai-sdk` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/vercel-ai-sdk`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/vercel-ai-sdk/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-11
- Page: https://skillmd.com/skills/leolin990405/vercel-ai-sdk

---


# Vercel AI SDK

> TypeScript toolkit for building AI-powered React/Next.js applications.

## Core Functions

### streamText (Server)
```typescript
import { streamText, tool } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

const result = streamText({
  model: openai("gpt-4o"),
  system: "You are a helpful coding assistant.",
  messages,
  tools: {
    searchDocs: tool({
      description: "Search documentation",
      parameters: z.object({ query: z.string() }),
      execute: async ({ query }) => searchIndex(query),
    }),
    runTest: tool({
      description: "Run a test file",
      parameters: z.object({ path: z.string() }),
      execute: async ({ path }) => runTests(path),
    }),
  },
  maxSteps: 5,  // Multi-step agent loop
});
```

### generateText (Non-streaming)
```typescript
import { generateText } from "ai";

const { text, toolCalls, toolResults } = await generateText({
  model: openai("gpt-4o"),
  prompt: "Analyze this code for bugs",
  tools: { ... },
  maxSteps: 3,
});
```

### useChat (React Hook)
```tsx
"use client";
import { useChat } from "@ai-sdk/react";

export function Chat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: "/api/chat",
    maxSteps: 5,
  });

  return (
    <div>
      {messages.map(m => (
        <div key={m.id}>
          <strong>{m.role}:</strong> {m.content}
          {m.toolInvocations?.map(t => (
            <pre key={t.toolCallId}>{JSON.stringify(t.result)}</pre>
          ))}
        </div>
      ))}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
        <button disabled={isLoading}>Send</button>
      </form>
    </div>
  );
}
```

### API Route (Next.js)
```typescript
// app/api/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";

export async function POST(req: Request) {
  const { messages } = await req.json();
  const result = streamText({
    model: openai("gpt-4o"),
    messages,
    tools: { ... },
    maxSteps: 5,
  });
  return result.toDataStreamResponse();
}
```

## Human-in-the-Loop

```typescript
const searchTool = tool({
  description: "Search and delete files",
  parameters: z.object({ path: z.string() }),
  needsApproval: true,  // Pauses for user approval
  execute: async ({ path }) => deleteFile(path),
});
```

## Multi-Provider Support

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

// Same API, different providers
streamText({ model: anthropic("claude-sonnet-4-5"), ... });
streamText({ model: google("gemini-2.0-flash"), ... });
```

## Sources

- [Vercel AI SDK Docs](https://sdk.vercel.ai/)
- [GitHub](https://github.com/vercel/ai)

