Glean 拾遗
Daily /2026-07-16 / How to Create Loops with Claude

How to Create Loops with Claude

Source x.com Glean’d 2026-07-16 06:00 Read 10 min
AI summary

This article advocates shifting from writing single prompts to designing loops—automated systems that keep AI agents working without human intervention. It breaks down a loop into six components: automation triggers, git worktrees for parallel isolation, skills (procedure manuals), connectors, sub-agents, and persistent memory files (e.g., STATE.md). The evaluator-optimizer pattern is highlighted: one agent generates, another verifies against objective gates like test suites or type checkers. Stop conditions must be checkable by external signals, not the agent's own claim. An autonomy ladder (suggest, draft, apply low-risk, full auto) helps gradually earn trust. The article also warns about token costs and the need for command allowlists in unattended loops.

Original · 10 min
x.com ↗
§ 1

How to Create Loops with Claude

How to Create Loops with Claude

§ 2

stop making prompts.

start designing loops.

a prompt gets you one response. a loop gets you a system that keeps working after you close the laptop. Boris Cherny, who runs Claude Code at Anthropic, put it plainly: he does not prompt Claude anymore, he has loops running that prompt Claude and figure out what to do. his job is to write loops.

Peter Steinberger said the same thing from a different angle: you should not be prompting coding agents anymore, you should be designing loops that prompt your agents. the leverage point has moved. it is no longer about crafting the perfect message. it is about building the system that sends messages for you, reviews the results, and decides what happens next.

a loop is a recursive goal. you define a purpose, the agent iterates against it, and the loop keeps running until a real stopping condition is met. the agent forgets everything between runs. the loop does not. that single fact is the entire architecture.

别再写提示词了。开始设计循环吧。

一条提示词只能给你一次回应,而一个循环能给你一个系统——在你合上笔记本电脑后,它仍在持续运转。Anthropic 公司 Claude Code 的负责人 Boris Cherny 直言不讳:他不再向 Claude 提问了,而是运行着多个循环,这些循环会自动向 Claude 发送提示并决定下一步行动。他的工作是编写循环。

Peter Steinberger 从另一个角度表达了同样的观点:你不再需要向编码代理发送提示,而是应该设计能够自动提示这些代理的循环。杠杆点已经转移了。关键不再是精心雕琢一条完美的消息,而是构建一个能够替你发送消息、审查结果并决定下一步行动的系统。

循环是一个递归的目标。你定义一个目的,代理围绕它迭代执行,循环持续运行直到满足真正的停止条件。代理在每次运行之间会忘记一切,但循环不会。仅这一点就构成了整个架构。

§ 3

What a Loop Actually Is

Addy Osmani, a Google engineer who wrote the essay that named this practice, breaks a loop into six parts: automations, worktrees, skills, connectors, sub-agents, and memory. every working loop is some combination of these six.

automations are what make a loop a loop instead of a one-time run. this is a schedule, a cron job, a webhook, or a hook inside Claude Code that fires without you typing anything. the agent finds work and triages it before you ask.

worktrees keep parallel agents from stepping on each other. if two agents touch the same files at the same time, you get collisions. git worktrees give each agent its own isolated copy of the repo to work in.

skills are procedure manuals the agent reads instead of being told from scratch every time. memory is a state file on disk, usually markdown, that survives between runs. the agent forgets, the file does not.

循环的真正构成

Google 工程师 Addy Osmani 撰写了命名这一实践的文章,他将循环分解为六个部分:自动化工件、工作树、技能、连接器、子代理和记忆。每个运行中的循环都是这六个部分的某种组合。

自动化工件是让循环成为循环而不是一次性运行的关键。这可以是一个定时任务、cron 作业、webhook,或是 Claude Code 内部无需你输入任何内容即可触发的钩子。代理会自动发现工作并在你询问之前进行分诊。

工作树可以防止并行代理互相干扰。如果两个代理同时操作相同的文件,就会产生冲突。Git 工作树为每个代理提供独立的仓库副本。

技能是代理阅读的操作手册,而不是每次从头告知。记忆是磁盘上的一个状态文件,通常是 Markdown 格式,它会在运行之间持久存在。代理会忘记,但文件不会。

§ 4

Start With One Trigger

every loop starts with something that fires without you. the simplest version is a cron job that runs a Claude Code prompt on a schedule. the next version is a hook, a script that runs automatically when a specific event happens, like a commit or a file change.

pick one recurring task you currently do manually and turn the trigger into the first piece. "every morning at 8am, read yesterday's CI failures, open issues, and recent commits, and write findings to a markdown file." that single automation is a complete, working loop on its own.

do not try to build the full six-part system on day one. one automation that writes one state file is already more leverage than a hundred well-crafted prompts, because it runs without you.

从一个触发器开始

每个循环都始于某个无需你操作即可触发的事件。最简单的版本是一个按计划运行 Claude Code 提示的 cron 作业。下一个版本是一个钩子,当特定事件(如提交或文件变更)发生时自动运行的脚本。

选择一项你当前手动执行的重复性任务,将触发器作为第一环。“每天早上8点,读取昨天的 CI 失败记录、打开的问题和最近的提交,并将发现写入一个 Markdown 文件。”仅此一个自动化工件就是一个完整、可运行的循环。

不要试图在第一天就构建完整的六部分系统。一个能写入状态文件的自动化工件,其杠杆效应已经超过一百条精心编写的提示词,因为它无需你参与即可运行。

§ 5

Give the Loop a Memory File

create one markdown file, call it STATE.md or PROGRESS.md, and place it where every iteration of the loop can read and write it. this file is the loop's only memory. everything the agent needs to pick up where it left off goes here.

at the start of each run, the agent reads this file first. at the end of each run, it writes back what happened and what comes next. this is the PROGRESS.md pattern, and it is the single most important file in any loop. without it, every run starts from zero regardless of how many runs came before.

structure the file in plain sections: what was done last run, what is in progress, what is blocked, what to try next. keep it short. a memory file the agent has to read 2000 lines of is worse than no memory file at all.

为循环赋予记忆文件

创建一个 Markdown 文件,命名为 STATE.mdPROGRESS.md,将其放置在循环的每次迭代都可读写的位置。这个文件是循环唯一的记忆。代理需要恢复执行所需的所有信息都存放在这里。

每次运行开始时,代理首先读取这个文件。运行结束时,它记录下发生了什么以及下一步做什么。这就是 PROGRESS.md 模式,它是任何循环中最重要的文件。没有它,每次运行都会从零开始,无论之前已经运行了多少次。

将文件结构划分为清晰的段落:上次运行完成了什么、正在进行什么、有什么阻塞、下一步尝试什么。保持简短。一个需要代理读取2000行的记忆文件还不如没有记忆文件。

§ 6

Split the Writer From the Checker

the model that wrote the code is, in Osmani's words, too nice grading its own homework. a single agent that writes and then reviews its own work will mark its own work as done more often than it should.

the fix is the evaluator-optimizer pattern, named in Anthropic's own engineering writeup on building effective agents: one agent generates, a second agent critiques against an objective standard, and the loop repeats until the check passes. the check has to fail on something real: a test suite, a type checker, a build command, a linter.

a second agent told to "review this" with no objective signal just adds a second optimist. it will agree with the first agent more often than not. the verifier needs a hard gate, not an opinion.

分离编写与检查角色

用 Osmani 的话说,负责编写代码的模型在给自己的作业打分时太过宽容。一个既编写又审查自身工作的代理,往往更容易将自身工作标记为完成。

解决方案是评估器-优化器模式,该模式在 Anthropic 关于构建高效代理的工程文档中有所介绍:一个代理生成内容,另一个代理对照客观标准进行批判,循环重复直到检查通过。检查必须基于真实事物失败:测试套件、类型检查器、构建命令、代码检查工具。

如果只是让另一个代理不带客观信号地“审查这个”,那只是增加了第二个乐观者。它很可能会同意第一个代理的看法。验证者需要一个硬性的门槛,而不是一个观点。

§ 7

Isolate Parallel Work With Worktrees

once you are running more than one agent against the same codebase, isolation stops being optional. run git worktree add ../agent-1-branch to give each agent its own working directory pointed at its own branch. this prevents two agents from editing the same file at the same time and corrupting each other's changes.

a typical parallel setup: one sub-agent explores and writes a plan, a second sub-agent implements against that plan in its own worktree, a third sub-agent verifies the implementation against tests in a separate worktree. each agent only ever sees its own copy.

this is also where loops scale from "one task running in the background" to "an entire pipeline of tasks running at once," each isolated, each reporting back to the shared memory file when done.

用工作树隔离并行工作

当你对同一个代码库运行多个代理时,隔离就不再是可选项。运行 git worktree add ../agent-1-branch 为每个代理提供指向各自分支的独立工作目录。这能防止两个代理同时编辑同一个文件导致相互破坏。

典型的并行设置:一个子代理探索并编写计划,第二个子代理在它自己的工作树中根据计划实现,第三个子代理在独立的工作树中对照测试验证实现。每个代理只看到自己的副本。

这也是循环从“后台运行单个任务”扩展到“同时运行整个任务管线”的地方,每个任务相互隔离,完成后向共享记忆文件报告。

§ 8

Set a Hard Stop Condition

a loop without a real exit condition fails quietly. engineer Geoffrey Huntley documented this as the "Ralph Wiggum loop": an agent meant to emit a completion signal only when finished emits it early, and the loop exits believing a half-done job is complete.

your stop condition needs to be checkable by something other than the agent's own claim. "the test suite passes," "the build succeeds," "the linked ticket moves to Done with a passing CI run" are real stop conditions. "the agent says it's finished" is not.

set a maximum iteration count as a backstop regardless of what your primary stop condition is. ten or twenty iterations is a reasonable ceiling for most loops. if the loop hits the ceiling without meeting the real stop condition, it should halt and flag for review, not keep running.

设定硬性停止条件

没有真正退出条件的循环会悄无声息地失败。工程师 Geoffrey Huntley 将此记录为“Ralph Wiggum 循环”:一个代理本应在完成时发出完成信号,却过早发出信号,导致循环认为工作已完成一半就退出。

你的停止条件需要由代理自身声明以外的因素来检查。“测试套件通过”、“构建成功”、“相关工单在 CI 通过后移至完成状态”是真正的停止条件。“代理说它完成了”则不是。

无论主要停止条件是什么,都要设置一个最大迭代次数作为兜底。对于大多数循环,十次或二十次迭代是一个合理的上限。如果循环达到上限而仍未满足真正的停止条件,它应该停止并标记为待审查,而不是继续运行。

§ 9

Wire In a Human Review Checkpoint

not every loop should run fully unattended from day one. Boris Cherny's framing uses an autonomy ladder with four levels: level one suggests only, level two drafts changes for a human to apply, level three applies low-risk changes but requires human approval before publish or merge, level four applies and completes automatically with audit logs.

start every new loop at level one or two. run it for a week, read its output, and correct what it gets wrong. once the loop is consistently producing work you would approve without changes, move it to level three. level four is earned, not assumed.

the runs that find something should go to a triage inbox or a flagged list. the runs that find nothing should archive themselves silently. you should never have to open a loop's output to confirm that nothing happened.

接入人工审核检查点

并非所有循环从一开始就应该完全无人值守。Boris Cherny 的框架使用了一个包含四个级别的自主性阶梯:第一级仅提供建议,第二级草拟更改供人工应用,第三级应用低风险更改但在发布或合并前需要人工批准,第四级自动应用并完成,同时保留审计日志。

将每个新循环从第一级或第二级开始。运行一周,读取输出,纠正其错误。一旦循环持续产生你不需要修改就能批准的工作,就将其提升到第三级。第四级是赢得的,而不是默认的。

发现问题的运行应进入分诊收件箱或标记列表。什么也没发现的运行应静默归档。你绝不应该为了确认无事发生而去打开循环的输出。

§ 10

Watch the Token Cost

a single bad iteration is a wasted prompt. a single bad loop running unattended overnight is a bill. agentic loops can run for dozens or hundreds of iterations, and each iteration is a full model call with the accumulated conversation history attached.

before you let any loop run unsupervised, run it manually for three to five iterations and check the token usage per iteration. multiply that by your maximum iteration count to get a worst-case cost per run. multiply that by how often the automation fires to get a worst-case daily cost.

build a command allowlist for any loop that can execute shell commands. restrict it to the specific commands the task actually needs, things like npm, git, ls, cat. an agent with unrestricted shell access inside an unattended loop is the fastest way to turn a token-cost problem into a security problem.

监控Token成本

一次糟糕的迭代浪费一个提示词。一个糟糕的循环整晚无人值守运行则是一笔账单。代理循环可能运行数十或数百次迭代,每次迭代都是一次完整的模型调用,附带累积的对话历史。

在让任何循环无监督运行之前,先手动运行三到五次迭代,检查每次迭代的 token 使用量。将其乘以你的最大迭代次数,得到每次运行的最坏情况成本。再乘以自动化工件的触发频率,得到最坏情况下的日成本。

为任何可以执行 shell 命令的循环构建命令白名单。将其限制为任务实际需要的特定命令,比如 npmgitlscat。在无人值守的循环内拥有不受限制的 shell 访问权限,是将 token 成本问题转变为安全问题的最快方式。

§ 11

Build the Second Loop Differently Than the First

your first loop should be small, single-purpose, and heavily supervised. your second loop should connect to the first. this is where automations, skills, and memory start compounding instead of just running in parallel.

a daily triage loop writes findings to a shared state file. a second loop, also on a schedule, reads that state file and picks the highest-priority item to act on. neither loop needs the other to function, but together they form a pipeline that moves work from "discovered" to "in progress" without you touching either one.

this is also when skills start paying off. once you have written a skill file for how your loop should triage CI failures, every future loop that touches CI failures reads that same skill instead of you re-explaining it. the loops do not just run independently, they share what they have learned.

构建第二个循环以形成管线

你的第一个循环应该小而专一,且受到严格监督。你的第二个循环应该连接到第一个循环。这时,自动化工件、技能和记忆开始产生复利效应,而不仅仅是并行运行。

一个每日分诊循环将发现写入共享状态文件。另一个按计划运行的循环读取该状态文件,并选择优先级最高的项目进行处理。两个循环各自独立运行,但共同形成了一个管线,将工作从“已发现”推进到“进行中”,而你无需触碰任何一个。

这也是技能开始发挥价值的时候。一旦你为循环编写了如何处理 CI 失败问题的技能文件,未来所有涉及 CI 失败的循环都会读取同一份技能,无需你重新解释。这些循环不仅仅是独立运行,它们还共享学到的知识。

§ 12

The Shift in What Your Job Becomes

once a few loops are running, your daily work changes shape. you stop opening a chat window to ask a question and start opening a triage inbox to review what the loops found overnight. the to-do list stops being a static pile of tasks and becomes a set of agents, routines, and loops that keep converting ideas into drafts, fixes, and reviews.

this does not mean you stop deciding what matters. it means the deciding happens at the loop-design level instead of the per-task level. you are not writing fewer prompts because you are doing less. you are writing fewer prompts because the loops are writing them for you, and your attention moves to the parts that actually need a human: the review checkpoint, the stop condition, and the next loop worth building.

工作角色的转变

一旦几个循环开始运行,你的日常工作形态就会改变。你不再打开聊天窗口提问,而是打开分诊收件箱查看循环在夜间发现了什么。待办事项列表不再是一堆静态任务,而成为一组代理、例程和循环,它们不断将想法转化为草稿、修复和审查。

这并不意味着你不再决定什么重要。而是决策发生在循环设计层面,而非每个任务层面。你写的提示词变少不是因为做的工作少了,而是因为循环在替你写提示词。你的注意力转移到真正需要人工的部分:审查检查点、停止条件,以及下一个值得构建的循环。

Open source ↗