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.

  • Category
  • Product
    Claude Code
  • Date
    June 30, 2026
  • Reading time
    5
    min
  • Share
    Copy link
    https://claude.com/blog/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. 目前关于“设计循环”(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 使用量的同时保持代码质量。并非所有任务都需要复杂的循环;请从最简单的解决方案开始,并有选择地使用这些模式。

No items found.
Prev
0/5
Next
Or read the documentation
Try Claude Code
Developer docs
eBook

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 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 尝试停止时,评估模型会检查你的条件,如果未满足目标或未达到你定义的轮次,它会将任务发回给 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:对于这些任务,你可以使用 `/loop` 来触发 Claude 的运行,它会按间隔重新运行提示词。例如:

/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主动循环 (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 #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.为 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 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管理 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 running agents in parallel, as well as the loop, schedule, goal, and dynamic workflows pages. 欲了解更多信息,请阅读 Claude Code 关于并行运行智能体的文档,以及 loop、schedule、goal 和动态工作流页面。

This article was written by Delba de Oliveira and Michael Segner本文由 Delba de Oliveira 和 Michael Segner 撰写

No items found.

Transform how your organization operates with Claude

See pricing
Contact sales

Get the developer newsletter

Product updates, how-tos, community spotlights, and more. Delivered monthly to your inbox.

Subscribe

Please provide your email address if you'd like to receive our monthly developer newsletter. You can unsubscribe at any time.

Thank you! You’re subscribed.
Sorry, there was a problem with your submission, please try again later.
Claude Code
Coding