9步编排Claude Code智能体集群:如何让1个主管Agent协调10个子Agent并行工作
本文详细拆解了在Claude Code中使用Dynamic Workflows并行调度多个子Agent的9个步骤。作者指出,单纯并行启动多个Agent很容易导致冲突和混乱,真正的关键在于主管Agent的编排循环:先检查任务是否可分解,让主管Agent分解为原子子任务并等待人工审批,通过Git Worktree隔离每个子Agent的工作目录,并行分发后使用SubagentStop Hook强制门控(运行测试和lint),再用一个独立评分Agent根据预设标准评估每个结果并自动退回不合格者,最后只由主管Agent按依赖顺序合并通过的提交。文章强调,核心技能不是‘能启动10个Agent’,而是能收回10个干净、已测试、已合并的结果。
You give Claude one instruction. Ten agents start working at once. Twenty minutes later you come back to ten finished branches, each one already tested and reviewed.
That's not a fantasy setup or a third-party tool. It's a built-in Claude Code pattern called Dynamic Workflows, where a single lead agent can plan and fan out tens, even hundreds, of parallel subagents in one session, with a grader sending each one back to revise until its result clears the bar.
The catch: spin up ten agents wrong and you get ten conflicting diffs, a polluted context, and more time merging the mess than you saved running it. The difference between a swarm that works and a swarm that fights itself is the loop the lead runs. Here are the 9 steps.
你给 Claude 一个指令,十个智能体同时开始工作。二十分钟后回来,十个分支已经完成,并且每个都经过了测试和审查。
这不是幻想,也不是第三方工具。这是 Claude Code 内置的一种模式,叫做 Dynamic Workflows,由一个主导智能体来规划并派出十几个甚至上百个并行子智能体,同时配备一个评审器,让每个子智能体反复修改直到结果达标。
问题在于:十个智能体部署不当,就会产生十个冲突的 diff,污染上下文,而合并这些混乱所花的时间比节省的还要多。群组能协同工作还是彼此打架,关键都在于主导智能体运行的循环。以下就是 9 个步骤。
A swarm only helps if the work splits into independent pieces.
This is the step that decides whether the swarm helps or hurts. Subagents pay off when a task naturally breaks into pieces that don't depend on each other: documentation per module, a migration across many independent files, a benchmark across dozens of combinations. If the pieces are tangled together, ten agents will just step on each other.
GOOD SWARM TASKS vs BAD ONES
Good: "Generate docs for all 12 modules" (each module is independent)
Good: "Migrate 30 files from the old API to the new one"
Bad: "Build one feature where every layer depends on the last"
Rule of thumb: if you can't name 3+ genuinely independent pieces, use one agent
✓ You only summon the swarm when the work truly splits apart
只有当工作可以被拆分为独立部分时,群组才有效。
这一步决定了群组是助力还是阻力。子智能体在任务能自然分解为互不依赖的部分时才有价值:比如给每个模块写文档、跨多个独立文件迁移、对几十种组合进行基准测试。如果各部分相互纠缠,十个智能体只会互相踩脚。
好群组任务 vs 差群组任务
好的:「为所有 12 个模块生成文档」(每个模块独立)
好的:「将 30 个文件从旧 API 迁移到新 API」
差的:「构建一个每一层都依赖上一层的功能」
经验法则:如果你说不出 3 个以上真正独立的部分,就只用单个智能体
✓ 只有当工作真正可以拆分时,才召唤群组
The lead breaks the job into atomic subtasks before spawning anyone.
The lead agent's first job isn't to spawn workers, it's to decompose. It splits the task into discrete subtasks, each with clear inputs, outputs, and acceptance criteria. Good subtasks are atomic: bounded files, a clear deliverable, and independently verifiable. This decomposition is what makes the parallelism safe.
ASK THE LEAD TO DECOMPOSE
"You are the lead. Decompose this task into independent
subtasks. For each: the files it owns, its exact deliverable,
and how to verify it's done. Do NOT spawn anyone yet.
Show me the decomposition first."
✓ The job is now a set of clean, independent units, not one blob
在派出任何智能体之前,主导智能体先把任务分解为原子级的子任务。
主导智能体的首要任务不是派工,而是分解。它将任务拆分成离散的子任务,每个子任务都有明确的输入、输出和验收标准。好的子任务是原子化的:有明确的范围边界、清晰的交付物,并且可以独立验证。这种分解是并行安全的基础。
要求主导智能体分解
「你是主导。把该任务分解为独立的子任务。每个子任务要明确:负责的文件、确切的交付物、如何验证完成。先不要派出任何人。先把分解方案展示给我。」
✓ 任务现在成了一组干净、独立的单元,而不是一团乱麻
Review the decomposition. Wrong dependencies here become merge hell later.
This is your one human checkpoint, and it's the most important one. The decomposition plan determines what runs in parallel. If the lead got a dependency wrong, two agents will edit the same thing and you'll get integration conflicts. Always read the plan before approving the dispatch.
What to check in the plan:Do any two subtasks touch the same files? (They shouldn't.) Is each deliverable independently verifiable? Are the dependencies right? Fixing the plan now costs one message. Fixing a tangled merge later costs an hour.
✓ You catch bad dependencies before they turn into merge conflicts
审查分解方案。这里的依赖关系一旦出错,后面就是合并噩梦。
这是你唯一的人工检查点,也是最重要的一个。分解方案决定了哪些任务可以并行。如果主导智能体搞错了依赖关系,两个智能体就会编辑同一个文件,导致集成冲突。在批准派出之前,一定要仔细阅读方案。
方案中要检查什么:有没有两个子任务涉及相同的文件?(不应该有)每个交付物是否可独立验证?依赖关系是否正确?现在修改方案只需一条消息的成本,而后面处理杂乱的合并需要一小时。
✓ 你在依赖关系变成合并冲突之前就发现了问题
Isolation is what stops ten agents from overwriting each other.
If your subagents write files in parallel, they need isolation or they'll clobber each other. Git worktrees give each agent its own checkout on its own branch, so they can all work at once without touching the same working directory. When each finishes, its branch is ready to merge cleanly.
ISOLATE EACH AGENT
# add to each subagent's frontmatter
isolation: worktree
# Claude Code provisions a fresh worktree per agent
# and cleans it up automatically when the agent finishes
# ten agents, ten branches, zero file collisions
✓ Ten agents work simultaneously without ever touching the same file
隔离机制让十个智能体不会互相覆盖。
如果子智能体并行写入文件,它们就需要隔离,否则会互相覆盖。Git worktree 让每个智能体在自己的分支上拥有独立的检出,这样它们可以同时工作,不会碰触同一个工作目录。每个智能体完成后,它的分支都可以干净地合并。
隔离每个智能体
# 添加到每个子智能体的前置信息中
isolation: worktree
# Claude Code 为每个智能体提供一个新的工作树
# 并在智能体完成后自动清理
# 十个智能体,十个分支,零文件冲突
✓ 十个智能体同时工作,永远不会碰触同一个文件
Now the lead dispatches the workers, in parallel.
With the plan approved and isolation set, the lead dispatches. For a handful of agents, the lead spawns subagents directly. For a job that fans out wide, Dynamic Workflows lets the lead plan and run tens to hundreds of subagents in a single session. Each runs in its own context window and reports only its result back, so the lead's context stays clean for coordination.
DISPATCH THE SWARM
"Plan approved. Dispatch one subagent per subtask, in
parallel, each in its own worktree. Each agent gets only
the context it needs and returns a single result summary.
Don't merge anything until every agent reports back."
The sweet spot:Three to five concurrent agents is the right size for most everyday work. Push to tens or hundreds only when the task genuinely fans out wide (a benchmark suite, edits across many independent files). Beyond the sweet spot, you spend more time merging summaries than you save.
✓ The whole swarm is now working at once, each in isolation
现在主导智能体并行派出工作人员。
计划批准、隔离设置完毕,主导智能体开始派出。对于少量智能体,主导直接派出子智能体。对于大规模并行任务,Dynamic Workflows 让主导在单个会话中规划并运行几十甚至上百个子智能体。每个子智能体在自己的上下文窗口中运行,只返回结果,这样主导的上下文保持干净以便协调。
派出群组
「计划已批准。为每个子任务并行派出一个子智能体,每个在自己的工作树中。每个智能体只获得它需要的上下文,并返回一个结果摘要。在所有智能体报告完成之前,不要合并任何东西。」
最优点:三到五个并发智能体适合大多数日常任务。只有当任务真正需要大规模并行(比如基准测试套件、跨多个独立文件的编辑)时,才推到十个以上。超出最优点,你花在合并摘要上的时间会比节省的还多。
✓ 整个群组现在同时工作,每个都在隔离环境中
No worker's output folds back in until it clears your non-negotiables.
Ten agents means ten chances for something to slip through. A SubagentStop hook runs the moment a subagent finishes and enforces your non-negotiables before the lead is allowed to fold the result back in: tests pass, no secrets in the diff, no out-of-scope file writes. It's deterministic, so it checks every single agent, not most of them.
.claude/settings.json // GATE EACH AGENT
{
"hooks": {
"SubagentStop": [{
"command": "npm run test && npm run lint"
}]
}
}
# every subagent's work is checked before it can merge
# one bad agent can't quietly poison the result
✓ Every worker's output is verified before it's allowed near your main branch
每个工作人员的输出必须通过硬性检查才能被合并。
十个智能体意味着有十次机会出问题。SubagentStop 钩子在子智能体完成时立即运行,强制检查硬性条件,然后主导智能体才能把结果合并进来:测试必须通过,diff 中不能有密钥,不能写入超出范围的文件。这个检查是确定性的,它会检查每一个智能体,而不是大多数。
.claude/settings.json // 把关每个智能体
{
"hooks": {
"SubagentStop": [{
"command": "npm run test && npm run lint"
}]
}
}
# 每个子智能体的工作在被允许合并前都要经过检查
# 一个坏智能体无法悄悄污染结果
✓ 每个工作人员的输出在允许进入主分支之前都经过了验证
A separate grader scores each result and forces a revision if it misses.
This is the part that turns a swarm from "fast but sloppy" into "fast and good." A separate grader scores each subagent's result against a rubric and sends any that miss the bar back to revise, automatically, until they pass. You define what "good" looks like once, and the swarm self-corrects toward it without you reviewing all ten by hand.
SET THE BAR
"Grade each subagent's result against this rubric:
- tests for the change exist and pass
- follows the conventions in CLAUDE.md
- no TODOs or placeholder code left behind
Send any result that misses the bar back to its agent to
revise. Repeat until every result passes."
✓ Weak results get revised automatically instead of landing on you
一个独立的评审器给每个结果打分,不合格的会自动退回修改。
这一步让群组从「快但粗糙」变成「又快又好」。独立的评审器根据评分标准给每个子智能体的结果打分,不达标的自动退回修改,直到通过。你只需要定义一次「好」的标准,群组就会自我修正,而不用你亲手检查所有十个结果。
设定标准
「根据以下标准给每个子智能体的结果打分:
- 变更的测试存在且通过
- 遵循 CLAUDE.md 中的约定
- 没有留下 TODO 或占位符代码
将任何不达标的结果退回给它的智能体重新修改。重复直到所有结果通过。」
✓ 不合格的结果会自动修订,而不是落到你头上
The lead is the integration layer. Workers never integrate their own work.
Here's a mistake that wrecks swarms: asking the subagents to integrate their own work. Do that and you get duplication and conflicts. The lead is the integration layer. It collects the graded, passing results and merges them in a controlled order, exactly like a tech lead assembling work from a team rather than each engineer pushing to main on their own.
LEAD-ONLY MERGE
"Only you, the lead, merge. Collect the passing branches and
integrate them in dependency order. After each merge, run the
full test suite before taking the next one. Stop and tell me
if any two results conflict instead of guessing."
✓ One agent owns integration, so the merges stay clean and ordered
主导智能体是集成层。工作人员从不自行集成自己的工作。
这里有一个会毁掉群组的错误:让子智能体自己集成自己的工作。这么做会导致重复和冲突。主导智能体才是集成层。它收集评分通过的结果,并按照依赖顺序有序合并,就像技术主管整合团队的工作,而不是让每个工程师自己往主分支推送。
仅主导合并
「只有你,主导,才能合并。收集通过的分支,按依赖顺序集成。每次合并后,运行完整测试套件,再进行下一次。如果任何两个结果冲突,停下来告诉我,不要猜测。」
✓ 一个智能体掌管集成,合并保持干净有序
Wrap the entire loop so you trigger the swarm with a word.
Once the loop works, package it. Claude Code already ships a batch skill that splits one large change into many worktree-isolated subagents that each open a pull request, and you can wrap your own version of this 9-step loop into a reusable skill or slash command. Set it up once, and "run the swarm on this" becomes a single trigger.
.claude/skills/swarm/SKILL.md // YOUR LOOP, ONE TRIGGER
# /swarm : decompose, isolate, dispatch, grade, merge
1. Check the task genuinely fans out
2. Decompose into independent subtasks
3. Show me the plan, wait for approval
4. Give each agent its own worktree
5. Dispatch the swarm in parallel
6. Gate each result with the SubagentStop hook
7. Grade against the rubric, revise the weak ones
8. Lead merges the passing results in order
✓ The whole 9-step swarm now runs from one command
把整个循环封装起来,这样你用一个词就能触发群组。
一旦循环正常工作,就把它打包。Claude Code 已经内置了一个批处理技能,可以把一个大变更拆分成许多隔离在工作树中的子智能体,每个单独开一个 PR,你也可以把这个 9 步循环封装成可复用的技能或斜杠命令。设置一次,然后「对此运行群组」就变成一个触发器。
.claude/skills/swarm/SKILL.md // 你的循环,一个触发器
# /swarm : 分解、隔离、派出、评分、合并
1. 检查任务是否真正可并行
2. 分解为独立子任务
3. 展示计划,等待批准
4. 给每个智能体自己的工作树
5. 并行派出群组
6. 用 SubagentStop 钩子把关每个结果
7. 根据评分标准评分,修订不合格的
8. 主导按顺序合并通过的结果
✓ 整个 9 步群组现在用一个命令就能运行
Anyone can spawn ten agents. The reason most people's swarms produce chaos is that they skip the structure: no decomposition, no isolation, no grading, no single integrator. Ten unmanaged agents is just ten ways to create a merge conflict. The loop is what turns a crowd into a team.
- Decomposition plus your approval means the work splits cleanly, not chaotically
- Worktrees mean ten agents never overwrite each other
- The hook plus the grader mean quality is enforced per agent, not hoped for
- A single lead integrator means merges stay clean instead of becoming a pileup
- The command means you set this up once and run a swarm with one word
The honest takeaway:Running 10 agents isn't impressive on its own, it's easy, and usually a mess. Running 10 agents that come back as one clean, tested, merged result is the actual skill. That skill is the loop, not the swarm.
谁都能派出十个智能体。大多数人搞的群组之所以混乱,是因为跳过了结构:没有分解、没有隔离、没有评分、没有单一的集成者。十个无人管理的智能体只是十种制造合并冲突的方式。循环才是把一群乌合之众变成高效团队的关键。
- 分解加上你的审批意味着工作被干净拆分,而不是混乱无序
- 工作树意味着十个智能体永远不会互相覆盖
- 钩子加评分意味着质量在每个智能体层面被强制保证,而不是靠期望
- 单一主导集成者意味着合并保持干净,而不是堆成一团
- 命令意味着你只需设置一次,之后用一个词就能运行群组
坦诚的结论:单跑 10 个智能体本身并不令人印象深刻,这很容易,而且通常是场混乱。真正有价值的技能是让 10 个智能体最后交付一个干净、经过测试、合并完成的结果。这个技能就是循环,而不是群组本身。