Glean 拾遗
日刊 /2026-07-09 / Claude Code 循环模式:从交互到自动化

Claude Code 循环模式:从交互到自动化

原文 x.com 收录 2026-07-09 06:01 阅读 9 min
AI 解读

Claude Code 团队官方博客,详细介绍了四种循环模式:基于回合、基于目标、基于时间和主动循环。文章解释了每种模式的触发方式、停止条件、适用场景及 token 管理策略,并提供了具体的 CLI 命令和 SKILL.md 示例。核心观点是:并非所有任务都需要复杂循环,应从最简单的方案开始,逐步为重复性工作设计自动化流水线。对于已在使用或计划使用 Claude Code 进行自动化开发的工程师极具参考价值。

原文 9 分钟
原文 x.com ↗
§ 1

Getting started with loops

开始使用循环

§ 2

Getting started with loops

By @ClaudeDevs · 2026-07-06T19:08:45.000Z

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.

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:

  • How they are triggered
  • How they are stopped
  • What Claude Code primitive is used
  • 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.

开始使用循环

作者:@ClaudeDevs · 2026-07-06T19:08:45.000Z

现在有很多关于“设计循环”而不是提示你的编码智能体的讨论。如果你花点时间在 X 上试图弄清楚循环到底是什么,你会遇到多种不同的答案。

在 Claude Code 团队中,我们将循环定义为智能体重复工作周期直到满足停止条件。我们根据以下因素将循环分为几种不同类型:

  • 触发方式
  • 停止方式
  • 使用的 Claude Code 原语
  • 最适合的任务类型 我们将介绍主要的循环类型、何时使用每种类型,以及如何在管理令牌使用的同时保持代码质量。并非所有任务都需要复杂的循环;从最简单的解决方案开始,有选择地使用这些模式。
§ 3

§ 4
  • Triggered by: A user prompt.
  • Stop criteria: Claude judges it has completed the task or needs additional context.
  • 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.

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.

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.

For example, in your SKILL.md file you may specify:

--- 
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.
  • 触发方式:用户提示。
  • 停止条件:Claude 判断任务已完成或需要更多上下文。
  • 最佳用途:不属于定期流程或计划的较短任务。
  • 使用管理:编写具体提示,并使用技能改善验证,以减少轮次。

你发送的每个提示都会启动一个手动循环,由你指挥每一轮。Claude 收集上下文、采取行动、检查工作、必要时重复,然后响应。我们称之为智能体循环。

例如,让 Claude 创建一个点赞按钮。它会读取你的代码、进行编辑、运行测试,然后返回它认为有效的内容。然后你手动检查工作,并写下下一个提示。

你可以通过将手动步骤编码为 SKILL.md 来改进验证步骤,这样 Claude 就可以端到端地检查更多自己的工作。这应包括工具或连接器,使 Claude 能够看到、测量或与结果交互。检查越定量,Claude 就越容易自我验证。

例如,在你的 SKILL.md 文件中可以指定:

--- 
name: verify-frontend-change 
description: 在声明完成之前端到端验证任何 UI 变更。 
--- 

# 验证前端变更
永远不要仅凭成功编辑就报告 UI 变更完成。要像人类审查者一样验证:

1. 启动开发服务器并在浏览器中打开编辑后的页面。

2. 直接与变更交互。对于新控件(按钮、输入、切换):点击它,确认预期的状态变更,并截图前后对比。

3. 检查浏览器控制台:零个新错误或警告。

4. 使用 Chrome Devtools MCP,运行性能跟踪并审计核心网页指标。

如果任何步骤失败,修复问题并从步骤 1 重新运行——不要交回部分验证的工作。
§ 5

§ 6
  • 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.

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.

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.

  • 触发方式:实时手动提示。
  • 停止条件:达成目标或达到最大轮次。
  • 最佳用途:具有可验证退出条件的任务。
  • 使用管理:设置具体的完成标准和明确的轮次上限,例如“尝试 5 次后停止”。

有时,一轮是不够的,特别是对于更复杂的任务。当智能体能够迭代时,它们表现更好。你可以通过使用 /goal 定义“完成”的样子来延长 Claude 持续迭代的时间。

当你定义成功标准时,Claude 不必判断什么是“足够好”并提前结束循环。每次 Claude 尝试停止时,评估器模型会检查你的条件,并将其送回工作,直到达成目标或达到你定义的轮次。

这就是为什么确定性标准(例如通过的测试数量或超过某个分数阈值)如此有效。

例如:

/goal 将主页 Lighthouse 分数提升到 90 或以上,尝试 5 次后停止。

§ 7
  • Triggered by: A specified time interval.
  • Stop criteria: You cancel it, or the work completes (the PR merges, the queue is empty).
  • 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.

For these, you can trigger when Claude runs with /loop which re-runs a prompt on an interval. For example:

/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.

  • 触发方式:指定的时间间隔。
  • 停止条件:你取消它,或者工作完成(PR 合并、队列为空)。
  • 最佳用途:用于重复性工作,或与外部环境/系统交互。
  • 使用管理:设置更长的间隔,或基于事件而非时间做出反应。

有些智能体工作是重复性的:任务保持不变,只有输入变化。例如,每天早上总结 Slack 消息。其他工作依赖于外部系统,与之交互的一种简单方法是按间隔检查并对变化做出反应。例如,一个可能收到代码审查或 CI 失败的 PR。

对于这些情况,你可以在 Claude 运行时使用 /loop 触发,它会按间隔重新运行提示。例如:

/loop 5m 检查我的 PR,处理审查意见,修复失败的 CI

/loop 在你的计算机上运行,所以如果你关闭它,它就会停止。你可以通过创建例程 /schedule 将循环移至云端。

§ 8

§ 9
  • 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.

For example, to handle incoming feedback, you can use:

  1. /schedule (research preview) to run a routine that checks for new reports
  2. /goal to define what done looks and skills to document how to verify it
  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.
  • 触发方式:事件或计划,无实时人类参与。
  • 停止条件:每个任务在达成目标时退出。例程本身一直运行,直到你将其关闭。
  • 最佳用途:定义明确的重复性工作流:错误报告、问题分类、迁移、依赖升级等。
  • 使用管理:将例程路由到更小、更快的模型,并使用能力最强的模型进行判断。

上述原语,连同其他 Claude Code 功能(如自动模式和动态工作流(研究预览))可以组合成一个用于长期运行的循环。

例如,要处理传入的反馈,你可以使用:

  1. /schedule(研究预览)运行检查新报告的例程
  2. /goal 定义完成的样子,以及技能文档如何验证
  3. 动态工作流编排智能体,对每个报告进行分类、修复并审查修复
  4. 自动模式,使例程无需请求许可即可运行 综合起来,提示可能如下:
/schedule 每小时一次:检查 project-feedback 频道中的错误报告。 /goal:直到本轮找到的每个报告都被分类、处理并回复才停止。修复错误时,使用工作流在并行工作树中探索三个解决方案,并让裁判进行对抗性审查。
§ 10

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.
  • 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. 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.

循环输出的质量取决于其周围的系统。在设计系统时:

  • 保持代码库本身整洁:Claude 会遵循代码库中已有的模式和约定。
  • 让 Claude 能够验证自己的工作:将你和团队眼中的“好”编码为技能。
  • 让文档易于访问:框架和库的文档提供了最新的最佳实践。
  • 使用第二个智能体进行代码审查:拥有新上下文的审查者偏见较少,且不受主智能体推理的影响。你可以使用内置的 /code-review 技能或 GitHub 的 Code Review。 当单个结果不符合标准时,不要停留在修复单个问题上,尝试将其编码以改进系统,使其适用于所有未来的迭代。
§ 11

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).
  • 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.
  • 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.

为了管理令牌使用,循环应有清晰的边界:

  • 为工作选择合适的原语和模型:较小的任务不需要多个智能体或循环。有些任务可以使用更便宜、更快的模型。
  • 定义明确的成功和停止标准:具体说明“完成”的样子,以便 Claude 能更快(但不要太快)找到解决方案。
  • 在大量运行前先试点:动态工作流可以生成数百个智能体。先在小范围工作中评估使用量。
  • 对确定性工作使用脚本:运行脚本比推理步骤更便宜。例如,PDF 技能可以附带一个填写表单的脚本,Claude 每次运行它,而不是重新推导代码。
  • 不要比你需要的频率更高地运行例程:将间隔与观察对象的变化频率匹配。
  • 审查使用量:/usage 命令按技能、子智能体和 MCP 分解最近的使用情况;不带参数的 /goal 显示迄今为止的轮次和令牌使用量;/workflows 显示每个智能体的令牌使用量,你可以随时停止智能体。
§ 12

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.

This article was written by @delba_oliveira

总结如下:

循环 你移交的内容 何时使用 使用工具
回合制 检查 你在探索或决策 自定义验证技能
目标驱动 停止条件 你知道完成的样子 /goal
时间驱动 触发 工作发生在项目外部并按计划进行 /loop, /schedule
主动 提示 工作是重复性的且定义明确 上述所有,以及动态工作流

要开始使用循环,请查看你已经在做的工作。选择一个你作为瓶颈的任务,问一下你可以移交哪个部分:你能编写验证检查吗?目标是否足够清晰?工作是按计划到达的吗?

一旦有了想法,运行循环,观察结果,比如它在何处卡住或过度延伸,并不要害怕迭代。

有关更多信息,请阅读有关并行运行智能体的 Claude Code 文档,以及循环、计划、目标和动态工作流页面。

本文由 @delba_oliveira 撰写

打开原文 ↗