Learn how the Claude Code team defines agentic loops, with practical guidance on progressing from turn-based to goal-based, time-based, and proactive loops—and when to use each.
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. 近来江湖上议论纷纷,言必称“设计循环”(designing loops),反倒不再只提如何给编码代理下指令了。若你在 X 上细细寻访,想弄清“循环”究竟为何物,怕是会听到五花八门的答案。
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 团队对此有番定见:所谓循环,便是让代理反复施展手段,直至达成预设的止境。我们将这些循环分门别类,考量标准有四:
How they are triggered其一,如何触发;
How they are stopped其二,如何止步;
What Claude Code primitive is used其三,调用了何种 Claude Code 原语;
What type of task is most appropriate for each.其四,何种任务最宜用此法。
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. 下文便要拆解这几种主流循环,细说其用法,并教你如何在把控 Token 消耗的同时,稳住代码质量。并非所有任务都需大费周章,当从最简处入手,择需而用。
Stop criteria: Claude judges it has completed the task or needs additional context.止步条件:Claude 自判任务已成,或需补充更多背景信息。
Best used for: Shorter tasks that are not part of a regular process or schedule.适用场景:琐碎、非常规或无固定周期的短任务。
Managed usage by: Write specific prompts and improve verification using skills to reduce the number of turns.管控之道:写好指令,并用“技能”(skills)加强校验,以此减少往返轮次。
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.你每发一条指令,便开启了一个手动循环,由你亲自把控每一轮的进退。Claude 收集背景、付诸行动、自查成果、必要时反复迭代,最后给出回复。此乃代理循环之本。
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 加个“点赞”按钮。它读代码、作修改、跑测试,交出成品。你过目之后,再下下一道指令。
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. 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. 你可将手动校验的步骤编入 SKILL.md,化作 Claude 的一项“技能”,让它能更全面地自查。这其中应包含工具或连接器,以便 Claude 能观测、度量并与结果交互。校验越是量化,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.
Goal-based loop (/goal)目标循环 (/goal)
Triggered by: A manual prompt in real-time.触发方式:实时手动指令。
Stop criteria: Goal achieved OR maximum number of turns reached.止步条件:目标达成,或轮次耗尽。
Best used for: Tasks that have verifiable exit criteria.适用场景:有明确验收标准之任务。
Managed usage by: Setting a specific completion criteria and explicit turn caps, “stop after 5 tries.”管控之道:定好验收标准,并设下轮次上限,如“试五次即止”。
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.遇上复杂任务,单轮往返往往不够,需让代理反复磨砺。你可用 /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 便不必纠结于“是否足够好”而过早收手。每当它想停下,评估模型便会核对你的条件,若未达标,便将其遣回继续,直至目标达成或轮次耗尽。
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.
Time-based loop (/loop and /schedule)定时循环 (/loop 与 /schedule)
Triggered by: A specified time interval.触发方式:按既定时间间隔。
Stop criteria: You cancel it, or the work completes (the PR merges, the queue is empty). 止步条件:你手动取消,或工作完成(如 PR 合并、队列清空)。
Best used for: For recurring work, or interfacing with external environments / systems. 适用场景:周期性工作,或与外部环境/系统交互。
Managed usage by: Set longer intervals or react based on events rather than time.管控之道:拉长间隔,或依事件触发而非死守时间。
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.有些代理工作是常态化的:任务不变,仅输入有别。如每日清晨汇总 Slack 消息。另一些则依赖外部系统,最简单的交互法便是定期巡检,见变则动。如 PR 收到代码评审或 CI 失败时。
For these, you can trigger when Claude runs with `/loop` which re-runs a prompt on an interval. For example:对此,可用 `/loop` 按固定间隔重跑指令。例如:
/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` 创建常规任务。
Proactive loops主动循环
Triggered by: An event or schedule, with no human in real time. 触发方式:由事件或日程触发,无需人工实时介入。
Stop criteria: Each task exits when its goal is met. The routine itself runs until you turn it off. 止步条件:任务达成即止,Routine 本身则持续运行直至你手动关闭。
Best used for: Recurring streams of well-defined work: bug reports, issue triage, migrations, dependency upgrades, etc.适用场景:定义明确的循环工作流:如处理 Bug 报告、工单分拣、迁移、依赖升级等。
Managed usage by: Routing routines to smaller, faster models and using the most capable model for judgment calls. 管控之道:Routine 委派给轻快模型,关键决断则交由最强模型。
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)及动态工作流(研究预览版),便可编织成处理长久任务的循环。
For example, to handle incoming feedback, you can use:譬如处理反馈,可组合如下:
`/schedule` (research preview) to run a routine that checks for new reports用 `/schedule` 巡检新报告;
`/goal` to define what done looks and skills to document how to verify it用 `/goal` 明确验收标准,并以“技能”记录校验之法;
Dynamic workflows to orchestrate agents that triage each report, fix it, and review the fix用动态工作流调度代理,分拣、修复并评审报告;
Auto mode so the routine runs without stopping to ask for permission开启自动模式,令流程无需请示即可顺畅运行。
Putting it together, a prompt could look like this:综上所述,指令可写成这样:
/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.
Maintaining code quality维护代码质量
The quality of a loop’s output depends on the system around it. When designing the system:循环产出的好坏,全系于系统之周全。设计系统时,当记:
Keep the codebase itself clean: Claude follows patterns and conventions that already exist in your codebase.保持代码库整洁:Claude 极善模仿,会顺着你现有的规矩与范式行事。
Give Claude a way to verify its own work: Encode what good looks like for you and your team with skills.授以自查之法:用“技能”将团队公认的准则编码,让它知晓何为好代码。
Make docs easy to reach: Frameworks and libraries docs have up-to-date best practices.文档触手可及:确保框架与库的文档即时更新,方能体现最佳实践。
Use a second agent for code reviews: A reviewer with fresh context is less biased and not influenced by the main agent’s reasoning. You can use the built-in `/code-review` skill or Code Review for Github.引入第二代理评审:旁观者清,评审者不受主代理逻辑牵绊。可调用内置的 `/code-review` 技能或 Github 代码评审。
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.若单次结果不遂人意,切莫只改这一个 Bug,当将其编码入库,以优化整个系统,惠及后续迭代。
Managing token usage管理 Token 消耗
To manage token usage, loops should have clear boundaries: 为省 Token,循环须有明确边界:
Choose the right primitive and model for the job: Smaller tasks don’t need multiple agents or loops. Some tasks can use cheaper and faster models. 择器而用:小任务无需动用多代理或复杂循环,轻快模型足矣。
Define clear success and stop criteria: Be specific about what done looks like so Claude can arrive at the solution sooner (but not too soon). 止步条件清晰:界定好“功成”之状,Claude 方能直奔目标,不至于兜转过久。
Pilot before a large run: Dynamic workflows can spawn hundreds of agents. Gauge usage on a smaller slice of the work first.先试后行:动态工作流一动便是百个代理,先取一小部分试水,摸清消耗再铺开。
Use scripts for deterministic work: Running a script is cheaper than reasoning through the steps. For example, a PDF skill can ship a form-filling script that Claude runs each time, instead of re-deriving the code.脚本代劳:确定性的活计,跑脚本远比推理省力。如 PDF 技能可内置表单填充脚本,Claude 只需调用,无需次次重推代码。
Don’t run routines more often that you need to: Match the interval to how often the thing you’re watching changes按需触发:观察频率当与变动频率相匹配。
Review usage: The `/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` 指令可查各技能、子代理及 MCP 的消耗详情;`/goal` 不加参数可看当前轮次与 Token;`/workflows` 则能总览各代理消耗,随时可叫停。
Getting started起步入门
To summarize: 总而言之:
Loop
You hand off
Use it when
Reach for
Turn-based
The check
You're exploring or deciding
Custom verification skills
Goal-based
The stop condition
You know what done looks like
/goal
Time-based
The trigger
The work happens outside your project on a schedule
/loop, /schedule
Proactive
The prompt
The work is recurring and well-defined
All of the above, and 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?欲用循环,先审视手头琐事。挑一件你觉得最耗神、最易卡壳的任务,问问自己:哪部分能交出去?能否写出校验准则?目标是否够明确?任务是否按期而至?
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.有了念头,便去跑跑看。观察它在哪儿停滞、在哪儿越界,莫怕迭代,反复打磨便是。