CCA 备考课 · 第 1.1 课CCA COURSE · LESSON 1.1

智能体循环与 stop_reasonThe Agent Loop and stop_reason

第一域 · 智能体架构与编排 · 编排层 · 第 3 关 · 编排 · 时长 11:20 · 英文授课Domain 1 · Agentic Architecture and Orchestration · Orchestration layer · Gate 3, Orchestration · 11:20 · Taught in English

这一课在地图上的位置,是运行时(D1)的心脏:编排拓扑、任务交接、重试降级、护栏,全都建在这个循环上面。On the map, this lesson sits at the heart of the runtime, Domain 1: orchestration topologies, task handoffs, retries and fallbacks, and guardrails are all built on top of this loop.

CCA 1.1 · Watch on YouTube

YouTube

CCA 1.1 · 小红书 RedNote

小红书

先讲个生产事故的经典款:一个运维 Agent,任务是把一批失败的任务重试到成功为止。上线第一晚,它对着一个永远修不好的任务,重试了四千多次。不是它傻,是它判断「该不该停」的方式错了:它看的是自己生成的文字里有没有「完成」两个字。文字是它自己写的,它想让自己继续,就会写「还没完成,继续」。(开场为教学示意场景。)

本课内容

  • 循环四步:观察、决策、行动、状态判定。最容易被忽视的是第四步,事故全出在这里。
  • stop_reason 怎么读:tool_use 继续,end_turn 才算结束,max_tokens 是截断,不是完工。
  • 为什么不能拿文本判停:文本是模型的输出,不是系统的状态,等于让考生自己给自己批卷子。
  • 循环之外的状态表:双条件判停、防重复、可续跑、可审计;巡检 50 台机器,崩在第 31 台也能接着跑。
  • 刹车三件套:最大步数、预算上限、熔断告警,别指望 Agent 自觉,机制先行。

核心图

观察决策 行动状态判定 Agent Loop 状态判定只认 stop_reason tool_use继续:执行工具,结果喂回end_turn结束:这一轮真正干完max_tokens截断,不是完工 状态表在循环之外:进度外存,信号与进度双条件都对上,才算真结束。刹车三件套(最大步数 · 预算上限 · 熔断告警)是给循环上的物理保险丝。
图:Agent 循环四步与状态判定。判停只认 stop_reason;状态表在循环之外对进度;刹车三件套是物理保险丝。

五个坑

  1. 用回复文本里的关键词判停,本课开场事故的原型。
  2. 没有最大步数,「直到成功为止」式死循环。
  3. 把 max_tokens 截断当成正常完工,半截产物直接喂给下游。
  4. 工具返回空结果时不设状态,让模型「自由发挥」补结论。
  5. 出事后第一反应是「把提示写得更严厉一点」:治理问题,永远先查机制层。
循环听信号,不听文本;刹车靠机制,不靠自觉。

相关判例

学完就用

这一课落在编排层 · 第 3 关 · 编排。把这一关放到你自己的系统上量一遍:免费 17 项快速自检,答完当场出报告,看你最弱的三关。

独立学习材料,与 Anthropic 无隶属关系。Claude 与 Claude Certified Architect 为 Anthropic 的商标;本课为独立备考课程,不授予、也不暗示任何认证。Independent study material, not affiliated with Anthropic. Claude and Claude Certified Architect are trademarks of Anthropic; this course is an independent preparation course and does not grant or imply certification.

Let me start with a classic production incident. An operations agent had one job: retry a batch of failed tasks until they succeeded. On its first night live, it retried one task, a task that could never be fixed, more than four thousand times. The agent was not stupid. It was deciding whether to stop in the wrong way: it read its own generated text, looking for the word "done". But the text was written by the agent itself: whenever it wanted to keep going, it simply wrote "not done yet, keep going". (Illustrative example.)

What this lesson covers

  • The loop’s four steps: observe, decide, act, and the state check, the step most systems write as an afterthought, and where the incidents happen
  • Reading stop_reason: tool_use continues the loop, end_turn is the only real finish, and max_tokens means truncated, not done
  • Why text can never judge stopping: text is the model’s output, not the system’s state; the model would be grading its own exam paper
  • The state table outside the loop: dual-condition stopping, no repeats, resumable, auditable, and an inspection of fifty machines that resumes at machine thirty-one after a crash
  • The brake kit: max steps, a budget cap, and circuit-breaker alerts, fitted as mechanisms, never left to the agent’s self-discipline

Core diagram

ObserveDecide ActState check Agent Loop The state check reads stop_reason only tool_useContinue: run the tool,feed the result backend_turnFinish: this turn isgenuinely finishedmax_tokensTruncated,not done The state table lives outside the loop: stop only when the signal andthe progress agree. The brake kit (max steps, budget cap, circuit-breakeralerts) is the physical fuse on the loop.
The agent loop and its state check. Stopping reads stop_reason only; the state table tracks progress outside the loop; the brake kit is the physical fuse.

The five traps

  1. Judging whether to stop by keywords in the reply text, the prototype of the incident in the opening.
  2. No max steps: a "until it succeeds" loop that can spin forever.
  3. Treating a max_tokens truncation as a normal finish, and feeding the half-finished output straight downstream.
  4. Setting no state when a tool returns an empty result, and letting the model improvise the conclusion.
  5. After an incident, reaching first for a stricter prompt. For governance problems, always inspect the mechanism layer first.
The loop listens to signals, not text. Brakes come from mechanisms, not self-discipline.

Related cases

Put it to work

This lesson sits in the Orchestration layer, at Gate 3, Orchestration. Run that gate against your own system with the free 17-point self-check: it reports on the spot and names your three weakest gates.