Clipping X

Getting started with loops 循环入门指南

by @ClaudeDevs 原文 ↗ 原载于 2026-07-07
Created: 2026-07-09

图像

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. 现在大家都在讨论“设计循环”,而不是简单地给你的编程助手发提示。如果你花点时间在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. 我们将介绍主要的循环类型、各自的适用场景,以及在管理令牌消耗的同时保持代码质量的方法。并非所有任务都需要复杂循环;请从最简单的方案入手,有选择地运用这些模式。

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.管理使用方式:编写具体提示词,并利用技能改进验证过程,减少交互回合数。

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.”使用管理方法:设定具体的完成标准和明确的轮次上限,例如“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: 举例说明:

/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: 针对这类场景,您可以在运行Claude时使用`/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.停止条件:每个任务在目标达成后即退出。例程本身将持续运行直至您手动关闭。
  • 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 auto mode and dynamic workflows (research preview) can be composed into a loop for long-running work.上述基础组件可与 Claude Code 的其他功能(如自动模式和动态工作流(研究预览版))组合成循环,用于执行长期运行的任务。

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: 综合来看,提示词可以这样编写:

/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 skills.让Claude能够自我验证:通过技能编码,明确你和团队期望的优质标准。
  • 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. 当个别结果未达到标准时,不要止步于修复单个问题,尝试将其编码到系统中,以改进所有未来的迭代。

Managing token usage 管理令牌使用量

To manage token usage, loops should have clear boundaries: 要管理令牌使用量,循环应设置明确的边界:

  • 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` 可显示到目前为止的轮次和令牌使用量;`/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. 一旦有了想法,就运行循环,观察结果——比如在哪些地方卡住了或用力过猛——不要害怕迭代优化。

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 文档中关于并行运行代理的内容,以及循环、计划、目标和动态工作流相关页面。

This article was written by @delba_oliveira 本文由 @delba\_oliveira 撰写。

输入关键词开始搜索