OpenClaw多Agent实战教程:打造专属AI团队

前言

在上一篇文章中,我们介绍了OpenClaw的整体架构。今天我们深入探讨一个强大的功能:多Agent路由

想象一下,你能否拥有:

  • 一个”工作助手”,专注代码和技术问题
  • 一个”生活助手”,处理日常事务
  • 一个”家庭助手”,在家庭群聊中服务

OpenClaw的多Agent功能让这一切成为可能——一个Gateway,多个独立”大脑”

什么是”一个Agent”?

在OpenClaw中,一个Agent是一个完全独立的AI人格,拥有自己的:

组件 说明
Workspace 工作目录,包含AGENTS.md、SOUL.md等配置
AgentDir 状态目录,存储认证配置和模型设置
Sessions 独立的会话存储,与其他Agent完全隔离
Auth Profiles 独立的认证配置,不自动共享
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~/.openclaw/
├── agents/
│ ├── main/ # 主Agent
│ │ ├── agent/
│ │ │ └── auth-profiles.json
│ │ └── sessions/
│ ├── coding/ # 编程Agent
│ │ ├── agent/
│ │ └── sessions/
│ └── family/ # 家庭Agent
│ ├── agent/
│ └── sessions/
├── workspace/ # main的工作区
├── workspace-coding/ # coding的工作区
└── workspace-family/ # family的工作区

为什么需要多Agent?

场景1:多用户共享

一家人或小团队共享一个Gateway,但每个人有自己的AI助手:

1
2
3
4
5
6
7
8
9
┌─────────────────────────────────────────────────┐
│ Gateway │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Alex │ │ Mia │ │ Dad │ │
│ │ Agent │ │ Agent │ │ Agent │ │
│ └────┬────┘ └────┬────┘ └────┬────┘ │
└───────┼────────────┼────────────┼──────────────┘
│ │ │
+15550001 +15550002 +15550003 (WhatsApp)

场景2:不同用途,不同”性格”

  • 日常聊天:使用Claude Sonnet,快速响应
  • 深度工作:使用Claude Opus,代码质量更高
  • 家庭群聊:限制工具权限,安全第一

场景3:多账号管理

  • 工作用WhatsApp账号 → 工作Agent
  • 个人用WhatsApp账号 → 个人Agent
  • Telegram机器人 → 客服Agent

快速入门:创建你的第一个多Agent系统

Step 1:创建Agent

使用向导创建新Agent:

1
2
3
4
5
# 创建coding agent
openclaw agents add coding

# 创建social agent
openclaw agents add social

这会自动创建:

  • 工作区:~/.openclaw/workspace-coding
  • 状态目录:~/.openclaw/agents/coding/

Step 2:验证Agent

1
openclaw agents list

输出示例:

1
2
3
4
5
Agent ID    Workspace                    Default
--------- --------------------------- -------
main ~/.openclaw/workspace yes
coding ~/.openclaw/workspace-coding no
social ~/.openclaw/workspace-social no

Step 3:配置Binding

编辑 ~/.openclaw/openclaw.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
agents: {
list: [
{ id: "main", workspace: "~/.openclaw/workspace", default: true },
{ id: "coding", workspace: "~/.openclaw/workspace-coding" },
{ id: "social", workspace: "~/.openclaw/workspace-social" },
],
},

bindings: [
// Telegram -> coding agent
{ agentId: "coding", match: { channel: "telegram" } },
// WhatsApp -> social agent
{ agentId: "social", match: { channel: "whatsapp" } },
// 其他 -> main agent (default)
],
}

Step 4:重启并验证

1
2
openclaw gateway restart
openclaw agents list --bindings

技术原理:路由机制

Binding的核心结构

1
2
3
4
5
6
7
8
9
10
11
12
13
{
agentId: "coding", // 目标Agent
match: { // 匹配规则
channel: "whatsapp", // 渠道
accountId: "personal", // 账号ID(可选)
peer: { // 对端(可选)
kind: "direct", // direct | group | channel
id: "+15551234567" // 具体ID
},
guildId: "12345", // Discord服务器ID
teamId: "T12345", // Slack团队ID
},
}

路由优先级(从高到低)

OpenClaw使用确定性路由最具体的匹配优先

1
2
3
4
5
6
7
8
1. peer 精确匹配       (特定用户/群组/频道)
2. parentPeer 匹配 (线程继承)
3. guildId + roles (Discord角色路由)
4. guildId (Discord服务器)
5. teamId (Slack团队)
6. accountId (账号匹配)
7. channel (渠道级别)
8. default agent (默认Agent)

关键规则

  • 同层级多个匹配时,配置文件中先出现的胜出
  • 多个匹配字段是AND关系(必须全部满足)

路由示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bindings: [
// 最高优先级:特定DM
{
agentId: "vip",
match: { channel: "whatsapp", peer: { kind: "direct", id: "+1555VIP001" } },
},
// 次优先级:特定群组
{
agentId: "work",
match: { channel: "whatsapp", peer: { kind: "group", id: "1203630...@g.us" } },
},
// 通道级别:其余WhatsApp消息
{ agentId: "social", match: { channel: "whatsapp" } },
// 其他通道走默认
]

实战案例

案例1:一个WhatsApp,多人共享

一个WhatsApp号码,不同用户路由到不同Agent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
agents: {
list: [
{ id: "alex", workspace: "~/.openclaw/workspace-alex" },
{ id: "mia", workspace: "~/.openclaw/workspace-mia" },
],
},
bindings: [
{
agentId: "alex",
match: { channel: "whatsapp", peer: { kind: "direct", id: "+15551230001" } },
},
{
agentId: "mia",
match: { channel: "whatsapp", peer: { kind: "direct", id: "+15551230002" } },
},
],
channels: {
whatsapp: {
dmPolicy: "allowlist",
allowFrom: ["+15551230001", "+15551230002"],
},
},
}

注意:回复都来自同一个WhatsApp号码,但每个用户有独立的AI”大脑”。

案例2:按通道分配模型

WhatsApp用Sonnet(快速),Telegram用Opus(深度):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
agents: {
list: [
{
id: "chat",
name: "Everyday",
workspace: "~/.openclaw/workspace-chat",
model: "anthropic/claude-sonnet-4-5",
},
{
id: "opus",
name: "Deep Work",
workspace: "~/.openclaw/workspace-opus",
model: "anthropic/claude-opus-4-6",
},
],
},
bindings: [
{ agentId: "chat", match: { channel: "whatsapp" } },
{ agentId: "opus", match: { channel: "telegram" } },
],
}

案例3:Discord多机器人

每个Discord Bot对应一个Agent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
agents: {
list: [
{ id: "main", workspace: "~/.openclaw/workspace-main" },
{ id: "coding", workspace: "~/.openclaw/workspace-coding" },
],
},
bindings: [
{ agentId: "main", match: { channel: "discord", accountId: "default" } },
{ agentId: "coding", match: { channel: "discord", accountId: "coding" } },
],
channels: {
discord: {
accounts: {
default: {
token: "DISCORD_BOT_TOKEN_MAIN",
guilds: {
"1234567890": {
channels: {
"2222222222": { allow: true },
},
},
},
},
coding: {
token: "DISCORD_BOT_TOKEN_CODING",
guilds: {
"1234567890": {
channels: {
"3333333333": { allow: true },
},
},
},
},
},
},
},
}

案例4:家庭群聊Agent

专用家庭Agent,带工具限制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
agents: {
list: [
{
id: "family",
name: "Family Bot",
workspace: "~/.openclaw/workspace-family",
identity: { name: "Family Bot" },
groupChat: {
mentionPatterns: ["@family", "@familybot"],
},
sandbox: { mode: "all", scope: "agent" },
tools: {
allow: ["read"],
deny: ["exec", "write", "edit", "browser", "nodes"],
},
},
],
},
bindings: [
{
agentId: "family",
match: {
channel: "whatsapp",
peer: { kind: "group", id: "120363999999@g.us" },
},
},
],
}

案例5:多个WhatsApp账号

工作和个人分开:

1
2
3
# 登录两个账号
openclaw channels login --channel whatsapp --account personal
openclaw channels login --channel whatsapp --account biz
1
2
3
4
5
6
7
8
9
10
11
12
{
agents: {
list: [
{ id: "home", workspace: "~/.openclaw/workspace-home" },
{ id: "work", workspace: "~/.openclaw/workspace-work" },
],
},
bindings: [
{ agentId: "home", match: { channel: "whatsapp", accountId: "personal" } },
{ agentId: "work", match: { channel: "whatsapp", accountId: "biz" } },
],
}

高级配置

Agent-to-Agent通信

默认关闭,需要显式启用:

1
2
3
4
5
6
7
8
{
tools: {
agentToAgent: {
enabled: true,
allow: ["home", "work"], // 允许通信的Agent对
},
},
}

Per-Agent沙箱和工具限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
agents: {
list: [
{
id: "personal",
workspace: "~/.openclaw/workspace-personal",
sandbox: { mode: "off" }, // 无沙箱
// 无工具限制
},
{
id: "family",
workspace: "~/.openclaw/workspace-family",
sandbox: {
mode: "all",
scope: "agent",
docker: {
setupCommand: "apt-get update && apt-get install -y git",
},
},
tools: {
allow: ["read"],
deny: ["exec", "write", "edit"],
},
},
],
},
}

自定义Agent人格

每个Agent的工作区可以有不同的配置文件:

workspace-coding/SOUL.md:

1
2
3
4
5
6
你是Coding Agent,专注于:
- 代码审查和优化
- 技术问题解答
- 系统架构建议

风格:简洁、技术性强、直接给出代码示例

workspace-family/SOUL.md:

1
2
3
4
5
6
你是Family Bot,服务家庭群聊:
- 回答日常问题
- 提醒家庭事务
- 轻松友好的语气

风格:温暖、幽默、避免技术术语

常见问题

Q: Agent之间会话会混淆吗?

不会。每个Agent有独立的session存储,完全隔离。

Q: 能否让多个Agent共享认证?

默认不共享。如需共享,手动复制 auth-profiles.json

1
2
cp ~/.openclaw/agents/main/agent/auth-profiles.json \
~/.openclaw/agents/coding/agent/

Q: 如何调试路由?

1
2
3
4
5
# 查看当前binding配置
openclaw agents list --bindings

# 查看通道状态
openclaw channels status --probe

Q: 群聊消息如何路由?

群聊使用 peer.kind: "group" 匹配,建议配合 mentionPatterns 使用:

1
2
3
4
5
{
groupChat: {
mentionPatterns: ["@coding", "@助手"],
},
}

总结

OpenClaw多Agent功能让你能够:

  1. 隔离人格:不同用途不同”大脑”
  2. 多用户支持:一个Gateway服务多人
  3. 灵活路由:按通道、账号、用户精确匹配
  4. 安全控制:Per-Agent沙箱和工具限制
  5. 模型选择:不同Agent使用不同模型

通过合理配置Bindings,你可以构建一个完整的”AI团队”,各司其职,高效协作。


相关链接

感谢你的阅读,如果文章对你有帮助,可以请作者喝杯茶!