Appearance
Anthropic 模型接口文档
统一消息接口,兼容普通消息、流式输出与工具调用。
- Base URL:
https://api.agentpivot.ai - 数据格式:JSON
一、接口概览
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /v1/messages | 统一消息接口(普通 / 流式 / 工具调用) |
二、鉴权
二选一:
| Header | 说明 |
|---|---|
x-api-key: <API_KEY> | 使用 API Key |
Authorization: Bearer <token> | 使用 JWT 或 Bearer Token |
三、请求参数
Content-Type:application/json
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型标识 |
messages | array | ✅ | 对话消息列表 |
max_tokens | number | ❌ | 回复最大 token 数,默认 4096(仅普通消息生效) |
thinking | object | ❌ | 思考配置,默认 { "type": "enabled", "budget_tokens": 2048 }(仅普通消息生效) |
stream | boolean | ❌ | 为 true 时返回 SSE 流式响应 |
tools | array | ❌ | 工具定义列表,长度 > 0 时走工具调用 |
messages 结构
| 字段 | 类型 | 说明 |
|---|---|---|
role | string | user | assistant |
content | string | 消息内容 |
thinking 结构
| 字段 | 类型 | 说明 |
|---|---|---|
type | string | 如 "enabled" |
budget_tokens | number | 思考过程 token 预算 |
tools 单项结构
| 字段 | 类型 | 说明 |
|---|---|---|
name | string | 工具名称,如 get_weather |
description | string | 工具描述 |
input_schema | object | 入参 JSON Schema(type、properties、required) |
四、行为说明
- 流式:
stream === true时,响应为 SSE(Content-Type: text/event-stream),不返回 JSON body。 - 工具调用:
tools存在且长度 > 0 时走工具调用逻辑,响应为 JSON。 - 普通消息:其余情况为普通对话,支持
max_tokens、thinking,响应为 JSON。
五、请求示例
1. 普通消息
bash
curl -X POST 'https://api.agentpivot.ai/v1/messages' \
-H 'Content-Type: application/json' \
-H 'x-api-key: YOUR_API_KEY' \
-d '{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 4096,
"messages": [{ "role": "user", "content": "Hello, world" }],
"thinking": { "type": "enabled", "budget_tokens": 2048 }
}'2. 流式
bash
curl -X POST 'https://api.agentpivot.ai/v1/messages' \
-H 'Content-Type: application/json' \
-H 'x-api-key: YOUR_API_KEY' \
-d '{
"model": "claude-haiku-4-5-20251001",
"messages": [{ "role": "user", "content": "Say hi in one word" }],
"stream": true
}'3. 工具调用
bash
curl -X POST 'https://api.agentpivot.ai/v1/messages' \
-H 'Content-Type: application/json' \
-H 'x-api-key: YOUR_API_KEY' \
-d '{
"model": "claude-sonnet-4-5-20250929",
"messages": [{ "role": "user", "content": "今天北京的天气怎么样?" }],
"tools": [{
"name": "get_weather",
"description": "获取指定位置的当前天气",
"input_schema": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "城市名称,如:北京" }
},
"required": ["location"]
}
}]
}'六、响应格式
普通消息(JSON)
json
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [{ "type": "text", "text": "..." }],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 10,
"output_tokens": 20,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 消息 ID |
type | string | 固定为 message |
role | string | 角色 |
content | array | 内容块,常见 { "type": "text", "text": "..." } |
stop_reason | string | 结束原因,如 end_turn |
usage | object | token 用量 |
工具调用(JSON)
响应体为 { "data": <工具调用结果> },结构与 Anthropic Messages API 兼容,通常包含模型返回的工具调用信息及可选后续轮次。
流式(SSE)
- Content-Type:
text/event-stream - 事件类型:
message_start、content_block_delta、message_delta、message_stop等,与 Anthropic Messages API 流式格式兼容。