POST /v1/messages
与人类兼容的消息 API,具有别名、流、工具、视觉、缓存和思考。
POST /v1/messages
验证
x-api-key: mg_live_...
anthropic-version: 2023-06-01
content-type: application/json
也接受承载认证。
要求
curl https://api.model-gate.com/v1/messages \
-H "x-api-key: mg_live_..." \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4.6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Write a short release note."}
]
}'
回复
{
"id": "msg_example",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4.6",
"content": [{"type": "text", "text": "This release improves documentation and model routing."}],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 11,
"output_tokens": 9,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}
流媒体
添加 "stream": true。 Model Gate转发与Anthropic兼容的SSE事件并记录最终的代币使用情况以进行结算。
别名和会计
如果提供了别名,则在请求排队或向上游发送之前,它会被替换为规范模型。定价、使用成本、请求历史记录和模型限制均使用规范模型。
| 地位 | 意义 | 行动 |
|---|---|---|
400 | 无效请求 | 检查请求正文和模型 ID。 |
401 | 无效或冻结的密钥 | 验证或轮换 API 密钥。 |
429 | 超出速率或并发限制 | 尊重 Retry-After 并通过退避重试。 |
5xx | 临时上游或网关错误 | 使用指数退避重试幂等请求。 |