Claude Messages
Anthropic 原生 Messages 协议。如果你的代码使用官方 anthropic SDK,只需将 base_url 指向网关即可。你也可以通过 OpenAI 兼容的 Chat Completions 端点调用 Claude 模型。
POST
https://apicdn.xyc.ai/v1/messages
鉴权请求头不同
该协议使用 x-api-key(而非 Authorization: Bearer),并且必须携带 anthropic-version。
请求示例
from anthropic import Anthropic
client = Anthropic(
base_url="https://apicdn.xyc.ai",
api_key="sk-xxxxxxxx",
)
msg = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[{"role": "user", "content": "Introduce yourself in one sentence."}],
)
print(msg.content[0].text)
curl https://apicdn.xyc.ai/v1/messages \
-H "x-api-key: sk-xxxxxxxx" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-8",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Introduce yourself in one sentence."}
]
}'
主要参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 必填。Claude 模型名称 |
messages | array | 必填。对话消息列表 |
max_tokens | int | 必填。生成的最大 token 数 |
system | string | 系统提示词 |
stream | bool | 是否流式输出 |
temperature | number | 采样温度 |
响应结构
{
"id": "msg_xxxxxxxx",
"type": "message",
"role": "assistant",
"model": "claude-opus-4-8",
"content": [{"type": "text", "text": "..."}],
"stop_reason": "end_turn",
"usage": {"input_tokens": 12, "output_tokens": 30}
}