Messages
POST /v1/messages
兼容 Anthropic Messages API 格式。支持流式(SSE)和非流式两种模式。
请求格式
{
"model": "claude-sonnet-4-8-20260601",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello!"}
],
"system": "You are a helpful assistant.",
"stream": false
}必需请求头
| 请求头 | 值 |
|---|---|
anthropic-version | 2023-06-01 |
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID |
max_tokens | integer | 是 | 最大生成 Token 数 |
messages | array | 是 | 消息列表,支持 user、assistant 角色 |
system | string | 否 | 系统提示词 |
stream | boolean | 否 | 是否启用流式响应 |
temperature | number | 否 | 采样温度 |
top_p | number | 否 | 核采样参数 |
top_k | integer | 否 | Top-K 采样 |
stop_sequences | array | 否 | 停止序列 |
tools | array | 否 | 工具定义 |
tool_choice | object | 否 | 工具选择策略 |
响应格式
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{"type": "text", "text": "Hello! How can I help you today?"}
],
"model": "claude-sonnet-4-8-20260601",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 10,
"output_tokens": 20
}
}提示词缓存
Messages 接口支持 Anthropic 风格的 prompt caching。在请求中使用 "cache_control": {"type": "ephemeral"} 标记缓存断点。
