Messages (Anthropic)
採用 Messages 協議格式,適用於 Claude 系列模型以及任何使用該協議的 SDK 與客戶端。
POST https://wrouter.ai/v1/messages請求頭
| Header | 必填 | 說明 |
|---|---|---|
Authorization | ✓* | Bearer sk-... |
x-api-key | ✓* | sk-...(替代 Authorization) |
anthropic-version | ✓ | 例如 2023-06-01 |
Content-Type | ✓ | application/json |
*Authorization 與 x-api-key 二選一。
請求體引數
| 引數 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | ✓ | 例如 claude-sonnet-4-5、claude-opus-4-1 |
messages | array | ✓ | 對話訊息,結構同 Anthropic 官方 |
max_tokens | integer | ✓ | 最大輸出 token |
system | string | array | 系統提示 | |
temperature | number | ||
top_p | number | ||
top_k | integer | ||
stream | boolean | SSE 流式 | |
stop_sequences | array | ||
tools | array | Anthropic Tools 定義 | |
tool_choice | object | {"type":"auto"|"any"|"tool","name":"..."} | |
thinking | object | Extended thinking,例如 {"type":"enabled","budget_tokens":10000} | |
cache_control | object | Prompt 快取,節省成本 | |
context_management | object | 長對話上下文管理策略 | |
output_config | object | 輸出配置 | |
output_format | object | 輸出格式約束(如結構化輸出) | |
mcp_servers | array | MCP(Model Context Protocol)伺服器列表 | |
container | string | object | 工具執行容器 | |
inference_geo | string | 推理區域提示 | |
metadata | object | {"user_id": "..."} | |
speed | string | "standard" | "fast" | |
service_tier | string | "auto" | "standard_only" |
詳細欄位語義遵循 Messages 協議規範。
示例
bash
curl https://wrouter.ai/v1/messages \
-H "x-api-key: $WROUTER_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [{"role":"user","content":"用一句話介紹 WRouter"}]
}'Python (anthropic SDK)
python
import anthropic
client = anthropic.Anthropic(
api_key="sk-...",
base_url="https://wrouter.ai",
)
resp = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "你好"}],
)
print(resp.content[0].text)響應
響應體結構:
json
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [{"type":"text","text":"你好!..."}],
"model": "claude-sonnet-4-5",
"stop_reason": "end_turn",
"usage": {"input_tokens": 12, "output_tokens": 24}
}流式時按 Messages SSE 事件協議返回 message_start、content_block_delta、message_delta、message_stop 等事件。
跨廠商呼叫
/v1/messages 端點也允許 model 為非 Claude 模型(例如 gpt-4o),WRouter 會做協議轉譯。但建議非 Claude 模型使用 /v1/chat/completions 以獲得最完整的特性支援。