Chapter 02 · Domain 1(权重 27%)
Agentic Loopstop_reason 驱动的控制流与任务分解
Domain 1 是全卷最重的一块,而 agentic loop 是这块的地基。本章只讲两件事:循环该由什么信号驱动(以及三个被官方点名的反模式),以及一个复杂任务该怎么拆。两者共用一个判断轴——让模型的结构化输出驱动控制流,而不是让你的字符串解析驱动控制流 。
一、循环的骨架
Agentic loop 的生命周期只有四步,考试反复在这四步的衔接处 出题:发请求 → 检查 stop_reason(停止原因)→ 执行被请求的工具 → 把结果放回对话历史,进入下一轮。
messages = [{"role": "user", "content": task}]
while True:
resp = client.messages.create(model=…, tools=TOOLS, messages=messages)
# ① 控制流的唯一权威信号
if resp.stop_reason == "end_turn":
break # 模型认为任务结束,退出循环
if resp.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": resp.content})
results = [run_tool(b) for b in resp.content if b.type == "tool_use"]
# ② 工具结果必须回灌进对话历史,模型才能据此推理下一步
messages.append({"role": "user", "content": results})
continue
两个容易被出题的细节:
stop_reason 是控制流的唯一权威信号。 "tool_use" 表示模型请求执行工具、循环继续;"end_turn" 表示模型完成回合、循环终止。这是一个结构化字段 ,不需要也不应该去解析自然语言。
工具结果要追加进对话历史。 模型没有隐式记忆,下一轮它能看到什么,完全取决于你往 messages 里放了什么。漏掉这一步的典型症状是:模型反复请求同一个工具 ——因为在它看来那次调用从未发生过。
一个高频误解 — stop_reason == "tool_use" 时,assistant 的这条消息里可以同时包含 text 块和 tool_use 块 (模型经常一边说明思路一边发起调用)。有 text 不代表任务结束;判断依据永远是 stop_reason 这个字段,不是内容里有没有文字。
二、三个被官方点名的反模式
Exam Guide 在 TS 1.1 的 Skills 部分直接列出了三个要避免的反模式。它们是同一个错误的三种表现:用一个概率性的信号替换了一个确定性的信号 (这正是 ch01 的 P3 与 D6 的题眼)。
反模式 它长什么样 为什么坏 正确做法
解析自然语言判终止
if "task complete" in resp.text: break
模型的措辞是概率性的:这次说 "All done",下次说 "I've finished the analysis"。措辞一变,循环要么提前退出(任务半途而废),要么永不退出
只读 stop_reason
迭代上限当主停机制
for i in range(10): …,跑满 10 轮就收工
把「跑够次数」当成「任务完成」。简单任务浪费轮次,复杂任务被硬截断,且截断点与任务状态毫无关系
上限只做兜底护栏 (防失控烧钱),主停机制仍是 stop_reason
看 assistant 文本当完成信号
if resp.content[0].type == "text": break
模型可以在同一条消息里既写文字又发起 tool call,见上面的「高频误解」
只读 stop_reason
三条的共同解药是同一句话:循环条件必须绑定在 API 返回的结构化字段上 。考场识别信号很直白——只要一个选项里出现「检测到某段文字」「跑满 N 轮」「返回了文本就算完」,它就是错的。
真实 API 的 stop_reason 还有 "max_tokens"(触到 max_tokens 上限被截断)等取值,生产代码里通常要单独处理——截断意味着这一轮的输出不完整,直接当 end_turn 处理会丢内容。但 Exam Guide 的 Appendix 只点名了 "tool_use" 与 "end_turn" 两个取值,考试也只会在这两者之间考控制流,不必为其他取值分配复习时间。
三、模型驱动 vs 预配置决策树
这是 Domain 1 反复出现的另一条轴。两种控制风格的差别,不在于「谁更智能」,而在于决策权放在哪里 :
模型驱动(model-driven) 预配置决策树 / 固定工具序列
谁决定下一步 Claude 根据当前上下文推理该调哪个工具 你的代码按 if/else 或固定顺序决定
适配 请求形态多样、路径无法穷举(客服工单、开放式研究) 路径确实固定、且必须固定(合规流程的顺序约束 )
失效模式 工具描述不清时会误选(见 ch11 ) 遇到未预料的分支就卡死或走错,且新增场景要改代码
考试立场 默认答案 只有在题干明确要求「确定性保证」时才对,而且此时正确形态是用 hooks/前置条件约束模型驱动的循环 ,不是退回决策树
注意这里和 P3(关键规则用确定性强制)的分工:需要 100% 合规时,正确做法是在模型驱动的循环外面加一道程序化闸门 (阻断 process_refund 直到 get_customer 返回已验证的 customer ID),而不是把整个 agent 退化成 if/else 流程图。 前者保留了 agent 处理多样请求的能力,后者把它变成了一个昂贵的状态机。详见 ch04 。
四、任务分解:固定流水线 vs 动态分解
一个复杂任务进来,第一个设计决策是「拆不拆、怎么拆」。Exam Guide 给了两种模式和一条选择规则:
固定顺序流水线prompt chaining 动态自适应分解dynamic decomposition
形态 把任务拆成预先定好的几个连续步骤,逐步执行 先做一步,根据这一步的中间发现 生成下一批子任务
适配 可预测的多方面审查:如「逐文件分析 → 跨文件集成 pass」 开放式调查:如「给遗留代码库补全测试」
识别信号 题干能数得出有几个方面要覆盖 题干里有「不知道会发现什么」「依赖关系需要边查边理」
模式 A:大型 code review 的两类 pass
一次性把十几个文件塞进同一个 review 请求,会出现注意力稀释(attention dilution)的典型症状:有的文件详评、有的敷衍,同一段模式在 A 文件被标为问题、在 B 文件被放行。解法是拆成两类 pass:
per-file local pass — 每个文件单独一遍,保证深度一致;抓局部问题(空指针、边界、错误处理)。
cross-file integration pass — 单独一遍看跨文件数据流;抓接口契约、状态共享、调用链问题。
注意这不是「多跑几遍取交集」——那会压掉只被间歇性发现的真 bug(这是 P7 的边界,也是官方样题 Q12 的 D 选项被判错的原因)。两类 pass 的关注点不同 ,不是同一件事做多次。
模式 B:开放式任务的三步走
「给这个遗留代码库加上全面的测试」这类任务,没法预先拆成固定步骤,因为你还不知道代码库长什么样。官方给的分解顺序是:
先测绘结构 (mapping structure)——有哪些模块、入口在哪、依赖怎么走;
定位高影响区 (high-impact areas)——哪些代码改动频繁、缺陷密集、被依赖最多;
生成优先级计划,并随依赖发现动态调整 ——计划不是一次写死的,每完成一块就用新发现修正它。
考场上,这三步的反面就是干扰项:「按字母序逐文件补测试」(没有优先级)、「先写一份完整的测试计划再开工」(假装已经了解代码库)、「一次性读完整个代码库再决定」(context 会先炸,见 ch13 )。
外部课程 · CertSafari — 本章 2 个考点(展开查看)
用于补读和错题复盘;第三方 AI 生成练习材料,非官方题库。核验答案时以 Exam Guide 和官方文档为依据。复盘方法 · 链接核对:2026-10-16。
考点回链 — 本章覆盖:TS 1.1(agentic loop 生命周期、stop_reason 控制流、tool results 回灌、模型驱动 vs 决策树、三个反模式)、TS 1.6(固定流水线 vs 动态分解、per-file + 集成 pass、开放式任务的测绘—定位—计划三步)。相邻考点:TS 1.4 的程序化强制见 ch04,TS 4.6 的多遍审查见 ch10。
本章题组
7 题,全部对标官方样题格式:题干带生产数据,四个选项都「说得通」。做完再看解析。
ch02 · Q1 · 准则 P3
Your Agent SDK application runs an order-processing agent. The loop is implemented as: send the request, then check whether the assistant's text contains the phrase "task complete" — if so, exit; otherwise execute any requested tools and iterate. In production, 8% of sessions terminate after the first tool call with the work half-finished, and another 3% never terminate until the API budget guard kills them. What is the correct fix?
A Drive the loop from stop_reason: continue while it is "tool_use", terminate when it is "end_turn", and remove the phrase check entirely.
B Instruct the model in the system prompt to always end its final message with the exact string "task complete" so the phrase check becomes reliable.
C Replace the phrase check with a fixed iteration cap of 10 turns, which bounds both the premature exits and the runaway sessions.
D Keep the phrase check but add a second check for whether the response contains any text block at all, treating a text-only response as completion.
正确 A TS 1.1:agentic loop 的控制流必须绑定在 stop_reason 这个结构化字段上——"tool_use" 继续、"end_turn" 终止。两种故障(提前退出 8%、永不退出 3%)是同一个根因的两面:用概率性的措辞替代了确定性的信号。
D6 概率当保证 违反 P3 B :靠 prompt 让模型「总是」输出某个固定字符串,仍然是概率性合规——现成的确定性字段就在响应里,没有任何理由去赌措辞。
D3 治标不治本 违反 P1 C :迭代上限是 TS 1.1 点名的反模式之一。它能限制 失控会话的损失,但「跑满 10 轮」和「任务完成」毫无关系:简单任务被浪费轮次,复杂任务被硬截断。上限只配做兜底护栏,不配做主停机制。
D3 治标不治本 违反 P1 D :把「响应里有 text 块」当完成信号,是 TS 1.1 列出的第三个反模式。stop_reason == "tool_use" 的响应完全可以同时包含 text 与 tool_use 块——模型经常一边说明思路一边发起调用,这个规则会让循环在工具还没执行时就退出。
ch02 · Q2 · 准则 P1
A research agent repeatedly calls the same search_papers tool with an identical query, up to 6 times in one session, before the iteration guard stops it. Instrumentation shows each tool call executes successfully and returns results in under 400 ms. The loop appends the assistant message to messages and then issues the next API request. What is the most likely root cause?
A The tool description for search_papers is too similar to another retrieval tool, so the model keeps re-selecting it; overlapping descriptions are a documented cause of unreliable tool selection.
B The tool results are never appended to the conversation history, so each iteration the model sees its own request but no outcome and re-issues it.
C The search backend returns non-deterministic result ordering, so the model cannot tell that it has already seen these results and keeps sampling for a stable answer set.
D The model's temperature is too high, causing it to re-explore the same action instead of progressing, since sampling noise is a common cause of non-convergent loops.
正确 B TS 1.1:tool results 必须被追加进对话历史,模型才能在下一轮据此推理。题干精确地指出循环「append 了 assistant message,然后发下一个请求」——中间少的正是 tool_result 那一步。「重复调用同一个工具、参数相同、工具本身运转正常」是这个 bug 的教科书症状:在模型看来那次调用从未发生过。
D4 甩锅错位 违反 P1 A :描述重叠会导致误选 (选了错的工具),不会导致同一个工具带同样参数重复六次 。而且题干说工具执行成功——被选中的工具是对的。TS 2.1 那条规律本身成立,只是它解释的是另一种故障形态。
D4 甩锅错位 违反 P1 C :把责任推给运转正常的下游后端。排序不稳定影响的是结果一致性,前提是模型看得到 结果——而这里它根本看不到。
D3 治标不治本 违反 P1 D :调采样参数是在循环结构性缺陷之上做微调。即使 temperature 降到 0,模型看不到工具结果时依然会重发同一个请求——这时它反而会更稳定地重复。
ch02 · Q3 · 准则 P5 + P2
Your support agent currently runs a hard-coded sequence: always call get_customer, then always lookup_order, then always check_policy, then answer. It handles the "where is my order" flow well, but for billing disputes it wastes two irrelevant calls, and for a new "update shipping address" request type it cannot proceed at all without a code change. What is the right architectural change?
A Extend the decision tree with a request-type branch at the top, adding a new hard-coded tool sequence for each supported request type.
B Keep the fixed sequence but make each tool return early with a no-op result when it is not relevant to the current request.
C Move to a model-driven agentic loop where Claude selects tools per iteration from clear tool descriptions, and add a programmatic prerequisite only where ordering is genuinely mandatory (identity verification before financial operations).
D Keep the fixed sequence and add a classifier that predicts the request type before the agent starts, so the pipeline can be selected in advance.
正确 C TS 1.1 的模型驱动 vs 预配置决策树:请求形态多样且无法穷举时,默认答案是让模型按上下文推理该调哪个工具。同时 TS 1.4 给出配套约束——真正需要确定性的顺序(身份验证先于资金操作)用程序化前置条件锁住,而不是把整个 agent 退回状态机。这是「保留灵活性 + 只在必要处上闸门」的标准形态。
D3 治标不治本 违反 P1 A :根因是「路径无法穷举」,每加一种请求类型就加一条分支,正好把根因固化成了维护负担。题干已经用「新请求类型没改代码就走不通」证明了这一点。
D3 治标不治本 违反 P1 B :no-op 只是让浪费的调用变便宜,顺序仍然写死,新请求类型仍然走不通。碰的是症状(无关调用),不是根因(决策权在代码里)。
D2 过度工程 违反 P2 D :在轻量手段(让模型自己选工具)尚未使用时,先部署一个需要标注数据与训练维护的分类器——与官方样题 Q3 的 C 选项同一工艺。而且它没解决问题:分类器只是把决策树的入口换了个实现。
ch02 · Q4 · 准则 P7
Your automated review agent processes a pull request touching 11 files in the billing module. Running it as a single request produces uneven output: three files get detailed line-level findings, six get one-sentence summaries, and one dependency-injection pattern is flagged as unsafe in invoice.ts but explicitly approved in refund.ts. How should you restructure the review?
A Increase max_tokens on the review request so the model has room to produce equally detailed output for every file.
B Add an instruction to the prompt requiring the model to spend equal analytical effort on each file and to check its findings for internal consistency before responding.
C Split into a per-file local analysis pass for each file, plus one separate cross-file integration pass focused on data flow between the files.
D Run the same full-PR review three times and report only findings that appear in at least two runs, which removes the inconsistent ones.
正确 C TS 1.6 / TS 4.6:深浅不一 + 同一模式两处结论矛盾,是注意力稀释的教科书症状。解法是拆成两类关注点不同的 pass——逐文件 local pass 保证深度一致,单独的 cross-file integration pass 抓跨文件数据流。这也是官方样题 Q12 的正确答案形态。
D3 治标不治本 违反 P1 A :输出长度上限不是瓶颈——题干的症状是注意力分配 不均,不是写不下。给更多输出空间,模型依然会把注意力集中在前后几个文件上。
D6 概率当保证 违反 P8 B :「请对每个文件投入同等精力」「回答前自查一致性」正是 TS 4.1 点名的模糊指令——它不改变输入结构,只是要求模型克服一个结构性限制。同 session 内的自查还叠加了 TS 4.6 的 self-review 局限。
D2 过度工程 违反 P7 D :三跑取交集把成本翻三倍,且会压掉 只被间歇性发现的真 bug——恰恰是最有价值的那类发现。官方样题 Q12 的 D 选项同一工艺。
ch02 · Q5 · 准则 P5
You are asked to add comprehensive test coverage to a 12-year-old payments codebase with no existing tests and no documentation. You do not know the module boundaries, and early probing shows the call graph is deeper than expected. Which decomposition strategy fits this task?
A Write a complete test plan up front listing every module and its test cases, then execute the plan file by file in order.
B Load the entire codebase into context first so the agent can reason about all of it at once, then generate tests in a single pass.
C Process files in alphabetical order, generating tests for each file as it is read, so coverage grows uniformly and progress is easy to track.
D Use dynamic decomposition: first map the codebase structure, then identify high-impact areas, then build a prioritized plan that is revised as dependencies are discovered.
正确 D TS 1.6:开放式任务("add comprehensive tests to a legacy codebase" 是 guide 的原例)应当用动态自适应分解——先测绘结构、再定位高影响区、再生成随依赖发现调整的优先级计划。题干的两个信号(不知道模块边界、调用图比预期深)正是「中间发现会改变计划」的标志。
D3 治标不治本 违反 P5 A :固定顺序流水线适配的是可预测 的多方面任务。这里前提不成立——写一份「完整」计划的前提是你已经了解代码库,而题干明说你不了解。
D2 过度工程 违反 P7 B :上来读全库既触发 TS 2.5 点名的反模式(应当 Grep 找入口再 Read 顺 import 追踪),又会在生成任何测试之前耗尽 context——中段内容还会因 lost-in-the-middle 被漏掉。
D3 治标不治本 违反 P1 C :字母序让进度好看,但与「哪里最该有测试」完全无关。它绕开了这个任务真正的难点——识别高影响区。
ch02 · Q6 · 准则 P5
Your team is building an agent that produces a monthly compliance report. The workflow is genuinely fixed: pull last month's transactions, classify each against the policy list, compute aggregates, then render the report. Each step's output is the next step's input, and the steps never vary. A colleague proposes replacing the current fixed pipeline with dynamic decomposition "for flexibility." How should you respond?
A Agree — dynamic decomposition is the more capable pattern and subsumes fixed pipelines, so it is the safer default for any multi-step workflow, and it costs nothing when the plan it produces matches the fixed order.
B Disagree — the workflow is predictable and its aspects are known in advance, which is exactly what prompt chaining is for; dynamic decomposition adds adaptivity that has nothing to adapt to.
C Agree, but only for the classification step, running the other three steps as a fixed chain and the classification step as a dynamically decomposed subtask, since per-transaction judgement difficulty varies the most there.
D Disagree — dynamic decomposition requires the coordinator's allowedTools to include "Task", which is not available in single-agent workflows, so adopting it would mean restructuring the pipeline into a coordinator plus subagents.
正确 B TS 1.6 的选择规则是双向的:固定顺序流水线(prompt chaining)适配可预测的多方面 工作流,动态分解适配开放式调查 。题干把「步骤固定、每步输出是下一步输入、从不变化」写得很明确——这是 prompt chaining 的定义域。P5 机制匹配负载在这里反向使用:更「高级」的机制用在不需要它的负载上,就是复杂度干扰项。
D2 过度工程 违反 P5 A :「更强的模式总是更安全的默认」是典型的过度工程论证。两种分解模式是并列适配不同负载 的,不存在包含关系。「反正结果一样就不亏」也不成立——动态分解每轮都要付出规划轮次的 token 与不确定性,固定链路则是可复现、可测试的。
D2 过度工程 违反 P5 C :混合方案在简单的「各配各的」方案存在时引入不必要的复杂度(同 ch01 P5 边界与官方样题 Q11 的 D 选项)。分类步骤的输入输出同样固定,没有需要适应的中间发现。「单条判断难度浮动」说的是每条交易 的判断难易,而动态分解适配的是子任务无法预先枚举 ——两回事。
D1 凭空捏造 违反 P1 D :结论碰巧偏向「不改」,理由却是编的。allowedTools 含 "Task" 是 coordinator spawn subagent 的要求(TS 1.3),与动态分解不是一回事——动态分解完全可以在单 agent 内进行。理由错的选项即使结论对也是错的。
ch02 · Q7 · 准则 P1 + P6
A code-exploration agent occasionally exceeds your cost guard. Reviewing traces, you find sessions where stop_reason is "tool_use" for 30+ consecutive iterations, each calling Grep with progressively broader patterns that return thousands of matches. The loop itself is implemented correctly against stop_reason. What is the appropriate response?
A Replace the stop_reason check with a maximum of 8 iterations, since the loop demonstrably does not converge on its own and a hard cap bounds the spend deterministically.
B Have the agent evaluate after each iteration whether it has "enough information" and stop when it self-reports sufficiency, so the loop ends when the task is done rather than at a fixed budget.
C Keep stop_reason as the primary stopping mechanism, add an iteration cap purely as a runaway guard, and fix the actual driver — trim the oversized Grep results before they enter context so the model can converge.
D Terminate the session as failed whenever a single tool result exceeds a size threshold, so runaway explorations cannot accumulate cost, and an oversized result is a measurable signal that exploration has gone off track.
正确 C 两条线索合起来读:循环实现是对的(所以别动主停机制),而每轮返回数千条匹配——TS 5.1 点名的「tool results 不成比例吃 token」。模型看不清结果就只能继续泛化搜索。正确做法是保留 stop_reason 主停 + 上限做兜底护栏(TS 1.1 允许的用法),再修真正的驱动因素:裁剪冗长工具输出(TS 5.1)。
D3 治标不治本 违反 P1 A :把兜底护栏提升为主停机制,正是 TS 1.1 的反模式。而且它只是让失败更早发生——8 轮之后任务照样没完成,只是账单小一点。
D6 概率当保证 违反 P8 B :自报「信息够了吗」是未校准的自我评估,TS 5.2 明确指出自报 confidence 不是复杂度的可靠代理;同时它把确定性的 stop_reason 换成了一个概率性判断,方向正好反了。
D5 吞错或炸链 违反 P6 D :单个工具结果超标就判整个会话失败,是典型的「炸链」——单点异常终止全流程,且丢掉了已完成的探索成果。正确处理是裁剪结果继续推进,而不是引爆。