18

Multi-Agent Patterns

⏱️ 35分钟

Multi-Agent Architecture Patterns

Multi-agent architectures 把工作分配给多个 language model 实例,每个实例有自己的 Context window。设计得好能突破单一 agent 限制,设计得差会引入协调成本。关键洞察:sub-agents 的核心价值是 Context isolation,而不是角色扮演。

如果你把 multi-agent 当成“角色扮演”,你可能会得到更复杂但不更好的系统。真正的价值是:Context isolation + 并行化。

  • Use multi-agent to isolate Context, not to role-play.
  • Supervisor / swarm / hierarchical 是主流模式。
  • Token 成本很高,只有复杂任务才值得。
  • 避免 telephone-game,允许 direct pass-through。
  • 明确 handoff 与 convergence 规则。

你将学到什么

  • 什么时候需要 multi-agent,什么时候不需要
  • 三种架构模式的优缺点
  • 如何设计协作与收敛机制

When to Activate

Activate this skill when:

  • Single-agent Context limits constrain task complexity
  • Tasks decompose naturally into parallel subtasks
  • Different subtasks require different tool sets or system prompts
  • Building systems that must handle multiple domains simultaneously
  • Scaling agent capabilities beyond single-context limits
  • Designing production agent systems with multiple specialized components

Core Concepts

Multi-agent systems 通过 Context 分发解决单 agent 限制。三种主流模式:supervisor/orchestrator、peer-to-peer/swarm、hierarchical。核心设计原则是 Context isolation。

有效的 multi-agent 系统需要显式 coordination protocols、避免 sycophancy 的 consensus 机制,以及对 bottlenecks、divergence、error propagation 的关注。

Detailed Topics

Why Multi-Agent Architectures

The Context Bottleneck 单 agent 会受到 reasoning、Context 管理与 tool 协调上限。任务复杂度提升后,Context 塞满 history、docs 与 tool outputs,导致 lost-in-middle、attention scarcity、Context poisoning 等退化。

Multi-agent 通过分拆任务到多个 Context window,减少单一 Context 的负担。

The Token Economics Reality Multi-agent 消耗显著更多 tokens:

ArchitectureToken MultiplierUse Case
Single agent chat1× baselineSimple queries
Single agent with tools~4× baselineTool-using tasks
Multi-agent system~15× baselineComplex research/coordination

研究显示 performance variance 主要由 token usage、tool calls、model choice 决定。更强模型(如 Claude Sonnet 4.5、GPT-5.2 thinking mode)往往比纯堆 tokens 更有效。

The Parallelization Argument 很多任务可并行拆分:多个来源检索、多个文档分析、对比不同方案。单 agent 必须串行处理;multi-agent 可以并行,时间接近最长子任务而非总和。

The Specialization Argument 不同任务需要不同 system prompts 与 tool sets。multi-agent 允许 specialization,而不让单 agent 背负所有配置。

Architectural Patterns

Pattern 1: Supervisor/Orchestrator 中心 supervisor 控制流程、分派任务、汇总结果。

User Query -> Supervisor -> [Specialist, Specialist, Specialist] -> Aggregation -> Final Output

适用:任务清晰、需要协调多个领域、需要 human oversight。

优势:控制力强。

劣势:supervisor Context 易成为瓶颈,容易出现 telephone game。

The Telephone Game Problem and Solution LangGraph benchmarks 显示 supervisor 架构容易丢失细节。

解决方式:让 sub-agent 直接 pass-through response:

def forward_message(message: str, to_user: bool = True):
    """
    Forward sub-agent response directly to user without supervisor synthesis.
    """
    if to_user:
        return {"type": "direct_response", "content": message}
    return {"type": "supervisor_input", "content": message}

Pattern 2: Peer-to-Peer/Swarm 无中心控制,agent 之间直接 handoff。

def transfer_to_agent_b():
    return agent_b

agent_a = Agent(
    name="Agent A",
    functions=[transfer_to_agent_b]
)

适用:探索型任务、需求不稳定、需要弹性协作。

优势:无单点瓶颈。

劣势:协调复杂、易发散。

Pattern 3: Hierarchical 多层级拆分:strategy / planning / execution。

Strategy Layer -> Planning Layer -> Execution Layer

适用:大型项目、企业流程、需要长期规划的任务。

Context Isolation as Design Principle

Context isolation 是 multi-agent 的核心价值。每个 agent 在干净的 Context 中完成子任务。

Isolation Mechanisms

  • Full context delegation
  • Instruction passing
  • File system memory

三者的权衡取决于 task complexity 与 latency 要求。

Consensus and Coordination

The Voting Problem 简单多数投票会把弱模型 hallucination 与强模型 reasoning 当成同等权重。

Weighted Voting / Debate Protocols 更可靠的做法是 weighted voting 或 debate。

Trigger-Based Intervention 设置 stall triggers 与 sycophancy triggers。

Practical Guidance

Failure Modes and Mitigations

  • Supervisor bottleneck → output schema + checkpointing
  • Coordination overhead → clear handoff + batching
  • Divergence → convergence checks + TTL
  • Error propagation → validate outputs + retry

Examples

Example 1: Research Team Architecture

Supervisor
├── Researcher
├── Analyzer
├── Fact-checker
└── Writer

Example 2: Handoff Protocol

def handle_customer_request(request):
    if request.type == "billing":
        return transfer_to(billing_agent)
    elif request.type == "technical":
        return transfer_to(technical_agent)
    elif request.type == "sales":
        return transfer_to(sales_agent)
    else:
        return handle_general(request)

Decision Helper: Do You Need Multi-Agent?

  • 任务是否能拆成并行子任务?
  • 单 agent 是否已接近 Context 上限?
  • 是否需要不同的 tool sets 或 system prompts?
  • 成本是否可接受(tokens + latency)?

如果 3 条以上为“是”,再考虑 multi-agent。

Guidelines

  1. Design for Context isolation as the primary benefit
  2. Choose architecture based on coordination needs, not metaphor
  3. Implement explicit handoff protocols
  4. Use weighted voting or debate
  5. Monitor for supervisor bottlenecks
  6. Validate outputs before passing
  7. Set TTL limits
  8. Test failure scenarios

Practice Task

  • 给你的项目画一张 multi-agent 架构图
  • 标出每个 agent 的 Context 边界与 tool sets

Integration

This skill builds on context-fundamentals and context-degradation. It connects to:

  • memory-systems
  • tool-design
  • context-optimization

References

External resources:

  • LangGraph Documentation
  • AutoGen Framework
  • CrewAI Documentation
  • Research on Multi-Agent Coordination

Skill Metadata

Created: 2025-12-20 Last Updated: 2025-12-20 Author: Agent Skills for Context Engineering Contributors Version: 1.0.0

📚 相关资源

❓ 常见问题

关于本章主题最常被搜索的问题,点击展开答案

Multi-agent 的核心价值到底是什么?是 "角色扮演" 吗?

不是角色扮演。核心价值是 Context isolation + 并行化。每个 sub-agent 在干净的 context 中完成子任务,避免单 agent 把 history、docs、tool outputs 全部塞满后出现 lost-in-middle、attention scarcity、context poisoning。如果你把 multi-agent 当成 "研究员 + 分析师 + 编辑" 的角色游戏,你只是把系统做复杂了,没做更好。真正的判断标准:sub-agent 是不是在 isolate context,还是只在演角色?

Supervisor、Swarm、Hierarchical 三种模式怎么选?

Supervisor / Orchestrator:中心节点分派任务、汇总结果,控制力强,但 supervisor context 易成瓶颈,容易 telephone game;适合任务清晰、需协调多领域、需要 human oversight。Peer-to-peer / Swarm:无中心,agent 之间直接 handoff(OpenAI Swarm 是典型),无单点瓶颈但易发散;适合探索型任务、需求不稳定。Hierarchical:strategy → planning → execution 多层拆分;适合大型项目、企业流程、长期规划。规模小先试 supervisor,发散就往 hierarchical,弹性需求高用 swarm。

Multi-agent 多花多少 token?这个成本什么时候值得?

本章给出 token 倍数:单 agent chat 1× baseline、单 agent + tools ~4×、multi-agent system ~15×。15 倍开销不是小数 —— 简单 query 完全不该用 multi-agent。研究显示性能差异主要由 token usage、tool calls、model choice 决定,更强模型(Claude Sonnet 4.5、GPT-5.2 thinking)往往比堆 sub-agent 更有效。值不值的判断:能不能并行拆?单 agent 真的撞 context 上限了吗?需不需要不同的 system prompt / tool sets?三条 yes 才考虑。

Telephone game 问题是什么?怎么避免?

Supervisor 模式下,sub-agent 的回答经过 supervisor "再总结" 后转给用户,每次合成都丢细节,多层之后变成 "传话游戏"。LangGraph benchmarks 验证过这个问题。解决方法:让 sub-agent 直接 pass-through,不让 supervisor 介入合成。本章给的代码模板:`forward_message(message, to_user=True)` 直接把 sub-agent 的 response 作为 `direct_response` 发给用户,跳过 supervisor。该汇总的还是汇总,但能直传的内容(如详细数据、长格式输出)走直传通道。

Multi-agent 的简单多数投票为什么不可靠?

因为它把弱模型的 hallucination 和强模型的 reasoning 当成同等权重 —— 三个 agent 投票,两个用 GPT-3.5 的乱猜票就能压过一个 Claude Sonnet 的正确答案。可靠做法两种:(1) Weighted voting,按模型能力加权;(2) Debate protocols,让 agent 互相挑战论据。再加上 trigger-based intervention(stall trigger + sycophancy trigger)防止 agent 集体附和。本章给出的设计原则:监控 systematic bias 比追求绝对一致率更重要。