多智能体系统协调专门的组件来处理复杂的工作流程。然而,并非每个复杂任务都需要这种方法——单个智能体配合正确的(有时是动态的)工具和提示词通常可以实现类似的结果。

为什么选择多智能体?

当开发者说他们需要"多智能体"时,通常是在寻找以下一种或多种能力:
  • 上下文管理 :在不压垮模型上下文窗口的情况下提供专门的知识。如果上下文是无限的且延迟为零,您可以将所有知识放入单个提示词中——但由于实际情况并非如此,您需要模式来有选择地呈现相关信息。
  • 分布式开发 :允许不同团队独立开发和维护功能,将它们组合成具有清晰边界的更大系统。
  • 并行化 :为子任务生成专门的工作者并同时执行它们以获得更快的结果。
当单个智能体有太多 工具 且难以决定使用哪个时,当任务需要具有大量上下文的专业知识(长提示词和特定领域工具)时,或者当您需要强制执行只有在满足某些条件后才能解锁功能的顺序约束时,多智能体模式特别有价值。
多智能体设计的核心是 上下文工程 ——决定每个智能体看到什么信息。系统的质量取决于确保每个智能体都能访问其任务所需的正确数据。

模式

以下是构建多智能体系统的主要模式,每种模式适合不同的用例:
模式 工作原理
Subagents 主智能体将子智能体作为工具进行协调。所有路由都通过主智能体,由它决定何时以及如何调用每个子智能体。
Handoffs 行为根据状态动态变化。工具调用更新状态变量,触发路由或配置更改,切换智能体或调整当前智能体的工具和提示词。
Skills 按需加载的专业提示词和知识。单个智能体保持控制,同时根据需要从技能中加载上下文。
Router 路由步骤对输入进行分类并将其定向到一个或多个专门的智能体。结果被综合成组合响应。
Custom workflow 使用 LangGraph 构建定制的执行流程,混合确定性逻辑和智能体行为。将其他模式作为节点嵌入您的工作流中。

选择模式

使用此表格将您的需求与正确的模式进行匹配:
模式 分布式开发 并行化 多跳 直接用户交互
Subagents ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Handoffs ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Skills ⭐⭐⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Router ⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐
  • 分布式开发 :不同的团队能否独立维护组件?
  • 并行化 :多个智能体能否同时执行?
  • 多跳 :该模式是否支持连续调用多个子智能体?
  • 直接用户交互 :子智能体能否直接与用户对话?
您可以混合使用模式!例如, 子智能体 架构可以调用工具,这些工具可以调用自定义工作流或路由器智能体。子智能体甚至可以使用 技能 模式按需加载上下文。可能性是无限的!

可视化概览

A main agent coordinates subagents as tools. All routing passes through the main agent.
Subagents pattern: main agent coordinates subagents as tools

Performance comparison

Different patterns have different performance characteristics. Understanding these tradeoffs helps you choose the right pattern for your latency and cost requirements. Key metrics:
  • Model calls : Number of LLM invocations. More calls = higher latency (especially if sequential) and higher per-request API costs.
  • Tokens processed : Total context window usage across all calls. More tokens = higher processing costs and potential context limits.

One-shot request

User: “Buy coffee”
A specialized coffee agent/skill can call a buy_coffee tool.
Pattern Model calls Best fit
Subagents 4
Handoffs 3
Skills 3
Router 3
4 model calls:
Subagents one-shot: 4 model calls for buy coffee request
Key insight: Handoffs, Skills, and Router are most efficient for single tasks (3 calls each). Subagents adds one extra call because results flow back through the main agent—this overhead provides centralized control.

Repeat request

Turn 1: “Buy coffee” Turn 2: “Buy coffee again”
The user repeats the same request in the same conversation.
Pattern Turn 2 calls Total (both turns) Best fit
Subagents 4 8
Handoffs 2 5
Skills 2 5
Router 3 6
4 calls again → 8 total
  • Subagents are stateless by design —each invocation follows the same flow
  • The main agent maintains conversation context, but subagents start fresh each time
  • This provides strong context isolation but repeats the full flow
Key insight: Stateful patterns (Handoffs, Skills) save 40-50% of calls on repeat requests. Subagents maintain consistent cost per request—this stateless design provides strong context isolation but at the cost of repeated model calls.

Multi-domain

User: “Compare Python, JavaScript, and Rust for web development”
Each language agent/skill contains ~2000 tokens of documentation. All patterns can make parallel tool calls.
Pattern Model calls Total tokens Best fit
Subagents 5 ~9K
Handoffs 7+ ~14K+
Skills 3 ~15K
Router 5 ~9K
5 calls, ~9K tokens
Subagents multi-domain: 5 calls with parallel execution
Each subagent works in isolation with only its relevant context. Total: 9K tokens .
Key insight: For multi-domain tasks, patterns with parallel execution (Subagents, Router) are most efficient. Skills has fewer calls but high token usage due to context accumulation. Handoffs is inefficient here—it must execute sequentially and can’t leverage parallel tool calling for consulting multiple domains simultaneously.

Summary

Here’s how patterns compare across all three scenarios:
Pattern One-shot Repeat request Multi-domain
Subagents 4 calls 8 calls (4+4) 5 calls, 9K tokens
Handoffs 3 calls 5 calls (3+2) 7+ calls, 14K+ tokens
Skills 3 calls 5 calls (3+2) 3 calls, 15K tokens
Router 3 calls 6 calls (3+3) 5 calls, 9K tokens
Choosing a pattern:
Optimize for Subagents Handoffs Skills Router
Single requests
Repeat requests
Parallel execution
Large-context domains
Simple, focused tasks

Connect these docs to Claude, VSCode, and more via MCP for real-time answers.