多 Agent 交接 Prompt:Claude Code 子代理模板

在 Claude Code subagent 之间交接工作的 12 个 Prompt 模板——研究 → 计划 → 实现 → 审查 → 发布,不丢上下文。

多数多 Agent 跑出来都漂移,因为没东西钉住交接:研究 agent 返回 3 段,实现 agent 自己造另一套设计,reviewer 跟两边都吵。好的交接 Prompt 要指定上游必须输出什么、下游必须读什么、明确什么是不在范围内的。

适合哪些场景

在配 Claude Code subagent 流水线的工程师、独自开发把 AI 当队的创业者、用 Agent SDK 链调用的开发者。

什么时候不建议这样写 Prompt

一条 prompt 就能搞定的任务别上 multi-agent,交接成本(延迟和漂移)是真实的。

Prompt 结构公式

每个交接 Prompt 都要带这六个要素:

  • 角色:AI 扮演谁(SRE / Release Captain / staff 工程师 / QA Lead)。
  • 上下文:技术栈 / 分支 / 失败日志 / diff / dashboard URL。
  • 目标:一个具体可交付物——根因、checklist、计划、ticket 列表、runbook。
  • 限制:AI 不能做什么(别自动修、别瞎造文件路径)。
  • 输出格式:编号清单、markdown 表格、JSON、unified diff、可运行代码。
  • 示例 / 信号:1-2 条”好输出”示例,或反例。

这套 Prompt 适合用在哪

  • 研究 → 计划 → 写代码 → 审查 流水线
  • 需求 → 测试 → 实现 TDD 链
  • 并行 Agent 收敛到一个产物
  • 长任务切给多 Agent 控制上下文
  • 提交进仓库的 Agent runbook

12 个可直接复制的 Prompt 模板

1. 交接契约模板

Before agents run, define the handoff: (1) Producer agent: name + one-sentence job, (2) Output schema (markdown / JSON / file path), (3) Out-of-scope items, (4) Consumer agent: name + what it MUST read first, (5) Failure path: if producer output is wrong shape, consumer says STOP. Output as YAML.

2. 研究 → 计划

You are the RESEARCH agent. Output a single markdown doc at `notes/research.md` with: (1) the question, (2) 3 candidate approaches, (3) for each: pros, cons, one risk. Do NOT propose a final plan — that's the plan agent's job. Do NOT write code. Stop after writing the file.

3. 计划 → 实现

You are the PLAN agent. Read `notes/research.md`. Output `notes/plan.md` with: (1) chosen approach + 1-line rationale referencing research, (2) numbered implementation steps (≤ 10), (3) files to touch, (4) tests to add, (5) explicit "do not change" list. No code yet.

4. 实现 → 审查

You are the IMPLEMENT agent. Read `notes/plan.md`. Execute exactly the numbered steps. After each step, append a one-line entry to `notes/progress.md`. Do not deviate from the plan; if you find a problem, write it to `notes/blockers.md` and STOP. No autonomous decisions.

5. 审查 → 发布

You are the REVIEW agent. Read `notes/plan.md` + the diff. For each plan item, mark: DONE / PARTIAL / SKIPPED + evidence (file:line). For SKIPPED / PARTIAL: open a follow-up note. Output `notes/review.md`. Do not change code. If anything is unsafe to ship, write `BLOCK` at top.

6. 需求 → 测试 → 实现

Three-agent chain: (a) SPEC agent: write `spec.md` from `{prdLink}`. (b) TEST agent: write failing tests from spec only, never reading source. (c) IMPL agent: make tests pass without changing tests. Output each agent's instructions separately. No agent does the next agent's job.

可替换变量: prdLink

7. 并行 Agent → 汇总

Run two agents in parallel on the same task: (a) AGENT-A pursues solution X, (b) AGENT-B pursues solution Y. Both write to `notes/<agent>.md`. A third CONSOLIDATE agent reads both, recommends one with rationale, lists differences. Don't let A or B see each other's output.

8. 上下文收尾交接

长会话快超 context 时用。

You are the WRAP agent. Read the current session and write `notes/session-summary.md` covering: (1) what was decided, (2) what code was changed (file:line summary), (3) open questions, (4) the exact next prompt to feed a fresh agent. Don't make new decisions.

9. Router 派发

You are the ROUTER. Given task: {task}, decide which subagent to invoke: RESEARCH (unknowns) / PLAN (knowns, no plan yet) / IMPLEMENT (plan exists) / REVIEW (code exists) / WRAP (long session). Output: { agent, reason, prompt-to-pass }. Don't do the work — only route.

可替换变量: task

10. 失败交接

If you (any agent) cannot complete your task, write `notes/blocker.md` with: (1) what you were doing, (2) what stopped you, (3) what info you need, (4) suggested next agent. Do NOT speculate — leave gaps blank. Pass control back to the router.

11. CLAUDE.md 里的角色定义

Write a `CLAUDE.md` section that defines our subagent roles: RESEARCH / PLAN / IMPLEMENT / REVIEW / WRAP. For each: (1) responsibility, (2) inputs they read, (3) outputs they write, (4) what they MUST NOT do. ≤ 200 words per role. Plain text — no marketing prose.

12. 交接调试

A handoff produced wrong output. Diagnose: (1) Was the contract clear (output shape, out-of-scope list)? (2) Did the consumer agent read the producer's output? (3) Did the producer's context include the consumer's requirements? Output one root cause + one prompt change.

容易踩的坑

  • 没有契约——每次跑都漂移得不一样。
  • 让一个 Agent 又计划又实现——计划只会为它写的代码自圆其说。
  • 把所有上下文喂给所有 Agent——每个都被噪音淹。
  • 没有”卡住就说”的路径——Agent 在不确定时会硬编。
  • 错一步全链路重跑——浪费 token 和时间。
  • Agent 互看草稿——把已决问题再吵一遍。
  • 没有 router——链路变成 if-else 噩梦。

优化技巧

  • 交接走文件,不走对话——文件跨 session 也在。
  • 每个 Agent 读一个文件、写一个文件。
  • 测试由另一个 Agent 写,能抓出更多 bug。
  • 所有 Agent 都内置”卡住就 STOP”指令——比胡编强。
  • 长会话准备 WRAP Agent 当 context 安全网。
  • 角色定义提交进 CLAUDE.md,让全队共享心智模型。
  • 记录每次交接(Agent / 文件 / SHA),方便排错。

FAQ

  • 为什么不写一个大 prompt?: 研究、决策、实现混一起,漂移不可避免。多 Agent 强制分离关注点。
  • 多 Agent 不更费 token 吗?: 是更费,但失败的单体跑会因重试更费。多 Agent 失败得快。
  • 能并行跑吗?: 工作独立时能(模板 7),否则串行。
  • Reviewer 该读计划吗?: 该读,否则没法评估完整度。
  • 怎么避免”全能 Agent”?: 每个角色一个职责,指令 ≤ 200 字。
  • 什么时候多 Agent 是过度设计?: 30 分钟内能搞完、或动 3 个以内文件的任务。

相关阅读

标签: #Prompt #编程 #Claude Code #Agents #多 Agent