C
Claude API 指南

TokenHub 上的 Claude —— OpenAI-Compatible Anthropic Models

通过 TokenHub 统一的 /chat/completions 端点调用 Anthropic Claude model。请求和响应格式完全 OpenAI-compatible——无需迁移 SDK。支持流式输出、tool 调用(function calling)和视觉(图片输入)。按 model 阶梯进行按 token 计费。

OpenAI-CompatibleStreamingTool UseVisionClaude 3 → 4.7Token-based

1获取您的 API Key

  1. 访问 tokenhub.store 并注册账号(支持 GitHub / Google 登录)
  2. 进入 Dashboard → API Keys,点击“Create New Key”
  3. 进入 Dashboard → Billing 充值 Credits(1 Credit = $1 USD)
  4. 复制 API Key(格式: th-xxxxxxxxxxxx...)
⚠️ API Key 仅在创建时显示一次。请妥善保存;如果丢失,请重新创建。

2API 概览

Base URL

https://tokenhub.store/api/v1

身份验证

在 Authorization 请求头中传入 API Key:

Header
Authorization: Bearer th-your-api-key

接口地址(OpenAI-compatible)

POST
/chat/completions

聊天补全。与 OpenAI /v1/chat/completions 的 schema 完全一致,包含流式输出、工具使用、视觉和 JSON 模式。

你可以继续使用官方 openai SDK——只需将 base_url 指向 TokenHub,并使用你的 TokenHub API Key 即可,无需修改其他代码。

3模型与价格

价格按每 100 万 tokens 计费(USD)。仅成功的调用才会计费。支持标准 ID 和 anthropic/* 别名。

层级模型 ID输入输出备注
Opus 4.7anthropic/claude-opus-4-7$5.00$25.00最新,推理与编码质量最高。
Opus 4.6anthropic/claude-opus-4-6$5.00$25.00最新,推理与编码质量最高。
Sonnet 4.6anthropic/claude-sonnet-4-6$3.00$15.00旗舰级平衡方案,兼顾质量与成本(推荐默认)。
Haiku 4.5anthropic/claude-haiku-4-5$1.00$5.00速度最快、价格最低的第 4 代;非常适合高 QPS 和分类任务。
Sonnet 4.5anthropic/claude-sonnet-4-5$3.00$15.00上一代通用主力模型。
Opus 4.5anthropic/claude-opus-4-5$5.00$25.00最新,推理与编码质量最高。
Sonnet 4anthropic/claude-4-sonnet$3.00$15.00上一代通用主力模型。
Opus 4anthropic/claude-4-opus$15.00$75.00最新,推理与编码质量最高。
3.5 Sonnetanthropic/claude-3-5-sonnet-latest$3.00$15.00稳定、经过生产验证;兼容性广泛。
3.5 Haikuanthropic/claude-3-5-haiku-latest$0.80$4.00稳定、经过生产验证;兼容性广泛。

4请求参数

参数类型必填默认值描述
modelstring必填Claude model ID。示例:"anthropic/claude-sonnet-4-6"。支持 anthropic/* 前缀形式和不带前缀的简写名称。
messagesarray必填聊天历史。每一项为 { role, content }。role ∈ system | user | assistant。content 可以是字符串或部件数组(用于视觉 / 工具结果)。
max_tokensinteger可选1024最大输出 tokens(Claude 需要此参数)。通常为 1024–4096。
temperaturenumber可选1.0采样 temperature,0.0–1.0。越低越确定性。
top_pnumber可选1.0核采样。使用 temperature 或 top_p,二选一。
streamboolean可选false如果为 true,将返回 Server-Sent Events (SSE) 增量。
stopstring[]可选最多 4 个停止序列。
toolsarray可选用于工具使用(function calling)的工具 / function 定义列表。
tool_choicestring|object可选auto控制工具选择:auto | none | required | { type:'function', function:{ name } }。
response_formatobject可选JSON 模式:{ "type": "json_object" } 会强制 model 返回有效 JSON。
userstring可选可选的终端用户 ID,用于你自己的追踪。

5curl 示例

bash
curl https://tokenhub.store/api/v1/chat/completions \
  -H "Authorization: Bearer th-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4-6",
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Explain CAP theorem in 3 bullets."}
    ],
    "max_tokens": 512,
    "temperature": 0.3
  }'

6Python 示例

python
from openai import OpenAI

client = OpenAI(
    api_key="th-your-api-key",
    base_url="https://tokenhub.store/api/v1",
)

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4-6",
    max_tokens=512,
    temperature=0.3,
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Explain CAP theorem in 3 bullets."},
    ],
)

print(resp.choices[0].message.content)
print("usage:", resp.usage)

7JavaScript / Node.js 示例

javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "th-your-api-key",
  baseURL: "https://tokenhub.store/api/v1",
});

const resp = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-4-6",
  max_tokens: 512,
  temperature: 0.3,
  messages: [
    { role: "system", content: "You are a concise assistant." },
    { role: "user", content: "Explain CAP theorem in 3 bullets." },
  ],
});

console.log(resp.choices[0].message.content);
console.log("usage:", resp.usage);

8使用建议

  • 始终设置 max_tokens — Claude 从语义上将其视为必需项。若不设置,长文本生成可能会比预期更早截断。
  • 将指令放在单个 system message 中;保持 user 回合聚焦。Claude 会强烈遵循 system 提示。
  • 对于结构化抽取,可将类似 'Return only JSON' 的 system 提示与 response_format: { type: 'json_object' } 结合使用。
  • 流式输出能显著改善长响应的感知延迟。delta 格式与 OpenAI 完全一致。
  • Haiku-4-5 的价格约为 Sonnet-4-6 的 5× 更低,同时在短任务上能力相近——可将简单查询路由到 Haiku 以节省成本。
  • 视觉:在 content 数组中将图片作为 { type: 'image_url', image_url: { url: 'https://...' } } 传入。也支持 Data URI(base64)。

9常见问题

准备开始了吗?

注册 TokenHub,开始通过我们的 OpenAI-compatible API 调用 Claude