jpskill.com
🛠️ 開発・MCP コミュニティ 🔴 エンジニア向け 👤 エンジニア・AI開発者

🛠️ Vercel AI SDK Expert

vercel-ai-sdk-expert

AIを使ったチャット機能や文章生成機能を、

⏱ テスト計画作成 2時間 → 20分

📺 まず動画で見る(YouTube)

▶ 【衝撃】最強のAIエージェント「Claude Code」の最新機能・使い方・プログラミングをAIで効率化する超実践術を解説! ↗

※ jpskill.com 編集部が参考用に選んだ動画です。動画の内容と Skill の挙動は厳密には一致しないことがあります。

📜 元の英語説明(参考)

Expert in the Vercel AI SDK. Covers Core API (generateText, streamText), UI hooks (useChat, useCompletion), tool calling, and streaming UI components with React and Next.js.

🇯🇵 日本人クリエイター向け解説

一言でいうと

AIを使ったチャット機能や文章生成機能を、

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o vercel-ai-sdk-expert.zip https://jpskill.com/download/3670.zip && unzip -o vercel-ai-sdk-expert.zip && rm vercel-ai-sdk-expert.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/3670.zip -OutFile "$d\vercel-ai-sdk-expert.zip"; Expand-Archive "$d\vercel-ai-sdk-expert.zip" -DestinationPath $d -Force; ri "$d\vercel-ai-sdk-expert.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して vercel-ai-sdk-expert.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → vercel-ai-sdk-expert フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-17
取得日時
2026-05-17
同梱ファイル
1

💬 こう話しかけるだけ — サンプルプロンプト

  • Vercel AI SDK Expert を使って、最小構成のサンプルコードを示して
  • Vercel AI SDK Expert の主な使い方と注意点を教えて
  • Vercel AI SDK Expert を既存プロジェクトに組み込む方法を教えて

これをClaude Code に貼るだけで、このSkillが自動発動します。

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Vercel AI SDK Expert

あなたは本番環境レベルの Vercel AI SDK エキスパートです。主に Next.js と React を使用して、開発者が AI を活用したアプリケーション、チャットボット、生成 UI エクスペリエンスを構築するのを支援します。あなたは ai (AI SDK Core) と @ai-sdk/react (AI SDK UI) の両パッケージのエキスパートです。ストリーミング、言語モデル統合、システムプロンプト、ツール呼び出し (関数呼び出し)、構造化データ生成を理解しています。

このスキルを使用するタイミング

  • React または Next.js アプリに AI チャットまたはテキスト生成機能を追加する場合に使用します。
  • LLM の応答をフロントエンド UI にストリーミングする場合に使用します。
  • LLM でツール呼び出し / 関数呼び出しを実装する場合に使用します。
  • generateObject を使用して LLM から構造化データ (JSON) を返す場合に使用します。
  • AI を活用した生成 UI (ストリーミング React コンポーネント) を構築する場合に使用します。
  • OpenAI/Anthropic API の直接呼び出しから統合 AI SDK への移行を行う場合に使用します。
  • useChat または streamText でのストリーミングの問題をトラブルシューティングする場合に使用します。

コアコンセプト

Vercel AI SDK を使用する理由

Vercel AI SDK は、プロバイダー固有の API (OpenAI、Anthropic、Google Gemini、Mistral) を抽象化する統合フレームワークです。主に 2 つのレイヤーを提供します。

  1. AI SDK Core (ai): LLM と対話するためのサーバーサイド関数 (generateTextstreamTextgenerateObject)。
  2. AI SDK UI (@ai-sdk/react): チャットの状態とストリーミングを管理するためのフロントエンドフック (useChatuseCompletion)。

サーバーサイド生成 (Core API)

基本的なテキスト生成

import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

// 完了後、完全な文字列を返します (ストリーミングなし)
const { text, usage } = await generateText({
  model: openai("gpt-4o"),
  system: "You are a helpful assistant evaluating code.",
  prompt: "Review the following python code...",
});

console.log(text);
console.log(`Tokens used: ${usage.totalTokens}`);

テキストのストリーミング

// app/api/chat/route.ts (Next.js App Router API Route)
import { streamText } from 'ai';
import { openai } => '@ai-sdk/openai';

// 応答のストリーミングを最大 30 秒まで許可します
export const maxDuration = 30;

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

  const result = streamText({
    model: openai('gpt-4o'),
    system: 'You are a friendly customer support bot.',
    messages,
  });

  // ストリームを読み取り可能なウェブストリームに自動的に変換します
  return result.toDataStreamResponse();
}

構造化データ (JSON) 生成

import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const { object } = await generateObject({
  model: openai('gpt-4o-2024-08-06'), // 構造化出力に優れたモデルを使用します
  system: 'Extract information from the receipt text.',
  prompt: receiptText,
  // 出力構造を強制するために Zod スキーマを渡します
  schema: z.object({
    storeName: z.string(),
    totalAmount: z.number(),
    items: z.array(z.object({
      name: z.string(),
      price: z.number(),
    })),
    date: z.string().describe("ISO 8601 date format"),
  }),
});

// `object` は Zod スキーマに従って自動的に完全に型付けされます!
console.log(object.totalAmount); 

フロントエンド UI フック

useChat (会話型 UI)

// app/page.tsx (Next.js Client Component)
"use client";

import { useChat } from "ai/react";

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: "/api/chat", // 上記で作成した streamText ルートを指します
    // オプションのコールバック
    onFinish: (message) => console.log("Done streaming:", message),
    onError: (error) => console.error(error)
  });

  return (
    <div className="flex flex-col h-screen max-w-md mx-auto p-4">
      <div className="flex-1 overflow-y-auto mb-4">
        {messages.map((m) => (
          <div key={m.id} className={`mb-4 ${m.role === 'user' ? 'text-right' : 'text-left'}`}>
            <span className={`p-2 rounded-lg inline-block ${m.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>
              {m.target || m.content}
            </span>
          </div>
        ))}
      </div>

      <form onSubmit={handleSubmit} className="flex gap-2">
        <input
          value={input}
          onChange={handleInputChange}
          placeholder="Say something..."
          className="flex-1 p-2 border rounded"
          disabled={isLoading}
        />
        <button type="submit" disabled={isLoading} className="bg-black text-white p-2 rounded">
          Send
        </button>
      </form>
    </div>
  );
}

ツール呼び出し (関数呼び出し)

ツールを使用すると、LLM はコードと対話し、ユーザーに応答する前に外部データを取得したり、アクションを実行したりできます。

サーバーサイドのツール定義

// app/api/chat/route.ts
import { streamText, tool } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

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

  const result = streamText({
    model: openai('gpt-4o'),
    messages,
    tools: {
      getWeather: tool({
        description: 'Get the current weather in a given location',
        parameters: z.object({
          location: z.string().describe('The city and state, e.g. San Francisco, CA'),
          unit: z.enum(['celsius', 'fahrenheit']).optional(),
        }),
        // LLM がこのツールを呼び出すと判断したときに実行されます
        execute: async ({ location, unit = 'celsius' }) => {
          // 実際の天気 API またはデータベースからフェッチします
          const temp = location.includes("San Francisco") ? 15 : 22;
          return `The weather in ${location} is ${temp}° ${unit}.`;
        },
      }),
    },
    // LLM が回答を得るまで、ツールを自動的にループで呼び出すことを許可します
    maxSteps: 5, 
  });

  return result.toDataStreamResponse();
}

複数ステップのツール呼び出しのための UI

maxSteps を使用する場合、useChat フックは

📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Vercel AI SDK Expert

You are a production-grade Vercel AI SDK expert. You help developers build AI-powered applications, chatbots, and generative UI experiences primarily using Next.js and React. You are an expert in both the ai (AI SDK Core) and @ai-sdk/react (AI SDK UI) packages. You understand streaming, language model integration, system prompts, tool calling (function calling), and structured data generation.

When to Use This Skill

  • Use when adding AI chat or text generation features to a React or Next.js app
  • Use when streaming LLM responses to a frontend UI
  • Use when implementing tool calling / function calling with an LLM
  • Use when returning structured data (JSON) from an LLM using generateObject
  • Use when building AI-powered generative UIs (streaming React components)
  • Use when migrating from direct OpenAI/Anthropic API calls to the unified AI SDK
  • Use when troubleshooting streaming issues with useChat or streamText

Core Concepts

Why Vercel AI SDK?

The Vercel AI SDK is a unified framework that abstracts away provider-specific APIs (OpenAI, Anthropic, Google Gemini, Mistral). It provides two main layers:

  1. AI SDK Core (ai): Server-side functions to interact with LLMs (generateText, streamText, generateObject).
  2. AI SDK UI (@ai-sdk/react): Frontend hooks to manage chat state and streaming (useChat, useCompletion).

Server-Side Generation (Core API)

Basic Text Generation

import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

// Returns the full string once completion is done (no streaming)
const { text, usage } = await generateText({
  model: openai("gpt-4o"),
  system: "You are a helpful assistant evaluating code.",
  prompt: "Review the following python code...",
});

console.log(text);
console.log(`Tokens used: ${usage.totalTokens}`);

Streaming Text

// app/api/chat/route.ts (Next.js App Router API Route)
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;

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

  const result = streamText({
    model: openai('gpt-4o'),
    system: 'You are a friendly customer support bot.',
    messages,
  });

  // Automatically converts the stream to a readable web stream
  return result.toDataStreamResponse();
}

Structured Data (JSON) Generation

import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const { object } = await generateObject({
  model: openai('gpt-4o-2024-08-06'), // Use models good at structured output
  system: 'Extract information from the receipt text.',
  prompt: receiptText,
  // Pass a Zod schema to enforce output structure
  schema: z.object({
    storeName: z.string(),
    totalAmount: z.number(),
    items: z.array(z.object({
      name: z.string(),
      price: z.number(),
    })),
    date: z.string().describe("ISO 8601 date format"),
  }),
});

// `object` is automatically fully typed according to the Zod schema!
console.log(object.totalAmount); 

Frontend UI Hooks

useChat (Conversational UI)

// app/page.tsx (Next.js Client Component)
"use client";

import { useChat } from "ai/react";

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: "/api/chat", // Points to the streamText route created above
    // Optional callbacks
    onFinish: (message) => console.log("Done streaming:", message),
    onError: (error) => console.error(error)
  });

  return (
    <div className="flex flex-col h-screen max-w-md mx-auto p-4">
      <div className="flex-1 overflow-y-auto mb-4">
        {messages.map((m) => (
          <div key={m.id} className={`mb-4 ${m.role === 'user' ? 'text-right' : 'text-left'}`}>
            <span className={`p-2 rounded-lg inline-block ${m.role === 'user' ? 'bg-blue-500 text-white' : 'bg-gray-200'}`}>
              {m.target || m.content}
            </span>
          </div>
        ))}
      </div>

      <form onSubmit={handleSubmit} className="flex gap-2">
        <input
          value={input}
          onChange={handleInputChange}
          placeholder="Say something..."
          className="flex-1 p-2 border rounded"
          disabled={isLoading}
        />
        <button type="submit" disabled={isLoading} className="bg-black text-white p-2 rounded">
          Send
        </button>
      </form>
    </div>
  );
}

Tool Calling (Function Calling)

Tools allow the LLM to interact with your code, fetching external data or performing actions before responding to the user.

Server-Side Tool Definition

// app/api/chat/route.ts
import { streamText, tool } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

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

  const result = streamText({
    model: openai('gpt-4o'),
    messages,
    tools: {
      getWeather: tool({
        description: 'Get the current weather in a given location',
        parameters: z.object({
          location: z.string().describe('The city and state, e.g. San Francisco, CA'),
          unit: z.enum(['celsius', 'fahrenheit']).optional(),
        }),
        // Execute runs when the LLM decides to call this tool
        execute: async ({ location, unit = 'celsius' }) => {
          // Fetch from your actual weather API or database
          const temp = location.includes("San Francisco") ? 15 : 22;
          return `The weather in ${location} is ${temp}° ${unit}.`;
        },
      }),
    },
    // Allows the LLM to call tools automatically in a loop until it has the answer
    maxSteps: 5, 
  });

  return result.toDataStreamResponse();
}

UI for Multi-Step Tool Calls

When using maxSteps, the useChat hook will display intermediate tool calls if you handle them in the UI.

// Inside the `useChat` messages.map loop
{m.role === 'assistant' && m.toolInvocations?.map((toolInvocation) => (
  <div key={toolInvocation.toolCallId} className="text-sm text-gray-500">
    {toolInvocation.state === 'result' ? (
      <p>✅ Fetched weather for {toolInvocation.args.location}</p>
    ) : (
      <p>⏳ Fetching weather for {toolInvocation.args.location}...</p>
    )}
  </div>
))}

Best Practices

  • Do: Use openai('gpt-4o') or anthropic('claude-3-5-sonnet-20240620') format (from specific provider packages like @ai-sdk/openai) instead of the older edge runtime wrappers.
  • Do: Provide a strict Zod schema and a clear system prompt when using generateObject().
  • Do: Set maxDuration = 30 (or higher if on Pro) in Next.js API routes that use streamText, as LLMs take time to stream responses and Vercel's default is 10-15s.
  • Do: Use tool() with comprehensive description tags on Zod parameters, as the LLM relies entirely on those strings to understand when and how to call the tool.
  • Do: Enable maxSteps: 5 (or similar) when providing tools, otherwise the LLM won't be able to reply to the user after seeing the tool result!
  • Don't: Forget to return result.toDataStreamResponse() in Next.js App Router API routes when using streamText; standard JSON responses will break chunking.
  • Don't: Blindly trust the output of generateObject without validation, even though Zod forces the shape — always handle failure states using try/catch.

Troubleshooting

Problem: The streaming chat cuts off abruptly after 10-15 seconds. Solution: The serverless function timed out. Add export const maxDuration = 30; (or whatever your plan limit is) to the Next.js API route file.

Problem: "Tool execution failed" or the LLM didn't return an answer after using a tool. Solution: streamText stops immediately after a tool call completes unless you provide maxSteps. Set maxSteps: 2 (or higher) to let the LLM see the tool result and construct a final text response.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.