
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プロバイダーを作成する
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
});@ai-sdk/anthropicにはありません。 baseURL をコード内で渡してください。通常の anthropic のインポートは、常にAnthropic自身のエンドポイントに接続します。generateTextとstreamText
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);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に指定します。現在のモデルは適応型思考を使用します。
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に表示されます。モデルの最小長に満たないプロンプトはキャッシュされません。
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では、上記のネイティブプロバイダーを使うことで、思考とキャッシュ制御を利用できます。
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は何ですか?
AI SDKのAnthropicベースURLを指定する環境変数はありますか?
AI SDKからClaudeを呼び出すと解析エラーが発生するのはなぜですか?
Claudeには@ai-sdk/anthropicと@ai-sdk/openai-compatibleのどちらを使うべきですか?
APIキーを取得 — Anthropicアカウントもウェイトリストへの登録も不要です。
APIキーを取得AI Prime Techは独立したAPIゲートウェイです。Anthropicとの提携関係はなく、同社の承認を受けておらず、同社の再販業者でもありません。Claudeおよび関連するモデル名は、それぞれの所有者の商標です。