本页目录5
这个考点是什么
"智能体循环"(agentic loop)指的是一次对话中 Claude 与工具反复往返、直到任务真正完成为止的完整生命周期:应用把消息发给 Messages API,Claude 生成一段回复,回复末尾附带一个 stop_reason;应用据此判断本轮是「已经结束」还是「还没结束、需要我做点什么再发一次请求」。
这个考点考的不是某个工具怎么写,而是这条循环本身的状态机——stop_reason 取哪个值对应哪种后续动作。
循环里有两条并不对称的续接路径。
一条是「客户端工具」(client tool)路径:
-
Claude 返回
stop_reason: "tool_use"并附带tool_use块,说明它想调用一个由你的应用执行的工具(无论是用户自定义工具,还是 Anthropic 发布了 schema 但仍需本地执行的 bash/text_editor 一类「Anthropic-schema client tools」); -
应用必须真正跑这个工具,把结果包成
tool_result块回传,循环才能继续。
另一条是「服务器工具」(server tool,如 web_search/web_fetch/code_execution)路径:
-
这些工具在 Anthropic 自己的基础设施内部循环执行,应用只负责启用和读取结果;
-
当这个内部循环撞到自身的迭代上限还没做完时,返回的是
stop_reason: "pause_turn",续接方式是把包含这条 pause_turn 响应的对话原样重新发送,而不是执行任何工具。
除此之外,end_turn、max_tokens、stop_sequence、refusal、model_context_window_exceeded 都是终止性的 stop_reason,出现它们意味着这一轮循环该停了,不需要也不应该再补发请求。
为什么考
出题常给一个具体场景——比如 server tool 在 Anthropic 内部循环里撞到迭代上限,或 client tool 一次返回多个并行 tool_use 块——让考生判断此时的 stop_reason 是什么、以及「正确的续接动作」是什么,核心是考 tool_use 与 pause_turn 这两种「都没结束但续接方式完全不同」的情形是否会被混为一谈。
也常借 tool_choice 的四种取值(auto/any/tool+name/none)设计辨析题,考「强制调用某个工具」和「强制调用某个工具但不指定是谁」的区别。
另一类常见考法是以「这个工具到底由谁执行」为线索,混淆 server tools(web_search/web_fetch/code_execution)与 Anthropic 出 schema 但仍需本地执行的 client tools(bash/text_editor)之间的边界。
核心辨析
- 1
tool_use与pause_turn都表示「本轮没完成、需要续」,但性质不同tool_use是客户端工具,需要应用亲自执行工具并回传tool_result;pause_turn是 server-side 工具(web_search/web_fetch/code_execution)的内部循环撞到迭代上限,续接方式是把含pause_turn的完整对话原样重发,既不用应用执行工具,也不应额外插入一条「请继续」的 user 消息。 - 2
收到
stop_reason: "tool_use"后的续接有固定格式先把 assistant 那条含
tool_use块的完整内容追加进历史,再用一条新的 user 消息把这一轮涉及的全部tool_result一起放进去,且必须紧跟在 assistant 消息之后;每个tool_result用tool_use_id对应各自的tool_use块,顺序由数组内位置而非消息条数决定。 - 3
tool_choice四种取值边界要分清auto(默认)由模型自行判断是否调用工具及调用哪个;any强制必须调用某个工具,但具体调用哪个仍由模型自选;{"type":"tool","name":"..."}才能锁定唯一指定的工具;none禁止任何工具调用。disable_parallel_tool_use是控制并行调用数量的独立开关,不能替代tool类型去指定具体工具。 - 4
server tools 与 client tools 的执行方边界不是「schema 是谁定义的」,而是「代码在哪跑」:
-
web_search/web_fetch/code_execution(以及tool_search)在 Anthropic 基础设施内部循环执行,应用只需启用并读取结果,不构造tool_result; -
bash/text_editor(及 memory、computer use 等)虽由 Anthropic 发布 schema 并训练过,仍属于「Anthropic-schema client tools」,Claude 照样返回tool_use,需应用本地执行后回传tool_result,走的是和用户自定义工具完全相同的路径。
-
- 5
end_turn、max_tokens、stop_sequence、refusal、model_context_window_exceeded都是终止性stop_reason,出现即应结束循环;只有tool_use和pause_turn需要应用侧继续动作,但二者对应的具体动作完全不同,不能按同一套逻辑处理。
反模式对照
收到 pause_turn 后自己去执行一个「工具」,或额外拼一条「请继续」的 user 消息再发送
把含 pause_turn 的 assistant 响应连同此前全部对话原样重新发送,让服务器从内部循环的断点自动续跑
pause_turn 只是 server-side 工具内部循环的暂停,不是要应用执行工具;额外插入消息或改写历史会打断服务器原本要续跑的上下文。
把同一轮里多个 tool_use 对应的 tool_result 拆成多条 user 消息分别发送
把这一轮全部 tool_result 放进同一条 user 消息的 content 数组一并发送,紧跟在 assistant 的 tool_use 消息之后
顺序由数组内位置决定而非消息条数;拆分发送会让模型误以为不该再并行调用工具,从而在后续轮次退化为逐个串行调用。
用 tool_choice: {"type": "any"} 以为能指定 Claude 调用某个具体工具
需要锁定某个具体工具时用 tool_choice: {"type": "tool", "name": "..."}
any 只保证「必须调用某个工具」,具体调用哪一个仍由模型自行挑选;只有 tool+name 类型才能把调用范围收窄到唯一指定的工具。
认为 bash/text_editor 这类 Anthropic 发布了 schema 的工具会像 web_search 一样在服务器端自动执行完再返回结果
把它们当作 client tool 处理:收到 tool_use 后由应用本地执行,再把结果包成 tool_result 回传
是否服务器端执行的分界线是「属不属于 server tools(web_search/web_fetch/code_execution/tool_search)」,而不是「schema 是不是 Anthropic 定义的」。
练这个考点
练习模式支持按考点专练(需 Google 登录,不占用正式考机会)。
专练考点 1.1该考点的公开题(免登录,含完整解析):
- You want to guarantee that on the next turn Claude calls exactly one specific tool named g…
- In the client-tool agentic loop, which stop_reason indicates that your application must ex…
- Anthropic describes the "augmented LLM" as the basic building block of agentic systems. Wh…
- A single assistant response returns with stop_reason "tool_use" and contains three tool_us…
- Your application drives an agentic loop with client (user-defined) tools. Claude returns a…
延伸阅读: