C
Claude API 指南

TokenHub 上的 Claude — OpenAI-Compatible Anthropic Models

透過 TokenHub 統一的 /chat/completions endpoint 呼叫 Anthropic Claude models。完整相容 OpenAI-compatible 的請求與回應格式 — 無需遷移 SDK。支援串流、tool use(function calling)以及 vision(image input)。依 model tier 以每 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 為字串或 parts 陣列(用於視覺 / 工具結果)。
max_tokensinteger選填1024最大輸出 tokens(Claude 需要此參數)。通常為 1024–4096。
temperaturenumber選填1.0Sampling temperature, 0.0–1.0. Lower = more deterministic.
top_pnumber選填1.0核採樣。請使用 temperature 或 top_p,其一即可,不要同時使用。
streamboolean選填false若為 true,回傳 Server-Sent Events(SSE)增量。
stopstring[]選填最多 4 個停止序列。
toolsarray選填工具 / 函式定義清單,用於工具使用(function calling)。
tool_choicestring|object選填auto控制工具選擇:auto | none | required | { type:'function', function:{ name } }。
response_formatobject選填JSON mode: { "type": "json_object" } forces the model to return valid 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 prompts 的遵循度很高。
  • 若要進行結構化擷取,請將像 'Return only JSON' 這樣的 system prompt 與 response_format: { type: 'json_object' } 搭配使用。
  • 串流能大幅改善長回應的感知延遲。delta 格式與 OpenAI 完全一致。
  • Haiku-4-5 的費用約為 Sonnet-4-6 的 5× 更便宜,且在短篇任務上具備相近能力 — 請將簡單查詢路由到 Haiku 以節省成本。
  • Vision:將圖片以 { type: 'image_url', image_url: { url: 'https://...' } } 的形式放入 content 陣列中。也支援 Data URI(base64)。

9常見問題

準備開始了嗎?

註冊 TokenHub,開始透過我們的 OpenAI-compatible API 呼叫 Claude