一句 "hello" 背后的完整 API 结构

POST https://api.anthropic.com/v1/messages  ·  claude-trace 抓取
→ Request Body
{
"model": "claude-opus-4-7",
"messages": [{
"role": "user",
"content": [6 个 block,5 个是注入的上下文
{ "text": "<system-reminder> Hooks 配置..." },
{ "text": "<system-reminder> Deferred Tools 清单..." },
{ "text": "<system-reminder> MCP 服务器使用指南..." },
{ "text": "<system-reminder> 可用 Skills 完整列表..." },
{ "text": "<system-reminder> CLAUDE.md 项目指令..." },
{ "text": "hello", "cache_control": {"ttl": "1h"} }
]
}],

"system": [4 块系统提示词
{ "text": "x-anthropic-billing-header: cc_version=2.1.112..." },
{ "text": "You are Claude Code, Anthropic's official CLI..." },
{ "text": "# System ... # Doing tasks ... # Executing ...",
"cache_control": {"ttl": "1h", "scope": "global"} },
{ "text": "# Text output ... # auto memory ... # Environment" }
],

"tools": [10 个工具定义
{ "name": "Agent", "description": "Launch a new agent to handle complex, multi-step tasks..." },
{ "name": "Bash", "description": "Executes a given bash command and returns its output..." },
{ "name": "Edit", "description": "Performs exact string replacements in files..." },
{ "name": "Glob", "description": "Fast file pattern matching tool..." },
{ "name": "Grep", "description": "A powerful search tool built on ripgrep..." },
{ "name": "Read", "description": "Reads a file from the local filesystem..." },
{ "name": "Write", "description": "Writes a file to the local filesystem..." },
{ "name": "ScheduleWakeup", "description": "Schedule when to resume work..." },
{ "name": "Skill", "description": "Execute a skill within the main conversation..." },
{ "name": "ToolSearch", "description": "Fetches full schema for deferred tools..." }
],

"max_tokens": 64000,
"stream": true,
"thinking": { "type": "adaptive" },自动决定是否深度推理
"output_config": { "effort": "xhigh" }最高推理档位(默认)
}
← Response  SSE text/event-stream
event: message_start
data:
{
"type": "message_start",
"message": {
"model": "claude-opus-4-7",
"usage": {
"input_tokens": 6,你的 hello
"cache_creation_input_tokens": 14,145,新写入 1h 缓存
"cache_read_input_tokens": 16,835,复用已有缓存
"output_tokens": 4
}
}
}

event: content_block_start
{ "type": "content_block_start", "content_block": {"type": "text"} }

event: ping
{ "type": "ping" }心跳,保持连接

event: content_block_delta
{ "delta": {"type": "text_delta", "text": "Hello!"} }

event: content_block_delta
{ "delta": {"type": "text_delta", "text": " How can I help you today?"} }

event: content_block_stop
{ "type": "content_block_stop", "index": 0 }

event: message_delta
{
"delta": { "stop_reason": "end_turn" },模型主动结束回复
"usage": { "output_tokens": 15 }最终 output token 数
}

event: message_stop
{ "type": "message_stop" }消息流结束

实际上下文 = 6 + 14,145 + 16,835 = 30,986 tokens 缓存读取比正常输入便宜 90%