Claude API 使用入门
在一页中介绍 Claude API 的基础知识,专为 Claude 和其他智能体编写。
本页为 Claude 或任何其他针对 Claude API 编写代码的智能体,在一处汇集了基础知识。内容包括模型 ID、包含文本和图像的 Messages API 请求、"thinking"(思考)、"tool use"(工具使用)及其错误、JSON 输出,以及 “streaming”(流式传输)。本页中的说明面向发送请求的一方,"Claude"指回答请求的模型。
其余内容请参阅 Messages API 参考可查看包含 API 参考文档链接的预览——按 Tab 键直到焦点进入卡片,按 Escape 键返回。以及各部分所链接的指南。
模型
Recommended default for most work, including complex agentic coding: Claude Opus 5.5: claude-opus-5-5
Step up for the hardest long-running agentic and research tasks, at 2x Claude Opus 5.5 pricing: Claude Fable 5.2: claude-fable-5-2
Previous Opus model: Claude Opus 5: claude-opus-5
Smart model: Claude Sonnet 5.5: claude-sonnet-5-5
Previous Sonnet model: Claude Sonnet 5: claude-sonnet-5
For fast, cost-effective tasks: Claude Haiku 5.5: claude-haiku-5-5
Previous Haiku model: Claude Haiku 4.5: claude-haiku-4-5-20251001调用 API
基本请求和响应
import anthropic
message = anthropic.Anthropic().messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}],
)
print(message){
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello!"
}
],
"model": "claude-opus-5-5",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 12,
"output_tokens": 6
}
}多轮对话
默认情况下,Messages API 是 “stateless”(无状态)的,因此您需要在每个请求中发送完整的对话历史。要继续对话,请将响应的 content 作为 assistant 消息添加到 messages 中。请保持其不变。它可能包含 thinking 和 tool_use 块,而在工具使用期间,这些块必须按原样传回。然后,添加下一条 user 消息。
较早的 assistant 消息不一定来自 Claude。您可以自己编写这些消息,如下例所示。在请求中,content 可以是字符串或块数组。响应包含块数组。最后一条消息是特例。请参阅预填充 Claude 的响应。
import anthropic
message = anthropic.Anthropic().messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude"},
{"role": "assistant", "content": "Hello!"},
{"role": "user", "content": "Can you describe LLMs to me?"},
],
)
print(message)借助处于 beta 阶段的 消息线程,API 可以将对话保留下来,这样之后的每个请求只需发送新消息。线程是尽力而为且短暂存在的。请按照它会在 API 存储的最后一个响应之后 5 分钟过期来进行规划。API 会在每个线程最后一次使用后不超过 1 小时内将其删除。请自行保留一份对话副本。如果之后的请求返回 404 错误,且 error.details.error_code 设置为 thread_not_found,请重新发送完整对话并开始一个新线程。
预填充 Claude 的响应
预填充是您在 assistant 消息中编写并放在 messages 最后位置的文本。messages 中其他位置的 assistant 消息不是预填充。Claude 会接续预填充的文本,响应中仅包含接续的内容。
使用预填充来设定响应的开头方式,例如让响应以固定格式开始,或跳过开场白。
API 是否接受预填充取决于模型:
- Claude 4.5 及更早版本的模型: API 接受预填充,除非该模型在您使用的平台上已退役。对已退役模型的所有请求都会失败。要查看模型的状态,请参阅模型弃用。
- 所有其他模型: API 会返回 400 错误(请参阅 Claude API 错误页面的不支持预填充部分)。请改用预填充的替代方法之一。
Claude Opus 4.5 已在 Anthropic 运营的平台上弃用。Claude Sonnet 4.5 已在相同平台上退役。有关退役日期和推荐的替代模型,请参阅模型弃用页面上的弃用历史。合作伙伴运营的平台会自行制定退役时间表。
Claude Opus 4.5 和 Claude Sonnet 4.5 的推荐替代模型不接受预填充。
以下示例在 Claude Opus 4.5 上运行,并设定多项选择答案的格式。预填充 The answer is ( 恰好在答案字母之前结束,以便 Claude 首先写出该字母。由于答案只有一个字母,该示例还将 max_tokens 设置为 1。对于较长的答案,请设置更高的 max_tokens。
import anthropic
message = anthropic.Anthropic().messages.create(
model="claude-opus-4-5-20251101",
max_tokens=1,
messages=[
{
"role": "user",
"content": "What is Latin for ant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",
},
{"role": "assistant", "content": "The answer is ("},
],
)
print(message){
"id": "msg_01Q8Faay6S7QPTvEUUQARt7h",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "C"
}
],
"model": "claude-opus-4-5-20251101",
"stop_reason": "max_tokens",
"stop_sequence": null,
"usage": {
"input_tokens": 42,
"output_tokens": 1
}
}请注意此响应中的两个字段:
text: 值为C。它仅包含接续的内容,不包含预填充。stop_reason: 值为max_tokens,因为该示例在一个令牌之后结束响应。
要获取完整文本 The answer is (C,请在您的代码中将预填充与 text 值拼接起来。
在 Claude 4.5 及更早版本的模型上,预填充受以下限制:
- 空白字符: 如果预填充以空白字符结尾,API 会返回 400 错误。
- 结构化输出: 如果请求同时设置了
output_config.format,API 会返回 400 错误(请参阅"结构化输出"页面的功能兼容性部分)。 - 扩展思考: 如果请求同时将
thinking.type设置为enabled,预填充不会导致错误。API 会将思考视为关闭,因此响应中没有thinking块。
每个 400 错误中的 message 都会说明导致该错误的原因。
预填充的替代方法
请以 user 消息结束 messages 来代替预填充。然后,根据您希望从响应中获得的内容选择一种方法:
- 遵循 schema 的 JSON: 使用结构化输出,它会约束响应遵循该 schema。在 schema 中使用
enum时,它还会将某个值约束为固定选项,但大小写除外(请参阅该页面的无效输出部分)。 - 其他输出格式: 在系统提示(即请求的
system字段可查看包含 API 参考文档链接的预览——按 Tab 键直到焦点进入卡片,按 Escape 键返回。)中描述格式。 - 无开场白: 在系统提示中要求直接给出答案。
- 接续内容、减少拒绝或保持一致的角色: 请参阅"提示最佳实践"页面的从预填充响应迁移部分。
与预填充不同,系统提示中的指令无法保证响应的开头方式,因此请在您的代码中检查响应的文本。
要将该多项选择示例迁移到不接受预填充的模型,请按照以下步骤操作:
- 更改模型并移除
assistant消息。 - 在系统提示中要求仅给出答案的字母。
- 将
max_tokens设置得足够大,以容纳整个响应。在思考始终开启或默认开启的模型上,思考令牌也计入max_tokens。 - 从响应的
text块中读取该字母。该块可能位于thinking块之后。不要将该字母与预填充拼接。
关于这些步骤在代码中对应的请求,请参阅使用 Messages API。
视觉
Claude 可以读取请求中的文本和图像。图像支持 base64 和 url 两种来源类型,以及 image/jpeg、image/png、image/gif 和 image/webp 媒体类型。
import anthropic
import base64
import httpx2
# 选项 1:Base64 编码的图像
image_url = "https://platform.claude.com/docs/images/vision-example.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx2.get(image_url).content).decode("utf-8")
message = anthropic.Anthropic().messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image_media_type,
"data": image_data,
},
},
{"type": "text", "text": "What is in the above image?"},
],
}
],
)
print(next(block.text for block in message.content if block.type == "text"))
# 选项 2:通过 URL 引用的图像
message_from_url = anthropic.Anthropic().messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://platform.claude.com/docs/images/vision-example.jpg",
},
},
{"type": "text", "text": "What is in the above image?"},
],
}
],
)
print(next(block.text for block in message_from_url.content if block.type == "text"))思考
思考可以帮助 Claude 完成非常困难的任务。使用自适应思考(thinking: {"type": "adaptive"})时,Claude 会自行决定何时思考以及思考多少。
使用自适应思考时,您可以通过 effort 参数而非令牌预算来引导思考深度。请在 output_config 中设置它,例如 output_config: {"effort": "medium"}。按思考量从少到多,级别依次为 low、medium、high、xhigh 和 max。effort 级别表格说明了哪些模型支持 xhigh 和 max。
以下模型支持思考。在列表中,"始终开启"表示您无法关闭思考。"默认开启"表示省略 thinking 参数时会运行思考。要关闭思考,请发送 thinking: {"type": "disabled"}。在 Claude Haiku 5.5 和 Claude Opus 5 上,这仅在 high 或更低的 effort 下才有效。Claude Sonnet 5.5 有所不同,列表之后的文字对此进行了说明。如果两个标签都没有,则在您设置 thinking 之前思考处于关闭状态。"旧版手动思考"是指思考的工作原理部分中描述的手动扩展思考。关于每个模型对每个 thinking 值的处理方式,请参阅"思考"页面的配置思考部分。
- Claude Fable 5.2(
claude-fable-5-2,仅支持自适应思考,始终开启) - Claude Mythos 5.2(
claude-mythos-5-2,仅支持自适应思考,始终开启) - Claude Opus 5.5(
claude-opus-5-5,仅支持自适应思考,始终开启) - Claude Sonnet 5.5(
claude-sonnet-5-5,支持自适应思考或between_tools,默认开启) - Claude Haiku 5.5(
claude-haiku-5-5,仅支持自适应思考,默认开启) - Claude Fable 5.1(
claude-fable-5-1,仅支持自适应思考,始终开启) - Claude Mythos 5.1(
claude-mythos-5-1,仅支持自适应思考,始终开启) - Claude Fable 5(
claude-fable-5,仅支持自适应思考,始终开启) - Claude Mythos 5(
claude-mythos-5,仅支持自适应思考,始终开启) - Claude Opus 5(
claude-opus-5,仅支持自适应思考,默认开启) - Claude Sonnet 5(
claude-sonnet-5,仅支持自适应思考,默认开启) - Claude Opus 4.8(
claude-opus-4-8,仅支持自适应思考) - Claude Opus 4.7(
claude-opus-4-7,仅支持自适应思考) - Claude Opus 4.6(
claude-opus-4-6,支持自适应思考或旧版手动思考) - Claude Sonnet 4.6(
claude-sonnet-4-6,支持自适应思考或旧版手动思考) - Claude Opus 4.5(
claude-opus-4-5-20251101,在 Anthropic 运营的平台上已弃用,仅支持旧版手动思考) - Claude Sonnet 4.5(已退役,但 Amazon Bedrock(
bedrock-runtime) 除外,仅支持旧版手动思考) - Claude Haiku 4.5(
claude-haiku-4-5-20251001,仅支持旧版手动思考)
Claude Mythos Preview 也支持思考。请参阅"思考"页面的配置思考部分。
Claude Sonnet 5.5 对 thinking: {"type": "disabled"} 会返回 400 错误。其最低设置为 thinking: {"type": "between_tools"},该设置会关闭前置思考,即出现在响应之前的 thinking 块。该设置不接受任何其他字段,并且在 xhigh 或 max effort 下会返回 400 错误。
思考的工作原理
开启思考后,API 响应可以在其 text 内容块之前包含 thinking 内容块。使用自适应思考时,Claude 可能会针对不需要思考的请求跳过思考,此时响应中不包含 thinking 块。因此,请按 type 而非位置来选择块。
thinking 参数的 display 字段用于设置每个 thinking 块返回的内容。您可以将其与 type: "adaptive" 或 type: "enabled" 一起设置:
"summarized":该块的thinking字段包含思考的摘要。这是 Claude 4.6 及更早版本模型上的默认值。"omitted":该块的thinking字段为空。这是 Claude 4.7 及更高版本的模型以及 Claude Mythos Preview 上的默认值。即使该字段为空,也请将该块传回。
以下示例设置了 display: "summarized"。
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
thinking={"type": "adaptive", "display": "summarized"},
messages=[
{
"role": "user",
"content": "Are there an infinite number of prime numbers such that n mod 4 == 3?",
}
],
)
# 响应包含摘要形式的思考块和文本块
for block in response.content:
match block.type:
case "thinking":
print(f"\nThinking summary: {block.thinking}")
case "text":
print(f"\nResponse: {block.text}")手动扩展思考(thinking: {"type": "enabled", "budget_tokens": N})是旧版机制。budget_tokens 设置的是 Claude 用于思考的令牌数目标,而非严格上限:
- 该预算适用于完整的思考,而非响应返回的摘要。
budget_tokens必须小于max_tokens,以便为响应留出空间,除非您使用交错思考。
结合工具使用的思考
您可以将思考与工具使用一起使用,这使 Claude 能够对工具选择和结果处理进行推理。
重要限制:
- 工具选择限制: 使用手动扩展思考(
thinking: {"type": "enabled"})时,仅支持tool_choice: {"type": "auto"}(默认)或tool_choice: {"type": "none"}。自适应思考接受强制工具使用,但强制工具使用下列出的模型除外。在接受强制工具使用的情况下,强制的工具调用会跳过思考:响应以工具调用开始,且没有thinking块。 - 保留思考块: 在工具使用期间,您必须将最后一条 assistant 消息的
thinking块传回 API。
保留思考块
import anthropic
client = anthropic.Anthropic()
weather_tool = {
"name": "get_weather",
"description": "Get the current weather for a location.",
"input_schema": {
"type": "object",
"properties": {"location": {"type": "string", "description": "The city name."}},
"required": ["location"],
},
}
weather_data = {"temperature": 72}
# 第一个请求:Claude 返回思考内容和工具请求
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
thinking={"type": "adaptive", "display": "summarized"},
tools=[weather_tool],
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
)
# 提取思考块和工具使用块
thinking_block = next(
(block for block in response.content if block.type == "thinking"), None
)
tool_use_block = next(
(block for block in response.content if block.type == "tool_use"), None
)
# 第二个请求:包含思考块和工具结果
continuation = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
thinking={"type": "adaptive", "display": "summarized"},
tools=[weather_tool],
messages=[
{"role": "user", "content": "What's the weather in Paris?"},
# 注意,thinking_block 与 tool_use_block 一同传入
{"role": "assistant", "content": [thinking_block, tool_use_block]},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use_block.id,
"content": f"Current temperature: {weather_data['temperature']}°F",
}
],
},
],
)
for block in continuation.content:
if block.type == "text":
print(block.text)交错思考
“Interleaved thinking”(交错思考)使 Claude 能够在工具调用之间进行思考,并在下一步之前对每个工具结果进行推理。
使用自适应思考时,交错思考会自动进行。在 Claude Opus 4.6 上,只有自适应思考会交错进行。
使用手动扩展思考时,请在请求中添加 interleaved-thinking-2025-05-14 beta 标头。这适用于 Claude Sonnet 4.6、Claude Opus 4.5、Claude Sonnet 4.5 和 Claude Haiku 4.5。在 Claude Sonnet 4.6 上,该标头已弃用。以下示例发送了该标头:
import anthropic
client = anthropic.Anthropic()
calculator_tool = {
"name": "calculator",
"description": "Perform arithmetic calculations.",
"input_schema": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "The math expression to evaluate.",
}
},
"required": ["expression"],
},
}
database_tool = {
"name": "database_query",
"description": "Query the product database.",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "The database query."}
},
"required": ["query"],
},
}
response = client.beta.messages.create(
model="claude-sonnet-4-6",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 10000},
tools=[calculator_tool, database_tool],
messages=[
{
"role": "user",
"content": "What's the total revenue if we sold 150 units of product A at $50 each?",
}
],
betas=["interleaved-thinking-2025-05-14"],
)
for block in response.content:
match block.type:
case "thinking":
print(f"Thinking: {block.thinking}")
case "tool_use":
print(f"Tool call: {block.name}({block.input})")
case "text":
print(f"Response: {block.text}")使用交错思考时,budget_tokens 可以超过 max_tokens,因为该预算涵盖一个助手轮次(整个工具使用循环)内的所有思考块。
工具使用
指定客户端工具
客户端工具在 API 请求的顶层参数 tools 中指定。每个工具定义包括:
| 参数 | 描述 |
|---|---|
name | 工具的名称。必须匹配正则表达式 ^[a-zA-Z0-9_-]{1,128}$。 |
description | 对工具功能、使用时机及其行为方式的详细纯文本描述。 |
input_schema | 定义工具预期参数的 JSON Schema 对象。 |
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'"
}
},
"required": ["location"]
}
}工具定义的最佳实践
提供极其详细的描述。 这是影响工具性能的最重要因素。您的描述应解释有关该工具的每一个细节,包括:
- 工具的功能
- 何时应该使用它(以及何时不应该使用)
- 每个参数的含义以及它如何影响工具的行为
- 任何重要的注意事项或限制
良好工具描述的示例:
{
"name": "get_stock_price",
"description": "Retrieves the current stock price for a given ticker symbol. The ticker symbol must be a valid symbol for a publicly traded company on a major US stock exchange like NYSE or NASDAQ. The tool will return the latest trade price in USD. It should be used when the user asks about the current or most recent price of a specific stock. It will not provide any other information about the stock or company.",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
}
},
"required": ["ticker"]
}
}对于复杂工具,请考虑使用 input_examples。 对于包含嵌套对象、可选参数或对格式敏感的输入的工具,请在工具定义中添加 input_examples 字段。它接受一个示例输入对象数组,其中每个对象都必须符合该工具的 input_schema。请参阅"定义工具"页面的提供工具使用示例部分。
控制 Claude 的输出
强制工具使用
您可以通过在 tool_choice 字段中指定工具来强制 Claude 使用特定工具:
tool_choice = {"type": "tool", "name": "get_weather"}使用 tool_choice 参数时,有四个可能的选项:
auto允许 Claude 自行决定是否调用任何提供的工具(默认)。any告诉 Claude 必须使用所提供工具中的一个。tool强制 Claude 始终使用某个特定工具。none阻止 Claude 使用任何工具。
在 Claude Fable 5.2、Claude Mythos 5.2、Claude Opus 5.5、Claude Sonnet 5.5、Claude Fable 5.1 和 Claude Mythos 5.1 上,any 和 tool 会返回 400 错误。请将 tool_choice 保持为 auto,并在工具定义上设置 "strict": true,以保证 Claude 发出的任何调用都与该工具的 input_schema 匹配。请参阅严格工具使用。
JSON 输出
要获得遵循 JSON schema 的响应,请使用结构化输出。在 output_config.format 中发送 schema:
{
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"],
"additionalProperties": false
}
}
}
}JSON 会在响应的 text 块中返回。
并行工具使用
默认情况下,Claude 可能会在一个响应中调用多个工具,此时该响应包含多个 tool_use 块。要关闭并行工具使用,请在 tool_choice 中设置 disable_parallel_tool_use: true。使用 auto 类型时,Claude 在每个响应中最多调用一个工具:
{
"tool_choice": { "type": "auto", "disable_parallel_tool_use": true }
}处理工具使用和工具结果内容块
处理客户端工具的结果
响应的 stop_reason 为 tool_use,并包含一个或多个 tool_use 内容块,其中包括:
id:此特定工具使用块的唯一标识符name:正在使用的工具的名称input:包含传递给工具的输入的对象
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "San Francisco, CA" }
}
]
}当您收到工具使用响应时,您应该:
- 从
tool_use块中提取name、id和input。 - 在您的代码库中运行与该工具名称对应的实际工具。
- 在新请求中继续对话。向
messages添加两条消息:- 一条
assistant消息,其中包含未经修改的响应content。 - 一条
user消息,其中为每个tool_use块包含一个tool_result。
- 一条
user 消息如下所示:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "15 degrees"
}
]
}处理 max_tokens 停止原因
如果 Claude 的响应在工具使用期间因达到 max_tokens 限制而被截断,请使用更高的 max_tokens 值重试请求。
处理 pause_turn 停止原因
使用网络搜索等服务器工具时,API 可能会返回 pause_turn 停止原因。请在后续请求中将暂停的响应原样传回,以继续对话。
错误排查
工具执行错误
如果工具本身在执行期间抛出错误,请返回带有 "is_error": true 的错误消息:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "ConnectionError: the weather service API is not available (HTTP 500)",
"is_error": true
}
]
}无效的工具调用
如果您的代码发现某个工具调用无效(例如,缺少必需参数),请返回一个说明该错误的 tool_result,并带有 "is_error": true。Claude 随后可以修正后再次尝试该调用。
无效调用通常意味着 Claude 没有足够的信息来使用该工具。在开发期间,请为工具定义提供更详细的 description 值。要使每次调用都与工具的 input_schema 匹配,请在工具定义上设置 "strict": true。请参阅严格工具使用。
流式传输消息
创建 Message 时,您可以设置 "stream": true,以使用 “server-sent events”(服务器发送事件),即 SSE,增量地流式传输响应。
使用 SDK 进行流式传输
import anthropic
client = anthropic.Anthropic()
with client.messages.stream(
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
model="claude-opus-5-5",
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)事件类型
每个服务器发送事件都包含一个命名的事件类型和相关的 JSON 数据。每个流使用以下事件流程:
message_start:包含一个content为空的Message对象。- 一系列内容块,每个内容块都有
content_block_start、一个或多个content_block_delta事件以及content_block_stop。 - 一个或多个
message_delta事件,表示对最终Message对象的顶层更改。 - 最后一个
message_stop事件。
警告: message_delta 事件的 usage 字段中显示的令牌计数是累计的。
内容块增量类型
文本增量
{
"type": "content_block_delta",
"index": 0,
"delta": { "type": "text_delta", "text": "Hello frien" }
}输入 JSON 增量
对于 tool_use 内容块,增量是部分 JSON 字符串:
{"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}思考增量
使用 display: "summarized" 时,思考文本会在 thinking_delta 事件中流式传输,如下例所示。使用 display: "omitted" 时,每个 thinking 块会流式传输一个 thinking_delta,其 thinking 字符串为空。
{
"type": "content_block_delta",
"index": 0,
"delta": {
"type": "thinking_delta",
"thinking": "Let me solve this step by step..."
}
}示例事件流
以下事件流式传输一个简短的文本响应:
event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-opus-5-5", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "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": "Hello"}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
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": 15}}
event: message_stop
data: {"type": "message_stop"}此页面对您有帮助吗?