首页 / 学习 / 通过 OpenAI SDK 使用 Claude:兼容 OpenAI 的端点、流式传输、工具和限制
OpenAI SDK + Claude。两个形状互补的实体连接器将两个代码窗口连接起来,清晰地比喻 SDK 兼容性。

通过 OpenAI SDK 使用 Claude:OpenAI 兼容端点、流式传输、工具与限制

为 OpenAI SDK 编写的代码无需重写即可调用 Claude:将 base_url 指向网关的 /v1,并使用网关密钥和 Claude 模型 ID。该端点接受 Chat Completions 请求,以相同格式进行流式传输,并支持函数调用。部分 Claude 功能仅在 Anthropic 原生格式中提供;最后一节会介绍何时需要切换。

端点信息

项目 值
基础 URLhttps://aiprimetech.io/v1
聊天端点POST https://aiprimetech.io/v1/chat/completions
模型列表GET https://aiprimetech.io/v1/models
身份验证Authorization: Bearer <key>; 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 响应的流式传输.

函数调用

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 指南 以及 Vercel AI SDK 指南 介绍了这种方式。

故障排查

你会看到什么 原因 解决方法
JSON 解析错误,响应以 <base_url 不带 /v1https://aiprimetech.io/v1
404 page not found/v1 两次恰好一个 /v1
401 Invalid API key进程中的密钥缺失或不正确检查变量;每个密钥只属于一个网关
模型不受支持带日期的旧模型 ID,或拼写错误的模型 ID使用上表中的 ID
'none' is not supportedreasoning_effort="none"使用 low 或更高,或者省略该项
429 Concurrency limit exceeded for user正在处理的请求过多限制并发数;请参阅 速率限制与重试
长回答超时耗时较长的非流式请求使用流式传输,或延长客户端超时时间

常见问题

可以通过 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 密钥

AI Prime Tech 是独立的 API 网关,与 Anthropic 无关联,未获得其背书,也不是其经销商。Claude 及相关模型名称是其各自所有者的商标。

更多 Claude API 指南

如何获取 Claude API 密钥Claude API 定价详解使用 Claude API 最便宜的方式通过 Claude API 不限量使用 Cursor不限量访问 Claude CodeClaude API 网关与 Anthropic 直连对比Claude 模型详解:Claude Opus、Sonnet、Haiku 与 Fable 的模型对比——全部 Claude API 模型、Anthropic 模型名称与价格(2026)使用加密货币支付 Claude API 费用无需候补名单即可访问 Claude API如何减少 Claude 词元用量通过 Claude API 不限量使用 Cline通过 Claude API 不限量使用 Roo Code在 JetBrains IDE 中不限量使用 Claude通过网关在 Aider 中使用 Claude:ANTHROPIC_API_BASE、配置文件、架构师模式与问题修复在 VS Code 中不限量使用 ClaudeOpenCode 的 Claude 配置:opencode.json 设置、模型与常见错误通过网关在 Hermes Agent 中使用 Claude:自定义提供商配置、成本控制与问题修复使用 Claude 不限量运行 OpenClaw兼容 Anthropic 的 API 意味着什么Claude Code API 密钥 — ANTHROPIC_API_KEY:如何获取、配置,以及何时应改用 claude setup-tokenClaude Code API 成本 — 定价与降低成本的方法Claude API 错误详解:服务器错误 500、过载 529、速率限制 429、错误请求 400 — 以及各自的修复方法如何购买 Claude API 额度,以及为什么购买 Claude 额度比任何“购买 Claude 账户”的方案都更好Claude 免费 API:有免费的 Claude API 密钥吗?2026 年究竟有哪些内容免费?Claude API 与 Claude Pro/Max 订阅——如何选择通过自定义提供商使用 Codex CLI:config.toml、GPT 与 Claude 模型、IDE 扩展与问题修复将 Claude 连接到 n8n通过 LiteLLM 路由到 Claude在 Kilo Code 中使用 Claude通过网关在 GitHub Copilot Chat 中使用 Claude:自定义端点配置、智能体模式与问题修复通过网关将 Zed 连接到 Claude将 Cherry Studio 连接到 ClaudeLangChain 中的 ChatAnthropic:导入、base_url、流式传输、工具和错误Anthropic Python SDK:安装、设置 base_url、流式传输和错误处理通过 Vercel AI SDK 使用 Claude:createAnthropic baseURL、流式传输和问题修复使用 Claude 运行 Factory DroidClaude Code API 详解将 Claude API 集成到您的应用中如何不受限制地使用 Claude,以及为什么不存在“Claude Code 破解版本”

更多 Claude API 指南

兼容 Anthropic 的 API 意味着什么Anthropic Python SDK:安装、设置 base_url、流式传输和错误处理如何购买 Claude API 额度使用 Claude API 最便宜的方式Claude API 错误详解:服务器错误 500、过载 529、速率限制 429、错误请求 400 — 以及各自的修复方法Claude 免费 API:有免费的 Claude API 密钥吗?2026 年究竟有哪些内容免费?将 Claude API 集成到您的应用中Claude 模型详解:Claude Opus、Sonnet、Haiku 与 Fable 的模型对比——全部 Claude API 模型、Anthropic 模型名称与价格(2026)Claude API 定价详解Claude API 网关与 Anthropic 直连对比Claude API 与 Claude Pro/Max 订阅——如何选择使用加密货币支付 Claude API 费用无需候补名单即可访问 Claude APIClaude Code API 详解Claude Code API 成本 — 定价与降低成本的方法Claude Code API 密钥 — ANTHROPIC_API_KEY:如何获取、配置,以及何时应改用 claude setup-token如何不限量使用 Claude如何获取 Claude API 密钥如何减少 Claude 词元用量通过网关在 Hermes Agent 中使用 Claude:自定义提供商配置、成本控制与问题修复使用 Claude 不限量运行 OpenClaw不限量 Claude API 访问意味着什么如何获取不限量的 Claude API 密钥不限量使用 Claude Code——无词元限制不限量访问 Claude Opus 4.8不限量使用 Claude——套餐与定价通过网关在 Aider 中使用 Claude:ANTHROPIC_API_BASE、配置文件、架构师模式与问题修复将 Cherry Studio 连接到 Claude不限量访问 Claude Code通过 Claude API 不限量使用 Cline通过自定义提供商使用 Codex CLI:config.toml、GPT 与 Claude 模型、IDE 扩展与问题修复通过 Claude 无限访问运行 Cursor使用 Claude 运行 Factory Droid通过网关在 GitHub Copilot Chat 中使用 Claude:自定义端点配置、智能体模式与问题修复在 JetBrains IDE 中不限量使用 Claude在 Kilo Code 中使用 ClaudeLangChain 中的 ChatAnthropic:导入、base_url、流式传输、工具和错误通过 LiteLLM 路由到 Claude将 Claude 连接到 n8nOpenCode 的 Claude 配置:opencode.json 设置、模型与常见错误通过 Claude API 不限量使用 Roo Code通过 Vercel AI SDK 使用 Claude:createAnthropic baseURL、流式传输和问题修复在 VS Code 中不限量使用 Claude通过网关将 Zed 连接到 Claude