Anthropic 模型
POST /v1/messages发送结构化的消息列表(文本和/或图片内容),模型将生成对话中的下一条消息。Messages API 可用于单次查询,也可用于无状态多轮对话。
参数
请求体
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
max_tokens | int | 是 | - | 生成响应的最大 token 数(含思考和回复内容),是总输出的硬上限,模型可能在达到该值前自然停止。设为 0 可仅预热提示缓存而不生成内容。不同模型上限不同。Anthropic 中该参数必填(使用本平台的 API Key 时可不填,平台会填写默认值,也可根据需要自由设置) |
messages | array | 是 | - | 输入消息列表,每条消息包含 role(user/assistant)和 content。连续相同角色的消息会合并为一条;最后一条为 assistant 时,响应从该消息续写(预填充) |
model | string | 是 | - | 完成请求的模型 ID |
cache_control | object | 否 | - | 顶层缓存控制,自动在请求中最后一个可缓存的内容块上应用 cache_control 标记 |
container | object 或 string | 否 | - | 用于跨请求复用的容器标识符(代码执行工具) |
context_management | object | 否 | - | 上下文管理配置,控制模型如何跨请求管理上下文(如是否清除工具结果) |
diagnostics | object | 否 | - | 请求级诊断,当前携带上一个响应 ID 用于提示缓存分歧报告 |
fallback_credit_token | string 或 object | 否 | - | 来自先前拒绝响应 stop_details 的令牌。重试被拒绝的请求时传入,可使前缀的缓存创建 token 按缓存读取费率计费。需同一组织与工作区、相同请求体、符合条件的备用模型、同一平台,并在拒绝后 5 分钟内使用 |
fallbacks | array 或 "default" | 否 | - | 请求的模型因策略原因拒绝时,在服务端按顺序尝试一个或多个替代模型。字符串 "default" 表示使用该模型的服务端默认备用配置 |
inference_geo | string | 否 | - | 指定推理处理的地理区域,未指定时使用工作区的 default_inference_geo |
mcp_servers | array | 否 | - | 请求中使用的 MCP 服务器定义(URL 方式),最多 20 个 |
metadata | object | 否 | - | 请求元数据,可包含 user_id 字段标识最终用户(用于滥用监控和追踪),格式如 {"user_id": "user-123"} |
output_config | object | 否 | - | 模型输出的配置选项,如输出格式 |
output_format | object | 否 | - | 已弃用:改用 output_config.format。指定响应输出格式的 Schema,后续版本将移除 |
service_tier | string | 否 | auto | 服务层级:auto(优先算力可用时使用)/ standard_only(仅标准算力) |
speed | string | 否 | - | 推理速度模式:standard / fast。fast 以更高价格显著加快输出 token 生成,并非所有模型支持,无效组合在创建时被拒绝 |
stop_sequences | array | 否 | - | 自定义停止序列。模型生成到任一序列时立即停止,stop_reason 为 stop_sequence,响应 stop_sequence 字段返回命中的序列 |
stream | bool | 否 | false | 是否启用 SSE 流式输出,见流式响应 |
system | string 或 array | 否 | - | 系统提示词,用于设置助手的行为、角色、上下文和约束。可以是字符串,也可以是文本块数组(用于更复杂的系统指令),在整个对话中持续有效 |
thinking | object | 否 | - | 扩展思考配置,用于开启推理能力(需要模型支持),见 thinking 详细参数 |
tool_choice | object | 否 | auto | 控制工具调用方式,见 tool_choice 详细参数 |
tools | array | 否 | - | 工具定义列表,见 tools 字段说明与工具调用示例 |
temperature | float | 否 | 1.0 | 已弃用:Opus 4.6 之后的模型不支持设置,仅接受 1.0(向后兼容),其他值返回 400 |
top_k | int | 否 | - | 已弃用:Opus 4.6 之后的模型不支持,任何值都会返回 400 |
top_p | float | 否 | - | 已弃用:Opus 4.6 之后的模型不支持设置,仅接受 >= 0.99(向后兼容),其他值返回 400 |
请求头
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
beta | array | 否 | 可选请求头,指定要使用的 beta 版本 |
user_profile_id | string | 否 | 将请求归因到的用户配置 ID(代表其他方操作时使用),需要 user-profiles beta 请求头 |
workspace_id | string | 否 | 选择本次请求的工作区(如 wrkspc_011CZkZaBF1tNoB5wlCeusgy)。仅当凭据可作用于多个工作区时需要;属于特定工作区的凭据可省略,若发送则必须与该工作区匹配 |
thinking 详细参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | adaptive(推荐,由模型自行决定是否思考及思考深度)/ enabled(指定思考预算,旧模型支持,已弃用)/ disabled(关闭思考) |
budget_tokens | int | 否 | type 为 enabled 时的思考 token 预算,最小 1024,计入 max_tokens |
不同模型的思考配置差异:
- cl-op-4.7 及更高版本(含 cl-op-4.8)、cl-so-5 等新模型:仅支持自适应思考
"thinking": {"type": "adaptive"},使用旧的"type": "enabled", "budget_tokens": N写法会返回400错误。- cl-op-4.6 / cl-so-4.6 等旧模型:仍可使用
"thinking": {"type": "enabled", "budget_tokens": N}(budget_tokens需小于max_tokens),但该写法已弃用,建议改用自适应思考。
tool_choice 详细参数
type 值 | 字段 | 说明 |
|---|---|---|
auto | - | 模型自行决定是否使用工具(默认) |
any | - | 必须使用至少一个工具 |
tool | name | 强制使用指定工具,name 为工具名称 |
none | - | 不使用任何工具 |
tools 字段说明
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name | string | 是 | 工具名称 |
description | string | 否 | 工具描述,可选但强烈建议提供 |
input_schema | object | 是 | 工具入参的 JSON Schema,模型将按此结构在 tool_use 中生成 input |
工具调用示例
在请求中定义 tools 后,模型可能返回 tool_use 内容块表示工具调用。您执行工具后,将结果以 tool_result 内容块回传给模型继续对话。
curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"tools": [
{
"name": "get_stock_price",
"description": "获取指定股票代码的当前股价。",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "股票代码,如 AAPL 表示 Apple Inc."
}
},
"required": ["ticker"]
}
}
],
"messages": [
{"role": "user", "content": "今天标普 500 指数是多少?"}
]
}'模型返回 tool_use 内容块(此时 stop_reason 为 tool_use):
[
{
"type": "tool_use",
"id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
"name": "get_stock_price",
"input": { "ticker": "^GSPC" }
}
]执行工具后,在后续 user 消息中以 tool_result 内容块回传结果:
curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"tools": [
{
"name": "get_stock_price",
"description": "获取指定股票代码的当前股价。",
"input_schema": {
"type": "object",
"properties": {
"ticker": {"type": "string"}
},
"required": ["ticker"]
}
}
],
"messages": [
{"role": "user", "content": "今天标普 500 指数是多少?"},
{"role": "assistant", "content": [
{"type": "tool_use", "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", "name": "get_stock_price", "input": {"ticker": "^GSPC"}}
]},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", "content": "259.75 USD"}
]}
]
}'请求示例
curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "hello"
}
]
}'import requests
url = "https://neolink.com/api/v1/messages"
headers = {
"Authorization": "Bearer <API Key>",
"Content-Type": "application/json",
}
payload = {
"model": "<Model ID>",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "hello"
}
]
}
resp = requests.post(url, headers=headers, json=payload, timeout=30)
resp.raise_for_status()
data = resp.json()
reply = "".join(
item["text"]
for item in data.get("content", [])
if item.get("type") == "text"
)
print("Assistant reply:")
print(reply)curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "你好,用一句话介绍你自己。"},
{"role": "assistant", "content": "你好,我是一个 AI 助手。"},
{"role": "user", "content": "能再详细一点吗?"}
]
}'curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"system": "你是一位专业翻译,负责将用户输入翻译成英文。",
"messages": [
{"role": "user", "content": "今天天气真不错。"}
]
}'curl --location --request POST 'https://neolink.com/api/v1/messages' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <API Key>' \
--data-raw '{
"model": "<Model ID>",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "希腊神话中太阳神的名字是?(A) Sol (B) Helios (C) Sun"},
{"role": "assistant", "content": "最佳答案是 ("}
]
}'说明:
messages中模型按user/assistant交替轮次训练,连续相同角色的消息会自动合并为一条。- 若最后一条消息为
assistant角色,响应内容将从该消息直接续写(助手预填充),可用于约束部分输出。- 每条消息的
content可以是字符串,也可以是内容块数组;字符串等价于单个text内容块:{"type": "text", "text": "..."}。- Messages API 中没有
system角色,系统提示词需通过顶层system参数传递。- 单次请求最多 100,000 条消息。
返回
响应体
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 对象类型,固定为 message |
id | string | 消息唯一 ID |
container | object | 请求中使用的容器信息(代码执行工具),见 container 详细参数 |
content | array | 模型生成的内容块数组,见 content 内容块类型 |
context_management | object | 请求期间应用的上下文管理策略信息,见 context_management 详细参数 |
diagnostics | object | 请求级诊断响应,见 diagnostics 详细参数;仅在请求传入 diagnostics 时返回 |
model | string | 实际完成本次请求的模型 |
role | string | 会话角色,固定为 assistant |
stop_details | object | 拒绝的结构化信息,见 stop_details 拒绝详情 |
stop_reason | string | 停止原因,见 stop_reason 枚举 |
stop_sequence | string | 命中的自定义停止序列,未命中时为 null |
usage | object | 用量统计,见 usage 字段 |
input_transformations | array | API 在展示给模型前对请求输入所做的更改,见 input_transformations 详细参数;需要 thinking-binding-controls-2026-08-01 beta |
container 详细参数
| 参数名 | 类型 | 说明 |
|---|---|---|
id | string | 本次请求使用的容器标识符 |
expires_at | string | 容器过期时间,格式:date-time |
skills | array | 容器中加载的技能列表 |
skills[].type | string | 技能类型:anthropic(内置)/ custom(用户定义) |
skills[].skill_id | string | 技能 ID,长度 1~64 |
skills[].version | string | 解析后的版本:自定义技能的版本 ID,长度 1~64 |
content 内容块类型
type | 说明 |
|---|---|
text | 文本内容,见 text 块字段 |
thinking | 思考过程,见 thinking 块字段 |
redacted_thinking | 被安全处理的思考内容,见 redacted_thinking 块字段 |
tool_use | 工具调用请求,见 tool_use 块字段 |
server_tool_use | 服务端工具调用,见 server_tool_use 块字段 |
web_search_tool_result | 网页搜索工具结果,见 web_search_tool_result 块字段 |
web_fetch_tool_result | 网页抓取工具结果,见 web_fetch_tool_result 块字段 |
advisor_tool_result | advisor 工具结果,见 advisor_tool_result 块字段 |
code_execution_tool_result | 代码执行工具结果,见 code_execution_tool_result 块字段 |
bash_code_execution_tool_result | Bash 代码执行工具结果,见 bash_code_execution_tool_result 块字段 |
text_editor_code_execution_tool_result | 文本编辑器代码执行工具结果,见 text_editor_code_execution_tool_result 块字段 |
tool_search_tool_result | 工具搜索结果,见 tool_search_tool_result 块字段 |
mcp_tool_use | MCP 工具调用,见 mcp_tool_use 块字段 |
mcp_tool_result | MCP 工具结果,见 mcp_tool_result 块字段 |
container_upload | 上传到容器的文件,见 container_upload 块字段 |
compaction | 自动压缩块,见 compaction 块字段 |
fallback | 模型切换标记,见 fallback 块字段 |
text 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 text |
text | string | 文本内容,最小长度 0 |
citations | array | 支持该文本块的引用列表,见 citations 引用类型 |
citations 引用类型
引用类型取决于被引用的文档类型:引用 PDF 返回 page_location,纯文本返回 char_location,内容文档返回 content_block_location。
type | 字段 | 说明 |
|---|---|---|
char_location | cited_text、document_index、document_title、end_char_index、start_char_index、file_id | 纯文本的字符位置引用,document_index/start_char_index 最小 0 |
page_location | cited_text、document_index、document_title、end_page_number、start_page_number、file_id | PDF 的页码位置引用,start_page_number 最小 1 |
content_block_location | cited_text、document_index、document_title、end_block_index、start_block_index、file_id | 内容块位置引用,end_block_index 为排他结束索引,单块引用时等于 start_block_index + 1;cited_text 不计入输入/输出 token |
web_search_result_location | cited_text、encrypted_index、title、url | 网页搜索结果引用,title 最大长度 512 |
search_result_location | cited_text、end_block_index、start_block_index、search_result_index、source、title | 搜索结果内容块引用,search_result_index 为请求中所有 search_result 内容块的 0 起始索引(与 document_index 分开计数) |
thinking 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 thinking |
thinking | string | 该块的思考过程文本 |
signature | string | 用于验证该思考块由 Claude 生成的签名。不透明字段,不应解析;回传给 API 时需原样保留(配合扩展思考使用工具时必须回传) |
redacted_thinking 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 redacted_thinking |
data | string | 被安全处理的思考内容,不透明且加密,无可读内容。多轮对话时需原样回传 |
tool_use 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 tool_use |
id | string | 工具调用 ID,格式:^[a-zA-Z0-9_-]+$ |
name | string | 工具名称,最小长度 1 |
input | object | 模型生成的工具入参 |
caller | object | 可选。调用来源:direct(模型直接调用)/ code_execution_20250825、code_execution_20260120(服务端工具生成,含 tool_id,格式 srvtoolu_ 前缀) |
toolset_name | string | 可选。工具集成员调用时的工具集族名,长度 1~64 |
server_tool_use 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 server_tool_use |
id | string | 调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
name | string | 服务端工具名:advisor / web_search / web_fetch / code_execution / bash_code_execution / text_editor_code_execution / tool_search_tool_regex / tool_search_tool_bm25 |
input | object | 工具入参 |
caller | object | 可选。调用来源,同 tool_use 块的 caller |
web_search_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 web_search_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object 或 array | 错误对象(type 为 web_search_tool_result_error,error_code 如 invalid_tool_input / unavailable / max_uses_exceeded / too_many_requests / query_too_long / request_too_large 等),或结果数组 |
content[].type | string | 固定为 web_search_result |
content[].url | string | 结果 URL |
content[].title | string | 结果标题 |
content[].page_age | string | 页面时间 |
content[].encrypted_content | string | 加密的内容快照 |
caller | object | 可选。调用来源,同 tool_use 块的 caller |
web_fetch_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 web_fetch_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 web_fetch_tool_result_error,error_code 如 invalid_tool_input / url_too_long / url_not_allowed / url_not_in_prior_context / url_not_accessible / unsupported_content_type / too_many_requests / max_uses_exceeded / unavailable / content_too_large 等),或抓取结果 |
content.type | string | 成功时固定为 web_fetch_result |
content.url | string | 抓取内容的 URL |
content.retrieved_at | string | 内容抓取时间,ISO 8601 时间戳 |
content.content | object | 抓取的文档:type 为 document,title 为文档标题,citations.enabled 为引用配置,source 为 base64(media_type: application/pdf)或 text(media_type: text/plain) |
caller | object | 可选。调用来源,同 tool_use 块的 caller |
advisor_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 advisor_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 advisor_tool_result_error,error_code 如 max_uses_exceeded / prompt_too_long / too_many_requests / overloaded / unavailable / execution_time_exceeded / model_not_found 等),或结果块 |
content.type | string | advisor_result(含 text 与 stop_reason)或 advisor_redacted_result(含加密 encrypted_content 与 stop_reason) |
content.text | string | advisor 子推理的输出文本 |
content.stop_reason | string | advisor 子推理的停止原因(与顶层消息 stop_reason 取值相同) |
content.encrypted_content | string | 不透明加密块,包含 advisor 输出,需原样往返,不要检查或修改 |
code_execution_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 code_execution_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 code_execution_tool_result_error,error_code:invalid_tool_input / unavailable / too_many_requests / execution_time_exceeded),或结果块 |
content.type | string | code_execution_result 或 encrypted_code_execution_result(stdout 加密,用于 PFC + web_search 结果) |
content.content | array | 输出文件列表,每项含 type(code_execution_output)与 file_id |
content.return_code | int | 进程返回码 |
content.stdout | string | 标准输出(加密变体中为 encrypted_stdout) |
content.stderr | string | 标准错误 |
bash_code_execution_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 bash_code_execution_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 bash_code_execution_tool_result_error,error_code 如 invalid_tool_input / unavailable / too_many_requests / execution_time_exceeded / output_file_too_large 等),或结果块 |
content.type | string | 固定为 bash_code_execution_result |
content.content | array | 输出文件列表,每项含 type(code_execution_output)与 file_id |
content.return_code | int | 进程返回码 |
content.stdout | string | 标准输出 |
content.stderr | string | 标准错误 |
text_editor_code_execution_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 text_editor_code_execution_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 text_editor_code_execution_tool_result_error,含 error_code 如 invalid_tool_input / unavailable / too_many_requests / execution_time_exceeded / file_not_found 等与 error_message),或以下四种结果块之一 |
content.type | string | text_editor_code_execution_view_result(查看:content、file_type(text/image/pdf)、num_lines、start_line、total_lines)/ text_editor_code_execution_create_result(创建:is_file_update)/ text_editor_code_execution_str_replace_result(替换:lines、new_lines、new_start、old_lines、old_start) |
tool_search_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 tool_search_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^srvtoolu_[a-zA-Z0-9_]+$ |
content | object | 错误对象(type 为 tool_search_tool_result_error,含 error_code 与 error_message),或搜索结果块 |
content.type | string | 固定为 tool_search_tool_search_result |
content.tool_references | array | 匹配的工具引用列表,每项含 type(tool_reference)与 tool_name(长度 1~256) |
mcp_tool_use 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 mcp_tool_use |
id | string | 调用 ID,格式:^[a-zA-Z0-9_-]+$ |
name | string | MCP 工具名称 |
server_name | string | MCP 服务器名称 |
input | object | 工具入参 |
mcp_tool_result 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 mcp_tool_result |
tool_use_id | string | 对应的调用 ID,格式:^[a-zA-Z0-9_-]+$ |
content | string 或 array | 工具结果:字符串,或文本块数组(每项含 type(text)、text、可选 citations) |
is_error | bool | 是否为错误结果 |
container_upload 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 container_upload |
file_id | string | 上传文件的 ID |
compaction 块字段
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 compaction |
content | string | 压缩内容的摘要;压缩失败时为 null(客户端可原样往返,服务端视为无操作) |
encrypted_content | string | 先前压缩的不透明元数据,需原样往返 |
fallback 块字段
标记 content 中一个模型的输出切换到下一个模型的位置。每个实际运行且拒绝的模型跳转产生一个块;响应是否由备用模型服务以 usage.iterations 中是否存在 fallback_message 条目为准。流式传输时该块通过标准 content_block_start / content_block_stop 对到达,不携带增量。
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 fallback |
from.model | string | 输出在此处结束的模型(拒绝的模型):请求模型跳转时回显调用方发送的模型字符串,备用模型跳转时为该模型规范 ID |
to.model | string | 产生后续内容的备用模型,始终为规范 ID |
trigger.type | string | 固定为 refusal |
trigger.category | string | 触发拒绝的策略类别:cyber(网络危害)/ bio(生物危害)/ frontier_llm(竞争模型开发)/ reasoning_extraction(要求复现内部推理)/ general_harms(其他危害领域) |
context_management 详细参数
| 参数名 | 类型 | 说明 |
|---|---|---|
applied_edits | array | 已应用的上下文管理编辑列表,见下表 |
applied_edits 条目类型:
type | 字段 | 说明 |
|---|---|---|
clear_tool_uses_20250919 | cleared_input_tokens、cleared_tool_uses | 清除工具使用的编辑,均为 int,最小 0 |
clear_thinking_20251015 | cleared_input_tokens、cleared_thinking_turns | 清除思考轮次的编辑,均为 int,最小 0 |
diagnostics 详细参数
| 参数名 | 类型 | 说明 |
|---|---|---|
cache_miss_reason | object | 解释为何提示缓存无法完全复用上一个请求(由 diagnostics.previous_message_id 标识)的前缀;null 表示诊断仍在进行(响应在后台比较完成前已序列化) |
cache_miss_reason 类型:
type | 字段 | 说明 |
|---|---|---|
model_changed | cache_missed_input_tokens | 模型变化导致前缀不匹配 |
system_changed | cache_missed_input_tokens | 系统提示变化导致前缀不匹配 |
tools_changed | cache_missed_input_tokens | 工具变化导致前缀不匹配 |
messages_changed | cache_missed_input_tokens | 消息变化导致前缀不匹配 |
previous_message_not_found | - | 上一个消息未找到 |
unavailable | - | 诊断不可用 |
cache_missed_input_tokens 为若前缀匹配上一个请求,本可从缓存读取的近似输入 token 数。
stop_reason 枚举
| 值 | 说明 |
|---|---|
end_turn | 模型到达自然停止点 |
max_tokens | 达到请求的 max_tokens 或模型上限 |
stop_sequence | 生成了自定义停止序列之一 |
tool_use | 模型调用了一个或多个工具 |
pause_turn | 长任务轮次被暂停,可将响应原样作为输入回传让模型继续 |
compaction | 触发自动压缩,长上下文被压缩摘要替换 |
refusal | 安全分类器介入,拒绝处理潜在违规请求 |
model_context_window_exceeded | 超出模型上下文窗口 |
非流式模式下该值始终非空;流式模式下在 message_start 事件中为 null,其余事件中非空。
stop_details 拒绝详情
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 refusal |
category | string | 触发拒绝的策略类别:cyber / bio / frontier_llm / reasoning_extraction / general_harms |
explanation | string | 拒绝的人类可读说明(不保证稳定),无说明时为 null |
fallback_credit_token | string | 重试时退还缓存成本的不透明代码,作为 fallback_credit_token 传入重试请求,拒绝后 5 分钟过期;重试需同一工作区、同一平台,不匹配返回 400;不符合条件时为 null |
fallback_has_prefill_claim | bool | 是否可通过追加 assistant 消息的方式兑换该令牌(true:同请求体 + 追加 assistant 消息重试;false:原请求体重试),仅在 fallback_credit_token 存在时返回 |
recommended_model | string | 服务端建议的重试目标模型(备用模型限流或过载导致无法尝试时返回),否则为 null |
usage 字段
| 参数名 | 类型 | 说明 |
|---|---|---|
input_tokens | int | 输入 token 数,最小 0 |
output_tokens | int | 输出 token 数(计费的权威总量),最小 0 |
cache_creation_input_tokens | int | 用于创建缓存条目的输入 token 数,最小 0 |
cache_read_input_tokens | int | 从缓存读取的输入 token 数,最小 0 |
cache_creation | object | 按 TTL 细分的缓存创建 token:ephemeral_5m_input_tokens(5 分钟缓存)/ ephemeral_1h_input_tokens(1 小时缓存),均最小 0 |
server_tool_use | object | 服务端工具请求次数:web_search_requests / web_fetch_requests,均最小 0 |
service_tier | string | 本次请求使用的服务层级:standard / priority / batch |
speed | string | 推理速度模式:standard / fast |
inference_geo | string | 本次请求推理所在的地理区域 |
iterations | array | 每次采样迭代的 token 用量明细,见 iterations 详细参数 |
output_tokens_details | object | 输出 token 分类明细:thinking_tokens(内部推理 token 数,含思考块分隔符;基于原始推理文本重新分词计算,始终 ≤ output_tokens),最小 0 |
fallback_credit | object | 请求传入 fallback_credit_token 的兑换结果,见 fallback_credit 详细参数 |
总输入 token =
input_tokens+cache_creation_input_tokens+cache_read_input_tokens。
iterations 详细参数
每次采样迭代一个条目,按 type 区分。message 条目可用于判断哪些迭代超过长上下文阈值(≥200k token)、从最后一个 message 条目推算上下文窗口大小、理解服务端工具循环中的 token 累积。
type | 字段 | 说明 |
|---|---|---|
message | input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens、cache_creation、model | 模型采样迭代(如服务端工具循环的各轮次) |
compaction | input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens、cache_creation | 压缩操作本身的用量(非被压缩上下文的大小,可能远小于被关闭的上下文;不要从 compaction 条目推算上下文窗口大小;不计入顶层 usage 字段)。输入 token 触发器默认 150,000 token |
advisor_message | input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens、cache_creation、model | advisor 子推理迭代 |
fallback_message | input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens、cache_creation、model | 服务端备用请求中实际服务响应的备用模型尝试(替代该跳的 message 条目;是否存在此条目标志响应是否由备用模型服务) |
fallback_credit 详细参数
| 参数名 | 类型 | 说明 |
|---|---|---|
status.type | string | redeemed(已兑换:重试按整个会话一直在重试模型上计费)/ not_applied(未应用,见 reason) |
status.reason | string | not_applied 时的原因:body_mismatch / continuation_excluded / continuation_only / expired / invalid_target_model / not_enabled / reprice_unavailable / temporarily_unavailable / variant_fields_present / wrong_organization / wrong_platform / wrong_workspace |
status.remove_to_redeem | array | 仅当 reason 为 variant_fields_present 时返回(非 null 非空数组):重试前需移除的请求字段列表,移除后在令牌 5 分钟窗口内重发仍可兑换 |
input_transformations 详细参数
API 在展示给模型前对请求输入所做的更改,每个更改一个条目,按请求顺序排列。当前唯一条目类型为 thinking_dropped;后续可能新增类型,不认识的类型应忽略。
| 参数名 | 类型 | 说明 |
|---|---|---|
type | string | 固定为 thinking_dropped:请求 messages 中未通过绑定检查而被移除的 thinking / redacted_thinking / connector_text 块 |
path | string | 被移除块在请求中的位置,格式 messages.{i}.content.{j}(i 为 messages 数组索引,j 为该消息 content 数组索引) |
reason | string | 触发移除的绑定检查:model_binding_mismatch(创建块与请求模型不匹配)/ prefix_binding_mismatch(块之前的会话与创建时不一致,该轮后续连续思考块一并移除)/ organization_binding_mismatch(不同组织创建)/ end_user_binding_mismatch(不同最终用户或被消费者组织绑定移除)。多个检查同时失败时按此优先级报告一个:organization_binding_mismatch > end_user_binding_mismatch > model_binding_mismatch > prefix_binding_mismatch |
被移除的块不计入 usage.input_tokens。流式模式下该数组在 message_start 中为最终值;仅当流中发生服务端模型备用时,最终 message_delta 事件才会携带服务模型的条目并替换 message_start 中的值。
响应示例
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"model": "cl-so-5",
"content": [
{
"type": "text",
"text": "Hi, I'm Claude."
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 12,
"output_tokens": 20,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}流式响应
请求体中设置 "stream": true 后,响应以 SSE 事件流形式逐步返回:
| 事件类型 | 说明 |
|---|---|
message_start | 消息开始,包含初始 message 对象(此时 usage 中仅 input_tokens 有值) |
content_block_start | 内容块开始,包含初始的空内容块 |
content_block_delta | 内容块增量,delta.type 可为 text_delta(文本)/ input_json_delta(工具入参)/ thinking_delta(思考)/ signature_delta(签名)/ citations_delta(引用) |
content_block_stop | 内容块结束 |
message_delta | 消息级增量,包含最终 stop_reason 与累计 usage |
message_stop | 消息结束 |
ping | 保活心跳 |
流式示例
event: message_start
data: {"type":"message_start","message":{"id":"msg_01XFDUDYJgAACzvnptvVoYEL","type":"message","role":"assistant","content":[],"model":"cl-so-5","stop_reason":null,"usage":{"input_tokens":12,"output_tokens":1}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi, I'm Claude."}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":20}}
event: message_stop
data: {"type":"message_stop"}