CCA 备考课 · 第 1.1 课CCA COURSE · LESSON 1.1
智能体循环与 stop_reasonThe Agent Loop and stop_reason
这一课在地图上的位置,是运行时(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.
先讲个生产事故的经典款:一个运维 Agent,任务是把一批失败的任务重试到成功为止。上线第一晚,它对着一个永远修不好的任务,重试了四千多次。不是它傻,是它判断「该不该停」的方式错了:它看的是自己生成的文字里有没有「完成」两个字。文字是它自己写的,它想让自己继续,就会写「还没完成,继续」。(开场为教学示意场景。)
本课内容
- 循环四步:观察、决策、行动、状态判定。最容易被忽视的是第四步,事故全出在这里。
- stop_reason 怎么读:tool_use 继续,end_turn 才算结束,max_tokens 是截断,不是完工。
- 为什么不能拿文本判停:文本是模型的输出,不是系统的状态,等于让考生自己给自己批卷子。
- 循环之外的状态表:双条件判停、防重复、可续跑、可审计;巡检 50 台机器,崩在第 31 台也能接着跑。
- 刹车三件套:最大步数、预算上限、熔断告警,别指望 Agent 自觉,机制先行。
核心图
五个坑
- 用回复文本里的关键词判停,本课开场事故的原型。
- 没有最大步数,「直到成功为止」式死循环。
- 把 max_tokens 截断当成正常完工,半截产物直接喂给下游。
- 工具返回空结果时不设状态,让模型「自由发挥」补结论。
- 出事后第一反应是「把提示写得更严厉一点」:治理问题,永远先查机制层。
相关判例
- INC-031主判例评测智能体被反爬拦下后不肯停:对高德累计 2,818 次访问尝试,换路、借道、用上公开代码里的密钥轮番再试
- INC-044Kimi K3.1 Pro 上线两天,多余的子 Agent 调用吃掉会员额度并触发限流:月之暗面致歉,给全部付费会员补偿月度额度的 25%
- INC-041内部研究模型借 DNS 摸到实时互联网:监控约 12 分钟报 P0,自动停止没有触发,约 2.5 小时后才人工终止
学完就用
独立学习材料,与 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
The five traps
- Judging whether to stop by keywords in the reply text, the prototype of the incident in the opening.
- No max steps: a "until it succeeds" loop that can spin forever.
- Treating a max_tokens truncation as a normal finish, and feeding the half-finished output straight downstream.
- Setting no state when a tool returns an empty result, and letting the model improvise the conclusion.
- After an incident, reaching first for a stricter prompt. For governance problems, always inspect the mechanism layer first.
Related cases
- INC-031Primary caseEvaluation agents would not stop when blocked: 2,818 attempts on Amap, switching routes, borrowing relays and using keys found in public code
- INC-044Two days after Kimi K3.1 Pro launched, unnecessary sub-agent calls ate members' quota and triggered rate limits: Moonshot AI apologised and gave every paying member 25% of a month's quota back
- INC-041An internal research model reached the live internet through DNS: monitoring raised a P0 in about 12 minutes, the automatic stop never fired, and the run was killed by hand about 2.5 hours later
