文章

对话

图像
Getting started with loops 循环入门
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.目前关于“设计循环”而非单纯提示(prompting)编码智能体的讨论非常多。如果你花时间在 X 上试图弄清楚循环到底是什么,你会看到多种不同的答案。
On the Claude Code team, we define 在 Claude Code 团队,我们将loops as agents repeating cycles of work until a stop condition is met循环定义为智能体重复工作周期直到满足停止条件. We categorize a few different types of loops based on:。我们根据以下几点对几种不同类型的循环进行了分类:
  • 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 使用的同时保持代码质量。并非所有任务都需要复杂的循环;请从最简单的解决方案开始,并有选择地使用这些模式。

Turn-based loops
轮次循环 (Turn-based loops)

图像
  • Triggered by: A user prompt.
    触发方式:用户提示。
  • 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 收集背景信息、采取行动、检查工作成果、根据需要重复,然后做出响应。我们称之为智能体循环(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 例如,让 Claude 创建一个“点赞”按钮。它会读取你的代码、进行编辑、运行测试,并交回它believes认为 works. You then manually check the work, and write the next 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. This should include tools or connectors to allow Claude to 你可以通过将手动步骤编码为 SKILL.md 来改进验证步骤,这样 Claude 就能端到端地自行检查更多工作。这应该包括允许 Claudesee查看, measure测量 or interact交互 with the result. The more quantitative the checks are, the easier it is for Claude to self-verify.结果的工具或连接器。检查标准越量化,Claude 进行自我验证就越容易。
For example, in your SKILL.md file you may specify:例如,在你的 SKILL.md 文件中,你可以指定:
markdownmarkdown
--- 
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.”
    管理方式:设定具体的完成标准和明确的轮数限制,例如“尝试 5 次后停止”。
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 就不必自行判断什么是“足够好”并提前结束循环。每当 Claude 试图停止时,评估模型会检查你的条件,如果未达标,则将其发回继续工作,直到目标达成或达到你定义的轮数为止。
This is why deterministic criteria, such as number of tests passed or clearing a certain score threshold, are so effective.这就是为什么确定性标准(如通过的测试数量或达到特定的分数阈值)如此有效的原因。
For example:例如:
bashbash
/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 消息。其他工作依赖于外部系统,与之交互的一种简单方法是按间隔检查并对变化做出反应。例如,一个可能会收到代码审查或 CI 失败的 PR。
For these, you can trigger when Claude runs with `/loop` which re-runs a prompt on an interval. For example:对于这些任务,你可以使用 `/loop` 触发 Claude 的运行,它会按间隔重新运行提示。例如:
bash
/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.
    停止标准:每个任务在达成目标时退出。例程本身会一直运行,直到你将其关闭。
  • Best used for: Recurring streams of well-defined work: bug reports, issue triage, migrations, dependency upgrades, etc.
    最佳用途:定义明确的重复性工作流:错误报告、问题分类、迁移、依赖项升级等。
  • Managed usage by: Routing routines to smaller, faster models and using the most capable model for judgment calls.
    管理方式:将例程路由到更小、更快的模型,并使用能力最强的模型进行决策。
The primitives above, along with other Claude Code features like 上述原语,连同 Claude Code 的其他功能(如auto mode自动模式 and dynamic workflows 动态工作流(research preview) can be composed into a loop for long-running work.(研究预览版)),可以组合成一个用于长期工作的循环。
For example, to handle incoming feedback, you can use:例如,要处理收到的反馈,你可以使用:
  1. `/schedule` (research preview) to run a routine that checks for new reports
    `/schedule`(研究预览版)运行一个检查新报告的例程
  2. `/goal` to define what done looks and skills to document how to verify it
    `/goal` 定义完成标准,并使用技能记录如何验证它
  3. Dynamic workflows to orchestrate agents that triage each report, fix it, and review the fix
    动态工作流来编排智能体,对每份报告进行分类、修复并审查修复结果
  4. Auto mode so the routine runs without stopping to ask for permission
    自动模式,这样例程运行过程中无需停下来请求许可
Putting it together, a prompt could look like this:综合起来,一个提示可能如下所示:
bash
/schedule every hour: check the project-feedback channel 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 .
    为 Claude 提供验证自身工作的方法:使用技能(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 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.当单个结果未达到标准时,不要只停留在修复该问题上,尝试将其编码以改善系统,从而惠及未来所有的迭代。

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` 显示每个智能体的 Token 使用量,你可以随时停止智能体。

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.一旦有了想法,就运行循环,观察结果(例如它在哪里停滞或过度执行),并且不要害怕对其进行迭代。
For more information, read the Claude Code docs on 欲了解更多信息,请阅读 Claude Code 关于 as well as the 以及, , , and ,和 pages.页面。
This article was written by 本文由
想发布自己的文章?想发布自己的文章?
升级为 Premium升级为 Premium
SW
发布你的回复发布你的回复

没有项目