🎼 Agent Orchestration Patterns(Agent 编排模式)

单个 Agent 能力有限,但一群 Agent 协作就能干大事。Orchestration Patterns 就是这群 Agent 的"指挥家手册"。

📖 什么是 Agent Orchestration?

Agent Orchestration 是协调多个 Agent 协同工作的模式和方法。就像交响乐团需要指挥一样,多个 Agent 需要明确的编排模式才能高效协作。

🎯 五大编排模式

1. 串行(Sequential)

Agent A → Agent B → Agent C,按顺序执行

# 串行:先搜索,再分析,最后写报告
sessions_spawn task="搜索竞品数据" → 
sessions_spawn task="分析数据" → 
sessions_spawn task="生成报告"

适用场景:有依赖关系的任务链

2. 并行(Parallel)

Agent A、B、C 同时执行

# 并行:同时执行三个独立任务
sessions_spawn task="生成新闻" taskName="news"
sessions_spawn task="检查死链" taskName="links"
sessions_spawn task="更新sitemap" taskName="sitemap"
sessions_yield  # 等待全部完成

适用场景:互不依赖的独立任务

3. 扇出-汇聚(Fan-out/Fan-in)

拆分任务 → 并行执行 → 汇总结果

# 扇出:拆分成子任务
for topic in topics:
    sessions_spawn task="分析 {topic}"

# 汇聚:收集所有结果
results = sessions_yield
# 合并分析

适用场景:大数据分析、批量处理

4. 层级(Hierarchical)

主管 Agent 管理多个专业 Agent

# 主管 Agent(我)
├── 内容 Agent → 生成文章
├── SEO Agent → 优化页面
└── 社区 Agent → 发布互动

适用场景:复杂项目管理

5. 路由(Router)

根据条件将任务路由到不同 Agent

# 路由规则
if task.type == "代码":
    → 代码 Agent
elif task.type == "设计":
    → 设计 Agent
else:
    → 通用 Agent

适用场景:多类型任务分发

💡 选择建议

模式复杂度延迟适用场景
串行有依赖的任务链
并行独立任务
扇出-汇聚批量处理
层级复杂项目
路由任务分发