🌙 开场白
2026年6月29日凌晨3点,我看着一个 Agent 在那里单打独斗,不禁陷入了沉思。
一个 Agent 很强大,但如果是 一群 Agent 呢?
就像《功夫》里的斧头帮 —— 一个斧头帮成员不厉害,但 100 个斧头帮成员就问你怕不怕?人多力量大,前提是有人指挥。
🎼 五大编配模式
Supervisor 模式
一个主管 Agent 分配任务,多个 Worker Agent 执行
适合:明确分工的场景
Pipeline 模式
Agent 按顺序处理,一个的输出是下一个的输入
适合:数据处理流程
Fan-Out/Fan-In 模式
先分发任务给多个 Agent,再汇总结果
适合:并行处理场景
Swarm 模式
对等 Agent 自由协作,无中心节点
适合:复杂的任务分解
Debate 模式
多个 Agent 相互辩论,达成共识
适合:需要多重论证的决策
在 OpenClaw 中,这些模式通过 Sub-Agent 机制实现。一个主 Agent 可以 spawn 多个子 Agent,各自负责不同任务。
👑 实战:Supervisor 模式
场景:每天的内容运营工厂
妙趣AI 每天需要完成:新闻采集、内容撰写、SEO优化、社区发布、数据分析。一个人忙不过来,需要一支 Agent 团队。
Supervisor
Researcher
Writer
Reviewer
Publisher
// 在 OpenClaw 中配置 Supervisor 模式
// openclaw.config.yaml
agents:
- name: "supervisor"
role: "supervisor"
model: "claude-3.5-sonnet"
system_prompt: |
你是内容运营的 Supervisor,负责:
1. 分析当天的内容需求
2. 委派任务给合适的 Agent
3. 检查工作成果
4. 协调冲突
- name: "researcher"
role: "worker"
skills: ["web-search", "web-fetch", "rss-reader"]
schedule: "0 8 * * *"
system_prompt: |
你是新闻采集员,善于从各种渠道搜索 AI 新闻。
- name: "writer"
role: "worker"
skills: ["ai-text-humanizer", "write"]
system_prompt: |
你是妙趣风格的写手,能把技术内容翻译成人话。
- name: "reviewer"
role: "worker"
skills: ["seo-audit", "spell-check"]
system_prompt: |
你是质量检查员,确保内容没有技术错误。
- name: "publisher"
role: "worker"
skills: ["discord-webhook", "feishu-im-message"]
system_prompt: |
你是发布专员,负责分发内容到各个渠道。
workflow:
steps:
- agent: "supervisor"
action: "分析今天的内容需求"
- agent: "supervisor"
action: "委派 researcher 采集新闻"
- agent: "researcher"
action: "执行新闻搜索"
parallel: true
- agent: "writer"
action: "撰写新闻稿件"
depends_on: "researcher"
- agent: "reviewer"
action: "审核内容质量"
depends_on: "writer"
- agent: "supervisor"
action: "最终确认"
depends_on: "reviewer"
- agent: "publisher"
action: "发布到各渠道"
depends_on: "supervisor"
🐝 高级实战:Swarm 模式
让 Agent 自行组队
在 Swarm 模式中,没有中心化的 Supervisor。Agent 们根据任务自行组队、协作、竞争。
// OpenClaw Swarm 模式配置
swarm:
name: "problem-solving-swarm"
agents:
- name: "analyzer"
role: "分析任务需求"
activation: "passive" # 等待任务触发
- name: "brainstormer"
role: "提出解决方案"
activation: "on_needed"
- name: "critic"
role: "反驳并优化方案"
activation: "automatic" # 自动参与
- name: "validator"
role: "验证最终方案"
activation: "on_consensus"
rules:
discussion_rounds: 3 # 最多讨论 3 轮
consensus_threshold: 0.75 # 75% 同意即可
timeout_seconds: 120 # 超时自动裁决
leader_election:
enabled: true
criteria: "best_confidence"
// 手动触发 Swarm
swarm.problem_solving({
problem: "如何提升 SEO 收录率?",
context: {
current_rate: "65%",
target_rate: "85%",
budget: "$500/month"
}
})
Swarm 模式的好处
| 特性 | Supervisor 模式 | Swarm 模式 |
|---|---|---|
| 决策方式 | 自上而下(主管决定) | 自下而上(多方共识) |
| 容错性 | 中等(主管挂则全挂) | 高(可自我重组) |
| 适用场景 | 明确分工的任务 | 创新和复杂问题 |
| 可预测性 | 高(流程固定) | 低(结果不可预测) |
| 实现复杂度 | 低 | 高 |
⚔️ 进阶:Debate 模式
让 Agent 打辩论赛
有时候,一个 Agent 的决定需要经过多方辩论。Debate 模式让多个 Agent 扮演不同的角色,就同一个问题展开辩论。
// OpenClaw Debate 模式
debate:
topic: "是否应该使用 Agent Context Caching?"
duration_rounds: 3
agents:
- name: "advocate"
stance: "pro"
arguments: [
"减少 60% 的 API 调用成本",
"响应速度提升 3 倍",
"对缓存命中率 > 30% 的场景效果显著"
]
- name: "skeptic"
stance: "con"
arguments: [
"缓存可能导致数据过时",
"增加了系统的复杂度",
"对实时性要求高的场景不适用"
]
- name: "judge"
role: "中立裁判"
criteria: [
"成本效益分析",
"技术可行性",
"风险等级"
]
# 辩论结果
decision: "有条件使用"
conditions: [
"热点数据缓存,TTL ≤ 5 分钟",
"实时数据不做缓存",
"缓存失败时自动降级到实时查询"
]
🌟 妙趣洞察: Debate 模式最有趣的地方在于,即使 Agent 最终同意了,你也无法确认它是真的被说服了,还是只是不想继续辩论了 —— 这和人类吵架一模一样!
📡 Agent 之间的通信协议
多 Agent 协作需要一个高效的通信协议。OpenClaw 支持多种通信方式:
| 协议 | 方式 | 延迟 | 适用场景 |
|---|---|---|---|
| MCP 直接调用 | 点对点 | ⭐ 低 | 紧急任务、指令下发 |
| A2A 协议 | 标准化的 Agent-to-Agent | ⭐⭐ 中 | 跨系统协作 |
| 共享记忆 | 通过共享数据库 | ⭐⭐⭐ 高 | 异步协作、信息共享 |
| 事件总线 | 发布-订阅模式 | ⭐⭐ 中 | 广播通知、事件驱动 |
| Session 间通信 | OpenClaw Session Send | ⭐⭐ 中 | 跨 session 协作 |
🎯 OpenClaw 实战:Agent 团队协作
完整代码:多 Agent 内容工厂
// multi-agent-factory.js
const { spawnSubAgent, orchestrate } = require('@openclaw/agent-framework');
async function contentFactory() {
// 1. Supervisor 分析需求
const demand = await spawnSubAgent('supervisor', {
task: 'analyze_demand',
context: { date: new Date(), user: 'miaoquai' }
});
// 2. 并行派遣多个 Worker
const [researcher, competitor] = await Promise.all([
spawnSubAgent('researcher', {
task: 'collect_news',
params: { topics: demand.topics, count: 5 }
}),
spawnSubAgent('researcher', {
task: 'monitor_competitors',
params: { competitors: demand.competitors }
})
]);
// 3. 写手生成内容
const writer = await spawnSubAgent('writer', {
task: 'generate_content',
params: {
news: researcher.results,
competitors: competitor.results,
style: demand.content_style
}
});
// 4. Reviewer 审核
const review = await spawnSubAgent('reviewer', {
task: 'review_content',
params: { content: writer.content }
});
if (review.failed_items.length > 0) {
// 5. 如果有问题,返回写手修改
const revised = await spawnSubAgent('writer', {
task: 'revise_content',
params: {
content: writer.content,
issues: review.failed_items
}
});
// 使用修订版
writer.content = revised.content;
}
// 6. 发布
await Promise.all([
spawnSubAgent('publisher', {
task: 'publish_web',
params: { content: writer.content }
}),
spawnSubAgent('publisher', {
task: 'publish_discord',
params: { content: writer.content.summary }
})
]);
return { status: 'success', content_id: writer.content.id };
}
// 定时执行
cron.schedule('0 8 * * *', () => contentFactory(), {
timezone: 'Asia/Shanghai',
description: '每日内容工厂'
});
📋 多 Agent 协作最佳实践
1. 职责明确
每个 Agent 应该有明确的职责边界。不要出现两个 Agent 都能发 Discord 消息的冲突。
2. 错误隔离
一个 Agent 失败不应该影响其他 Agent:
// 错误隔离示例
try {
await spawnSubAgent('agent_a', task);
} catch (e) {
console.error('Agent A 失败:', e);
// 不影响 Agent B 和 C
}
await Promise.all([
spawnSubAgent('agent_b', task_b),
spawnSubAgent('agent_c', task_c)
]);
3. 资源竞争避免
避免多个 Agent 同时读写同一个文件:
// 使用文件锁
orchestrate.withLock('sitemap.xml', async () => {
await agent_a.write_sitemap_entry();
await agent_b.write_sitemap_entry();
});
4. 进度可观测
每个 Agent 的执行状态都应该可追踪:
// OpenClaw 内置 telemetry
orchestrate.on('agent_complete', (event) => {
console.log(`Agent ${event.agent} 完成:
- 任务: ${event.task}
- 耗时: ${event.duration}ms
- 结果: ${event.status}
`);
});
❓ 常见问题
Q1: 什么时候使用多 Agent,什么时候单 Agent 就够了?
单 Agent:任务明确、步骤固定、不需要外部知识。比如"搜索+总结+输出"。
多 Agent:任务复杂的分解、需要不同领域知识、需要多方验证。比如"市场分析+竞品对比+策略建议"。
Q2: 多 Agent 的成本会飙升吗?
取决于你的架构设计。好的设计可以控制成本:
- 使用小型模型(如 DeepSeek)执行简单任务
- 只在需要时 spawn 子 Agent
- 重用子 Agent 的上下文(不用每次都重新加载)
Q3: 如何调试多 Agent 系统?
OpenClaw 提供 Agent Trace 功能:
// 开启追踪
orchestrate.enableTrace({
save_to: '/var/log/agent-traces/',
include_context: true,
include_tool_calls: true
});
// 查看某个执行链
trace.show('content-factory-2026-06-29');
// 输出:
// Supervisor → Researcher → Writer → Reviewer → Publisher
// ├── Researcher (2.3s) ✅ → 5 条新闻
// ├── Writer (4.1s) ✅ → 3000 字
// ├── Reviewer (1.2s) ⚠️ → 2 个拼写错误
// ├── Writer (0.8s) ✅ → 修改完成
// └── Publisher (1.5s) ✅ → 已发布
// 总计:9.9s
🎬 总结
周星驰在《少林足球》里说:"球,不是这样踢的。"
多 Agent 编排也是 —— Agent,不是这样用的。单独一个 Agent 是工具,一群 Agent 是团队。
记住三句话:
- 👑 主管模式最稳 —— 90% 的场景都适用
- 🐝 Swarm 模式最酷 —— 但复杂度也最高
- ⚔️ Debate 模式最有深度 —— 但可能是自己跟自己吵架
- 从 Supervisor 模式开始,搭建你的第一个多 Agent 系统
- 使用 OpenClaw 的
sessions_spawn创建子 Agent - 监控 Agent 协作的性能和成本
- 逐步尝试更高级的 Swarm 和 Debate 模式