Source: https://aiprimetech.io/zh/learn/use-claude-api-with-vercel-ai-sdk/ [首页](/zh/) / [学习](/zh/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,导致请求访问了网站,返回 HTML 而不是 JSON。 使用 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 密钥](/zh/learn/how-to-get-a-claude-api-key/)[Claude API 定价详解](/zh/learn/claude-api-pricing-explained/)[使用 Claude API 最便宜的方式](/zh/learn/cheapest-way-to-use-claude-api/)[通过 Claude API 不限量使用 Cursor](/learn/use-claude-api-with-cursor/)[不限量访问 Claude Code](/zh/learn/use-claude-api-with-claude-code/)[Claude API 网关与 Anthropic 直连对比](/zh/learn/claude-api-vs-anthropic-direct/)[通过兼容 OpenAI 的 API 使用 Claude](/zh/learn/openai-compatible-claude-api/)[Claude 模型详解:Claude Opus、Sonnet、Haiku 与 Fable 的模型对比——全部 Claude API 模型、Anthropic 模型名称与价格(2026)](/zh/learn/claude-api-models-compared/)[使用加密货币支付 Claude API 费用](/zh/learn/claude-api-with-crypto-payment/)[无需候补名单即可访问 Claude API](/zh/learn/claude-api-without-waitlist/)[如何减少 Claude 词元用量](/zh/learn/how-to-reduce-claude-token-usage/)[通过 Claude API 不限量使用 Cline](/zh/learn/use-claude-api-with-cline/)[通过 Claude API 不限量使用 Roo Code](/zh/learn/use-claude-api-with-roo-code/)[在 JetBrains IDE 中不限量使用 Claude](/zh/learn/use-claude-api-with-jetbrains/)[通过 Claude API 无限制使用 Aider](/zh/learn/use-claude-api-with-aider/)[在 VS Code 中不限量使用 Claude](/zh/learn/use-claude-api-with-vscode/)[OpenCode 的 Claude 配置:opencode.json 设置、模型与常见错误](/zh/learn/use-claude-api-with-opencode/)[在 Claude 上运行不受限制的 Hermes 智能体](/zh/learn/run-hermes-agent-with-claude-api/)[使用 Claude 不限量运行 OpenClaw](/zh/learn/run-openclaw-with-claude-api/)[兼容 Anthropic 的 API 意味着什么](/zh/learn/anthropic-compatible-api/)[Claude Code API 密钥 — ANTHROPIC_API_KEY:如何获取、配置,以及何时应改用 claude setup-token](/zh/learn/claude-code-api-key-setup/)[Claude Code API 成本 — 定价与降低成本的方法](/zh/learn/claude-code-api-costs/)[Claude API 错误详解:服务器错误 500、过载 529、速率限制 429、错误请求 400 — 以及各自的修复方法](/zh/learn/claude-api-errors-troubleshooting/)[如何购买 Claude API 额度,以及为什么购买 Claude 额度比任何“购买 Claude 账户”的方案都更好](/zh/learn/buy-claude-api-credits/)[Claude 免费 API:有免费的 Claude API 密钥吗?2026 年究竟有哪些内容免费?](/zh/learn/claude-api-free-trial/)[Claude API 与 Claude Pro/Max 订阅——如何选择](/zh/learn/claude-api-vs-subscription/)[使用 Claude 运行 Codex CLI](/zh/learn/use-claude-api-with-codex/)[将 Claude 连接到 n8n](/zh/learn/use-claude-api-with-n8n/)[通过 LiteLLM 路由到 Claude](/zh/learn/use-claude-api-with-litellm/)[在 Kilo Code 中使用 Claude](/zh/learn/use-claude-api-with-kilo-code/)[将 Claude 接入 GitHub Copilot](/zh/learn/use-claude-api-with-github-copilot/)[通过网关将 Zed 连接到 Claude](/zh/learn/use-claude-api-with-zed/)[将 Cherry Studio 连接到 Claude](/zh/learn/use-claude-api-with-cherry-studio/)[LangChain 中的 ChatAnthropic:导入、base_url、流式传输、工具和错误](/zh/learn/use-claude-api-with-langchain/)[Anthropic Python SDK:安装、设置 base_url、流式传输和错误处理](/zh/learn/anthropic-python-sdk/)[使用 Claude 运行 Factory Droid](/zh/learn/use-claude-api-with-factory-droid/)[Claude Code API 详解](/zh/learn/claude-code-api/)[将 Claude API 集成到您的应用中](/zh/learn/claude-api-integration/)[如何不受限制地使用 Claude,以及为什么不存在“Claude Code 破解版本”](/zh/learn/get-unlimited-claude-usage/)