Source: https://aiprimetech.io/ja/learn/use-claude-api-with-vercel-ai-sdk/ [ホーム](/ja/) / [学習](/ja/learn/) / Vercel AI SDKでClaudeを使う:createAnthropicのbaseURL、ストリーミング、問題の解決 # Vercel AI SDKでClaudeを使用:createAnthropicのbaseURL、ストリーミング、問題の修正 AI SDKのAnthropicプロバイダーである @ai-sdk/anthropicは、Messages APIと通信します。その baseURL のデフォルト値は https://api.anthropic.com/v1なので、ゲートウェイを使う場合は、 /v1を含むプレフィックス全体を置き換えます。OpenCode、Kilo Code、多くのエージェントフレームワークは内部で同じプロバイダーを使用しているため、それらの設定にも同じルールが適用されます。 ## インストール ターミナルコピー ``` npm i ai @ai-sdk/anthropic ``` ## プロバイダーを作成する lib/anthropic.tsコピー ``` import { createAnthropic } from '@ai-sdk/anthropic'; export const anthropic = createAnthropic({ baseURL: 'https://aiprimetech.io/v1', // replaces https://api.anthropic.com/v1 apiKey: process.env.AIPRIME_API_KEY, // sent as x-api-key; defaults to ANTHROPIC_API_KEY }); ``` ベースURLを指定する環境変数は、 @ai-sdk/anthropicにはありません。 baseURL をコード内で渡してください。通常の anthropic のインポートは、常にAnthropic自身のエンドポイントに接続します。 ## generateTextとstreamText generate.tsコピー ``` import { generateText } from 'ai'; import { anthropic } from './lib/anthropic'; const { text, usage } = await generateText({ model: anthropic('claude-sonnet-5'), prompt: 'Explain idempotency keys in two sentences.', }); console.log(text, usage); ``` stream.tsコピー ``` import { streamText } from 'ai'; import { anthropic } from './lib/anthropic'; const result = streamText({ model: anthropic('claude-sonnet-5'), prompt: 'Write a haiku about load balancers.', }); for await (const chunk of result.textStream) process.stdout.write(chunk); ``` どちらの呼び出しでも、 maxRetries (デフォルトは2)を指定でき、429や5xxなどの一時的な障害に対応できます。 ## 思考とプロンプトキャッシュ Anthropic固有のオプションは providerOptions.anthropicに指定します。現在のモデルは適応型思考を使用します。 thinking.tsコピー ``` const { text } = await generateText({ model: anthropic('claude-sonnet-5'), providerOptions: { anthropic: { thinking: { type: 'adaptive' } } }, prompt: 'Plan a zero-downtime database migration in five steps.', }); ``` 長く、内容が安定したシステムプロンプトをキャッシュするには、 cacheControlを使用します。キャッシュの読み取りは、その後 usage.inputTokenDetails.cacheReadTokensに表示されます。モデルの最小長に満たないプロンプトはキャッシュされません。 cache.tsコピー ``` const result = await generateText({ model: anthropic('claude-sonnet-5'), messages: [ { role: 'system', content: longStableInstructions, providerOptions: { anthropic: { cacheControl: { type: 'ephemeral' } } }, }, { role: 'user', content: question }, ], }); ``` ## OpenAI互換の代替手段 ゲートウェイはOpenAI Chat Completions形式にも対応しているため、 @ai-sdk/openai-compatible は同じ /v1 ベースURLで動作します。同じキーでGPTモデルを使う場合はこの経路を使用します。Claudeでは、上記のネイティブプロバイダーを使うことで、思考とキャッシュ制御を利用できます。 lib/gateway.tsコピー ``` import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; const gateway = createOpenAICompatible({ name: 'aiprime', baseURL: 'https://aiprimetech.io/v1', apiKey: process.env.AIPRIME_API_KEY, }); // gateway('claude-sonnet-5') or a GPT model ID ``` ## トラブルシューティング | 表示される内容 | 原因 | 対処方法 | JSON の解析エラー、次で始まるレスポンス < | baseURL なしで /v1:リクエストがウェブサイトに届いています | baseURL: 'https://aiprimetech.io/v1' | 404 page not found | /v1 重複 | 必ず単一の /v1 | Invalid API key | process.env の値がそのランタイムで未定義です(サーバーレス関数とエッジ関数には独自の環境があります)。そのため、SDKはフォールバック先として ANTHROPIC_API_KEY | デプロイ先の環境に変数を設定してください | モデルがサポートされていない | モデルIDが古いか、スペルが間違っています | 以下の表にあるIDを使用してください | Concurrency limit exceeded for user | Promise.all を大きな配列に対して実行 | 並列呼び出しを制限してください。例えば、 p-limit | No available accounts / 503 | 上流サービスの一時的な処理能力不足 | 自動で再試行されます。次の値を増やしてください: maxRetries | モデルID | 用途 | claude-sonnet-5 | コーディングとエージェント作業の標準モデル:高速、高性能、中程度の価格。 | claude-opus-5-5 | より難しい推論、大規模なリファクタリング、長時間のエージェント実行。 | claude-fable-5-1 | 最も高性能で最も高価。最も難しいタスクに絞って使いましょう。 | claude-haiku-4-5 | 低価格で高速:タイトル、要約、分類、小さな処理。 ## よくある質問 @ai-sdk/anthropicのデフォルトのbaseURLは何ですか? https://api.anthropic.com/v1. ゲートウェイではプレフィックス全体を置き換えるため、末尾は/v1にする必要があります。例:https://aiprimetech.io/v1. AI SDKのAnthropicベースURLを指定する環境変数はありますか? いいえ。プロバイダーは環境からANTHROPIC_API_KEY(またはANTHROPIC_AUTH_TOKEN)を読み取りますが、ベースURLはcreateAnthropic({ baseURL })で設定します。 AI SDKからClaudeを呼び出すと解析エラーが発生するのはなぜですか? 通常はbaseURLに/v1が付いていないため、リクエストがウェブサイトに届き、JSONの代わりにHTMLが返されることが原因です。 Claudeには@ai-sdk/anthropicと@ai-sdk/openai-compatibleのどちらを使うべきですか? @ai-sdk/anthropicを使ってください。ネイティブのMessages APIで通信するため、思考とプロンプトキャッシュが動作します。GPTモデルにはOpenAI互換プロバイダーを使用してください。 数分でClaudeの利用を開始 APIキーを取得 — Anthropicアカウントもウェイトリストへの登録も不要です。[APIキーを取得](/register) AI Prime Techは独立したAPIゲートウェイです。Anthropicとの提携関係はなく、同社の承認を受けておらず、同社の再販業者でもありません。Claudeおよび関連するモデル名は、それぞれの所有者の商標です。 ## Claude APIのその他のガイド [Claude APIキーの取得方法](/ja/learn/how-to-get-a-claude-api-key/)[Claude API の料金を解説](/ja/learn/claude-api-pricing-explained/)[Claude APIを最も安く利用する方法](/ja/learn/cheapest-way-to-use-claude-api/)[Claude APIでCursorを無制限に利用](/learn/use-claude-api-with-cursor/)[Claude Codeへの無制限アクセス](/ja/learn/use-claude-api-with-claude-code/)[Claude APIゲートウェイとAnthropicへの直接接続の比較](/ja/learn/claude-api-vs-anthropic-direct/)[OpenAI互換API経由でClaudeを使う](/ja/learn/openai-compatible-claude-api/)[Claude モデルを解説:Claude Opus、Sonnet、Haiku、Fable のモデル比較 — すべての Claude API モデル、Anthropic のモデル名と料金(2026)](/ja/learn/claude-api-models-compared/)[暗号資産でClaude APIの料金を支払う](/ja/learn/claude-api-with-crypto-payment/)[ウェイティングリストなしでClaude APIを利用](/ja/learn/claude-api-without-waitlist/)[Claudeのトークン使用量を削減する方法](/ja/learn/how-to-reduce-claude-token-usage/)[Claude APIでClineを無制限に利用](/ja/learn/use-claude-api-with-cline/)[Claude APIでRoo Codeを無制限に利用](/ja/learn/use-claude-api-with-roo-code/)[JetBrains IDEでClaudeを無制限に利用](/ja/learn/use-claude-api-with-jetbrains/)[Claude APIでAiderを無制限に使う](/ja/learn/use-claude-api-with-aider/)[VS CodeでClaudeを無制限に利用](/ja/learn/use-claude-api-with-vscode/)[Claude向けのOpenCode設定:opencode.jsonの設定、モデル、よくあるエラー](/ja/learn/use-claude-api-with-opencode/)[ClaudeでHermesエージェントを無制限に実行する](/ja/learn/run-hermes-agent-with-claude-api/)[ClaudeでOpenClawを無制限に実行](/ja/learn/run-openclaw-with-claude-api/)[Anthropic互換APIとは](/ja/learn/anthropic-compatible-api/)[Claude CodeのAPIキー — ANTHROPIC_API_KEY:取得方法、設定方法、代わりにclaude setup-tokenを使うべき場合](/ja/learn/claude-code-api-key-setup/)[Claude Code APIのコスト — 料金と削減方法](/ja/learn/claude-code-api-costs/)[Claude APIエラーを解説:サーバーエラー500、過負荷529、レート制限429、不正なリクエスト400 — それぞれの解決方法](/ja/learn/claude-api-errors-troubleshooting/)[Claude APIクレジットの購入方法 — Claudeクレジットがどの「Claudeアカウント購入」のオファーよりも優れている理由](/ja/learn/buy-claude-api-credits/)[Claudeの無料API:無料のClaude APIキーはあるのか、2026に実際に無料で使えるものは?](/ja/learn/claude-api-free-trial/)[Claude APIとClaude Pro/Maxサブスクリプション — どちらを選ぶべきか](/ja/learn/claude-api-vs-subscription/)[ClaudeでCodex CLIを実行する](/ja/learn/use-claude-api-with-codex/)[Claudeをn8nに接続する](/ja/learn/use-claude-api-with-n8n/)[LiteLLMからClaudeへのルーティング](/ja/learn/use-claude-api-with-litellm/)[Kilo CodeでClaudeを使う](/ja/learn/use-claude-api-with-kilo-code/)[GitHub CopilotにClaudeを導入する](/ja/learn/use-claude-api-with-github-copilot/)[ゲートウェイ経由でZedをClaudeに接続する](/ja/learn/use-claude-api-with-zed/)[Cherry StudioをClaudeに接続](/ja/learn/use-claude-api-with-cherry-studio/)[LangChainのChatAnthropic:インポート、base_url、ストリーミング、ツール、エラー](/ja/learn/use-claude-api-with-langchain/)[Anthropic Python SDK:インストール、base_urlの設定、ストリーミング、エラー処理](/ja/learn/anthropic-python-sdk/)[ClaudeでFactory Droidを実行](/ja/learn/use-claude-api-with-factory-droid/)[Claude Code APIの仕組みを解説](/ja/learn/claude-code-api/)[アプリケーションへのClaude APIの統合](/ja/learn/claude-api-integration/)[Claudeを無制限に利用する方法 — 「Claude Codeのクラック版」が存在しない理由](/ja/learn/get-unlimited-claude-usage/)