logo
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

Internal reference:

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