Loops = agents repeating cycles of work until a stop condition is met.
loop 就是 agent 反复循环工作,直到满足停止条件。
Claude Code 团队按四个维度区分 loop:如何触发、如何停止、用哪个原语、适合哪类任务。
Turn-based → goal-based → time-based → proactive.
四种 loop 由浅入深,不是所有任务都要复杂 loop。
从最简单的方案入手,有选择地使用这些模式;先别过度设计。
You can improve verification by encoding your manual steps as a SKILL.md.
逐轮 loop 里,把手动步骤写成 skill 能提升自我验证。
检查越量化(测试数、分数阈值),Claude 越容易自查,轮次越少。
/goal lets an evaluator model keep Claude working until done.
/goal 定义"完成",评估模型盯着它做到目标或撞轮次上限。
Claude 不必自行判断"够好了没"而提前收工;确定性标准最有效。
/loop runs locally; /schedule moves the loop to the cloud.
定时 loop:/loop 跑本地,/schedule 搬云端做成 routine。
适合周期性工作或对接外部系统;拉长间隔或改成事件驱动来省用量。
Compose primitives + auto mode + workflows for proactive loops.
主动 loop 把各原语 + auto mode + dynamic workflows 组合成无人值守流。
质量靠周边系统(整洁代码库、第二个 review agent);控量靠清晰边界与选对模型。
There's a lot of talk right now about "designing loops" instead of prompting your coding agent. If you spend some time on X trying to pin down what a loop actually is, you'll come across multiple different answers.
眼下有很多关于"设计循环(loop)"而非直接给编码 agent 写 prompt 的讨论。如果你花点时间在 X 上想弄清 loop 到底是什么,会遇到五花八门的答案。
On the Claude Code team, we define loops as agents repeating cycles of work until a stop condition is met. We categorize a few different types of loops based on:
在 Claude Code 团队,我们把 loop 定义为:agent 不断重复一轮轮工作,直到满足某个停止条件。我们按以下几个维度来区分不同类型的 loop:
We'll cover the main loop types, when to use each, and how to maintain code quality while managing token usage. Not all tasks require complex loops; start with the simplest solution and use these patterns selectively.
我们会讲清主要的 loop 类型、各自何时使用,以及如何在控制 token 用量的同时保持代码质量。并非所有任务都需要复杂的 loop;从最简单的方案入手,有选择地使用这些模式。
Every prompt you send starts a manual loop with you directing each turn. Claude gathers context, takes action, checks its work, repeats if needed, and responds. We call this the agentic loop.
你发出的每一条 prompt 都会启动一个由你指挥每一轮的手动 loop。Claude 收集上下文、采取行动、检查自己的成果、必要时重复,然后给出回复。我们称之为 agentic loop(智能体循环)。
For example, ask Claude to create a like button. It reads your code, makes the edit, runs the tests, and hands back something it believes works. You then manually check the work, and write the next prompt.
举个例子,让 Claude 做一个点赞按钮。它会读你的代码、做出修改、运行测试,然后交回一个它认为能用的成果。接着由你手动检查,再写下一条 prompt。
You can improve the verification step by encoding your manual steps as a SKILL.md so Claude can check more of its own work, end-to-end. (For choosing between skills, hooks, and subagents for this kind of automation, see our guide to steering Claude Code.)
你可以把手动步骤固化成一个 SKILL.md 来改进验证环节,让 Claude 能端到端地检查更多自己的工作。(在这类自动化中如何在 skill、hook 和子 agent 之间取舍,见我们的《steering Claude Code》指南。)
This should include tools or connectors to allow Claude to see, measure or interact with the result. The more quantitative the checks are, the easier it is for Claude to self-verify.
这里应当包含让 Claude 能看到、测量或与结果交互的工具或连接器(connector)。检查越量化,Claude 就越容易自我验证。
For example, in your SKILL.md file you may specify:
例如,你的 SKILL.md 文件里可以这样写:
---
name: verify-frontend-change
description: Verify any UI change end-to-end before declaring it done.
---
# Verifying frontend changes
Never report a UI change as complete based on a successful edit alone. Verify it the way a human reviewer would:
1. Start the dev server and open the edited page in the browser.
2. Interact with the change directly. For a new control (button, input, toggle): click it, confirm the expected state change, and screenshot before/after.
3. Check the browser console: zero new errors or warnings.
4. Use the Chrome Devtools MCP, run a performance trace and audit Core Web Vitals.
If any step fails, fix the issue and rerun from step 1 — do not hand back partially verified work.
Sometimes, a single turn is not enough, especially for more complex tasks. Agents do better when they can iterate. You can extend how long Claude keeps iterating by defining what done looks like with /goal.
有时一轮并不够,尤其是较复杂的任务。agent 在能够迭代时表现更好。你可以用 /goal 定义"完成"是什么样子,从而延长 Claude 持续迭代的时间。
When you define the success criteria, Claude doesn't have to make a determination on what is "good enough" and end the loop early. Each time Claude tries to stop, an evaluator model checks your condition and sends it back to work until the goal is met or a number of turns you define is reached.
当你定义了成功标准,Claude 就不必自行判断怎样才算"足够好"而提前结束 loop。每当 Claude 想要停止,一个评估模型(evaluator model)都会核对你的条件,并把它送回去继续工作,直到目标达成、或达到你设定的轮次上限。
This is why deterministic criteria, such as number of tests passed or clearing a certain score threshold, are so effective.
这正是为什么确定性的标准——比如通过的测试数量,或跨过某个分数阈值——如此有效。
For example:
例如:
/goal get the homepage Lighthouse score to 90 or above, stop after 5 tries.
Some agentic work is recurring: the task stays the same and only the inputs change. For example, summarizing Slack messages every morning. Other work depends on external systems, and a simple way to interface with one is to check it on an interval and react to what changed. For example, a PR which may receive code reviews or fail CI.
有些 agent 工作是周期性的:任务不变,只有输入在变。比如每天早上汇总 Slack 消息。另一些工作依赖外部系统,而与外部系统对接的一个简单办法,就是按间隔去检查它,并对变化做出反应。比如一个可能收到 code review 或 CI 失败的 PR。
For these, you can trigger when Claude runs with /loop which re-runs a prompt on an interval. For example:
对这类工作,你可以用 /loop 来控制 Claude 何时运行——它会按间隔重跑一条 prompt。例如:
/loop 5m check my PR, address review comments, and fix failing CI
/loop runs on your computer, so if you turn it off, it stops. You can move the loop to the cloud by creating a routine with /schedule.
/loop 跑在你自己的电脑上,所以一旦你关掉它,它就停了。你可以用 /schedule 创建一个 routine,把 loop 搬到云端。
The primitives above, along with other Claude Code features like auto mode and dynamic workflows (research preview) can be composed into a loop for long-running work.
上面这些原语,连同 Claude Code 的其他特性——比如 auto mode 和 dynamic workflows(研究预览版)——可以组合成一个用于长时间运行工作的 loop。
For example, to handle incoming feedback, you can use:
例如,要处理持续涌入的反馈,你可以用:
/schedule (research preview) to run a routine that checks for new reports/schedule(研究预览版):运行一个 routine,检查是否有新的上报。/goal to define what done looks and skills to document how to verify it/goal:定义"完成"的样子;并用 skill 记录如何验证。Putting it together, a prompt could look like this:
把它们拼在一起,一条 prompt 可能长这样:
/schedule every hour: check #project-feedback for bug reports. /goal: don't stop until every report found this run is triaged, actioned, and responded to. When fixing a bug, use a workflow to explore three solutions in parallel worktrees and have a judge adversarially review them.
The quality of a loop's output depends on the system around it. When designing the system:
一个 loop 产出的质量,取决于它周围的这套系统。设计这套系统时:
/code-review skill or Code Review for Github.用第二个 agent 做 code review:一个上下文全新的审查者偏见更少,也不受主 agent 推理过程的影响。你可以用内置的 /code-review skill,或用于 GitHub 的 Code Review。When an individual result doesn't meet the standard, don't stop at fixing the individual issue, try to encode it to improve the system for all future iterations.
当某个单独的结果不达标时,别止步于修好这一个问题——试着把它固化下来,为将来所有的迭代改进整个系统。
To manage token usage, loops should have clear boundaries:
要管好 token 用量,loop 应当有清晰的边界:
/usage command breaks down recent usage by skills, subagents, and MCPs, /goal with no arguments shows number of turns and token usage so far, /workflows shows each agent's token usage and you can stop an agent at any time.审视用量:/usage 命令会按 skill、子 agent 和 MCP 拆解近期用量;不带参数的 /goal 会显示目前的轮次和 token 用量;/workflows 会显示每个 agent 的 token 用量,而且你随时可以停掉某个 agent。To summarize:
总结一下:
| Loop循环类型 | You hand off你交出去的 | Use it when何时使用 | Reach for用什么 |
|---|---|---|---|
| Turn-based逐轮 | The check检查(验证) | You're exploring or deciding你在探索或做决定 | Custom verification skills自定义验证 skill |
| Goal-based目标驱动 | The stop condition停止条件 | You know what done looks like你清楚"完成"长什么样 | /goal/goal |
| Time-based定时 | The trigger触发 | The work happens outside your project on a schedule工作在你项目之外、按排期发生 | /loop, /schedule/loop、/schedule |
| Proactive主动 | The promptprompt(整条指令) | The work is recurring and well-defined工作是周期性且定义明确的 | All of the above, and dynamic workflows以上全部,外加 dynamic workflows |
To get started with loops, look at the work you already do. Pick one task where you're the bottleneck and ask which piece you could hand off: can you write the verification check? Is the goal clear enough? Does the work arrive on a schedule?
要上手 loop,先看看你已经在做的工作。挑一个你正是瓶颈的任务,问问自己哪一块可以交出去:你能写出那个验证检查吗?目标是否足够清晰?这份工作是否按某个排期到来?
Once you have an idea, run the loop, observe the results like where it stalls or over-reaches, and don't be afraid to iterate on it.
一旦有了想法,就把 loop 跑起来,观察结果——比如它在哪里卡住、在哪里用力过猛——并且别怕反复迭代它。
For more information, read the Claude Code docs on running agents in parallel, as well as the loop, schedule, goal, and dynamic workflows pages.
想了解更多,可以阅读 Claude Code 文档中关于并行运行 agent 的部分,以及 loop、schedule、goal 和 dynamic workflows 各页面。