Source: https://aiprimetech.io/zh/learn/openai-compatible-claude-api/ [首页](/zh/) / [学习](/zh/learn/) / 通过 OpenAI SDK 使用 Claude:兼容 OpenAI 的端点、流式传输、工具和限制 # 通过 OpenAI SDK 使用 Claude:OpenAI 兼容端点、流式传输、工具与限制 为 OpenAI SDK 编写的代码无需重写即可调用 Claude:将 base_url 指向网关的 /v1,并使用网关密钥和 Claude 模型 ID。该端点接受 Chat Completions 请求,以相同格式进行流式传输,并支持函数调用。部分 Claude 功能仅在 Anthropic 原生格式中提供;最后一节会介绍何时需要切换。 ## 端点信息 | 项目 | 值 | 基础 URL | https://aiprimetech.io/v1 | 聊天端点 | POST https://aiprimetech.io/v1/chat/completions | 模型列表 | GET https://aiprimetech.io/v1/models | 身份验证 | Authorization: Bearer ; x-api-key 也可接受 | 模型 | Claude ID,例如 claude-sonnet-5;使用 Codex 分组密钥时可用的 GPT ID ## Python 终端复制 ``` pip install openai ``` app.py复制 ``` import os from openai import OpenAI client = OpenAI(base_url="https://aiprimetech.io/v1", api_key=os.environ["AIPRIME_API_KEY"]) resp = client.chat.completions.create( model="claude-sonnet-5", max_tokens=1024, messages=[ {"role": "system", "content": "You are a concise assistant."}, {"role": "user", "content": "Explain idempotency keys in two sentences."}, ], ) print(resp.choices[0].message.content) print(resp.usage) ``` base_url 必须以 /v1结尾:SDK 会将 /chat/completions 追加到其后。如果没有 /v1 请求会发送到网站并返回 HTML;如果 /v1 出现两次,则返回 404 page not found. ## Node.js app.mjs复制 ``` import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://aiprimetech.io/v1", apiKey: process.env.AIPRIME_API_KEY }); const resp = await client.chat.completions.create({ model: "claude-sonnet-5", max_tokens: 1024, messages: [{ role: "user", content: "Write a haiku about retries." }], }); console.log(resp.choices[0].message.content); ``` ## 流式传输 stream.py复制 ``` stream = client.chat.completions.create( model="claude-sonnet-5", max_tokens=2048, stream=True, stream_options={"include_usage": True}, messages=[{"role": "user", "content": "Outline a zero-downtime migration."}], ) for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) if chunk.usage: print("\n", chunk.usage) ``` 长回答请使用流式传输:非流式请求必须在客户端超时之前完成,而流式请求不受这一要求限制。 include_usage 会添加一个包含 token 计数的最终数据块。关于其重要性的更多说明,请参阅 [LLM 响应的流式传输](/zh/blog/streaming-api-guide/). ## 函数调用 tools.py复制 ``` tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Current weather for a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }] resp = client.chat.completions.create( model="claude-sonnet-5", max_tokens=512, tools=tools, messages=[{"role": "user", "content": "What is the weather in Paris?"}], ) call = resp.choices[0].message.tool_calls[0] print(call.function.name, call.function.arguments) # get_weather {"city": "Paris"} ``` 返回的响应包含 finish_reason: "tool_calls" 以及 OpenAI 风格的 tool_calls。运行该函数,追加一条 tool 消息,其中包含结果以及匹配的 tool_call_id,然后再次调用,操作方式与 OpenAI 模型完全相同。 ## 推理与思考 reasoning_effort 映射到 Claude 的 effort 设置,并接受 low, medium, high, xhigh 或 max。值 none 会被拒绝。当模型进行思考时,其推理内容会通过单独的 reasoning_content 字段返回,该字段位于 content旁边,因此读取 message.content 的代码无需修改即可继续运行。 reasoning.py复制 ``` resp = client.chat.completions.create( model="claude-opus-5-5", max_tokens=4096, reasoning_effort="high", messages=[{"role": "user", "content": "Find the bug in this function: ..."}], ) ``` ## 模型 | 模型 ID | 适用场景 | claude-sonnet-5 | 编程和智能体任务的默认选择:速度快、能力强、价格适中。 | claude-opus-5-5 | 更复杂的推理、大规模重构、长时间运行的智能体任务。 | claude-fable-5-1 | 能力最强,价格也最高;留给最困难的任务使用。 | claude-haiku-4-5 | 价格低、速度快:标题、摘要、分类和小步骤。 请严格使用这些 ID。带日期的旧 ID,例如 claude-3-5-sonnet-20241022 ,仍会出现在某些模型列表中,但已不再提供服务,调用时会返回模型不受支持的错误。 ## 何时应改用原生 Anthropic API OpenAI 格式支持聊天、流式传输、工具和图像。如果需要显式提示词缓存断点、在对话轮次之间回传的思考块、PDF 文档或引用,请通过同一网关使用 Anthropic 自有的 Messages 格式。 [Anthropic Python SDK 指南](/zh/learn/anthropic-python-sdk/) 以及 [Vercel AI SDK 指南](/zh/learn/use-claude-api-with-vercel-ai-sdk/) 介绍了这种方式。 ## 故障排查 | 你会看到什么 | 原因 | 解决方法 | JSON 解析错误,响应以 < | base_url 不带 /v1 | https://aiprimetech.io/v1 | 404 page not found | /v1 两次 | 恰好一个 /v1 | 401 Invalid API key | 进程中的密钥缺失或不正确 | 检查变量;每个密钥只属于一个网关 | 模型不受支持 | 带日期的旧模型 ID,或拼写错误的模型 ID | 使用上表中的 ID | 'none' is not supported | reasoning_effort="none" | 使用 low 或更高,或者省略该项 | 429 Concurrency limit exceeded for user | 正在处理的请求过多 | 限制并发数;请参阅 [速率限制与重试](/zh/blog/rate-limits-and-retries/) | 长回答超时 | 耗时较长的非流式请求 | 使用流式传输,或延长客户端超时时间 ## 常见问题 可以通过 OpenAI SDK 使用 Claude 吗? 可以,通过兼容 OpenAI 的端点即可:将 base_url 设置为网关的 /v1,使用网关 API 密钥和 Claude 模型 ID,例如 claude-sonnet-5.。流式传输和函数调用均可使用 OpenAI 格式。 在 OpenAI SDK 中使用 Claude 时,应使用哪个基础 URL? 使用以 /v1 结尾的网关 URL,例如 https://aiprimetech.io/v1.。SDK 会追加 /chat/completions。 通过 OpenAI 格式使用 Claude 时,函数调用是否可用? 可以。按 OpenAI 格式传入 tools;Claude 会返回 tool_calls,并将 finish_reason 设为 tool_calls,随后按通常方式使用 tool 消息回复即可。 何时应改用原生 Anthropic API? 当你需要显式提示词缓存、跨轮次思考块、PDF 输入或引用时。这些功能属于 Anthropic 的 Messages 格式。 几分钟内开始使用 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/)[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/)[通过网关在 Aider 中使用 Claude:ANTHROPIC_API_BASE、配置文件、架构师模式与问题修复](/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/)[通过网关在 Hermes Agent 中使用 Claude:自定义提供商配置、成本控制与问题修复](/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/)[通过自定义提供商使用 Codex CLI:config.toml、GPT 与 Claude 模型、IDE 扩展与问题修复](/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/)[通过网关在 GitHub Copilot Chat 中使用 Claude:自定义端点配置、智能体模式与问题修复](/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/)[通过 Vercel AI SDK 使用 Claude:createAnthropic baseURL、流式传输和问题修复](/zh/learn/use-claude-api-with-vercel-ai-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/)