如何搭建一个会循环跑的 Claude Code Agent 团队(含完整配置)

发布于 2026年06月18日 22:40 #Claude#Agent 框架 原文链接

如何搭建一个会循环跑的 Claude Code Agent 团队(含完整配置) 封面图
  • 使用 builder 和 checker 两个独立 Agent 分别负责编码和检查,避免自检盲点
  • loop orchestrator 驱动 build-check-fix 循环,自动传递失败信息给 builder
  • stop rules 设定全绿、5 次循环、连续两次相同失败等终止条件,防止无限循环
  • 实际运行只需一行 /loop 命令,团队自动循环直到所有检查通过
  • 常见坑包括无上限循环、让 builder 自检、无重复失败规则、检查可被绕过

原文链接:https://x.com/zodchiii/article/2066882971374678057

大多数配置跑一次 Agent 就把结果丢给你。而一个会循环跑的团队会持续工作,直到产出真的过关。

下面是 3 个文件的完整配置:agents、驱动它们的 loop、以及告诉它们什么时候该停的 rules。

完整配置如下 👇

为什么一次性团队不够用

跑一次的团队就像一场没人盯终点的接力赛。writer 写、tester 测、reviewer 审,最后所有东西都堆到你面前——带着各种坏掉的部分。

会循环的团队把这个缺口补上了。当 tester 发现失败,它不是只汇报然后退出,loop 会把它送回 writer 去修,然后循环再跑一遍。你只在所有检查都通过、或者团队真的撞墙时才介入。

这套配置就 3 个文件:agents、loop、stop rules。

文件 1:agents

两个专家,各干一件事。放进 .claude/agents/

builder.md:

---
name: builder
description: Writes and fixes code. Invoke to implement a task or to fix failures the checker found.
tools: Read, Write, Edit, Glob, Grep, Bash
model: sonnet
---

You build and you fix. Nothing else.

- On a new task: implement it, matching existing style.
- On a fix request: read the failure, find the cause, fix that cause only.
- Never weaken a test to make it pass. Fix the code.
- Report what you changed in one line.

checker.md:

---
name: checker
description: Runs all checks and reports what failed. Invoke after the builder. Never edits code.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You check, you never fix.

Run all three, in order:
1. Tests: `npm test` (or `pytest -q`, `cargo test --quiet`)
2. Types: `npx tsc --noEmit` (or `pyright`, `cargo check`)
3. Lint: `npm run lint` (or `ruff check`, `cargo clippy`)

Then report in this exact format:

- All pass: "ALL GREEN"
- Any fail: "FAILED" then each cause as
  `file:line - what broke - which check caught it`

Never paraphrase a failure. Copy the real error. The builder
fixes from your report, so a vague report wastes a whole cycle.

文件 2:loop

这是驱动整个循环的 orchestrator。放进 .claude/commands/loop.md

---
description: Run the builder and checker in a loop until all checks pass
argument-hint: <task>
allowed-tools: Read, Grep, Glob, Bash, Task
model: opus
---

Run this task as a loop: $ARGUMENTS

1. Write a one-line brief: goal, files in scope, definition of done.
2. Dispatch the builder to implement the task.
3. Dispatch the checker to run all checks.
4. If checker says ALL GREEN: stop, show me the result.
5. If checker says FAILED: send the failures to the builder to fix,
   then go back to step 3.
6. Repeat up to 5 cycles. Track the cycle count out loud.

Stop conditions are in CLAUDE.md. Follow them exactly.

loop 就是整个 idea 的核心:build、check、失败了再 build。

orchestrator 会自动把 checker 的失败传给 builder,所以团队不用你来回递消息,自己就能跑下去。

文件 3:stop rules

没有刹车的 loop 要么跑到天荒地老,要么假装通过。把这些规则放进 CLAUDE.md

## Loop stop rules

The team loops until one of these is true:

- ALL GREEN: every check passes. Stop and report success with proof.
- 5 cycles used: stop. Report what still fails and what was tried.
- Same failure twice in a row: stop. The builder is guessing, not
  fixing. Escalate to me.
- A fix makes a previously passing check fail: stop. Something is
  being broken to fix something else.

Never report success without checker output from the final cycle.
Never weaken or delete a check to reach ALL GREEN.

这些规则就是把「有用的循环」和「Agent 原地打转烧 token」区分开来的关键。

「连续两次同样的失败」这条最重要:两次相同失败说明团队卡住了,应该让人来介入。

实际跑起来你会看到什么

你只打一行:

/loop add rate limiting to the login route, 5 attempts per IP per minute

然后看着团队自己循环:

Done in 3 cycles. Review the diff?

Cycle 1
  builder  → wrote rateLimiter.ts, edited login.ts
  checker  → FAILED
             login.test.ts:42 - expected 429, got 200 - tests
             rateLimiter.ts:18 - 'window' possibly undefined - types

Cycle 2
  builder  → fixed the 429 response, added null guard on window
  checker  → FAILED
             login.test.ts:51 - counter not reset after window - tests

Cycle 3
  builder  → reset counter on window expiry
  checker  → ALL GREEN (14/14 tests, types clean, lint clean)

这就是整件事的精髓,全在屏幕上。你一次失败都没接力。checker 找到问题、builder 修掉它们、loop 在 3 个 cycle 内跑到全绿,全程不用你碰键盘。

常见坑

没有 cycle 上限。 没写「最多 5 个 cycle」的话,卡住的团队会一直循环到 token 烧光。这个上限能把无限循环变成一份清晰的报告。

让 builder 自己检查自己。 同一个 Agent 既写又判,等于用当初写 bug 时的盲点给自己打分。builder 和 checker 必须分开。

没有「连续两次相同失败」规则。 两次相同失败说明在瞎猜而不是在修,那正是该停下来看的时刻,不该再花第 4 个 cycle。

loop 能钻空子的检查。 如果 checker 可以靠删测试通过,它最终会这么干。stop rules 之所以禁止削弱检查,是有原因的。

10 分钟搭建

3 分钟:在 .claude/agents/ 下建 builder.mdchecker.md

3 分钟:在 .claude/commands/loop.md 建 loop orchestrator。

2 分钟:把 stop rules 加到 CLAUDE.md

2 分钟:拿一个真实任务跑 /loop,看着它循环:build、check、fix、pass。

你不用再来回递失败消息了。团队跑 loop,你读结果。它没有变得更聪明,只是不再在活干完之前就退出。

评论互动

© 2026 王若风的技术博客 · Powered by Astro