10. OpenClaw 多 Agent 路由与会话隔离
适用于 OpenClaw v2026.2 | 本文适合团队使用或需要隔离上下文的用户。
TL;DR: 多 Agent 配置:"agents": {"instances": {"personal": {...}, "work": {...}}}。路由规则:"routing": {"rules": [{"match": {"channel": "telegram"}, "agent": "personal"}]}。会话隔离:"sessions": {"isolation": "user"}。沙箱模式:"sandbox": {"mode": "non-main"}。适合多用户、多项目、多用途场景。
为什么需要多 Agent?
单 Agent 的局限
场景
单 Agent 问题
多 Agent 解决方案
多用户
上下文混乱,隐私问题
每用户独立 Agent
多项目
项目信息混在一起
每项目独立 Agent
多用途
一个 Agent 多种角色
专门化的 Agent
多团队
权限难以控制
按团队隔离 Agent
成本控制
无法区分用量
每个模型独立计费
多 Agent 使用场景
flowchart TB
subgraph Users["用户"]
U1[用户A]
U2[用户B]
U3[团队X]
U4[团队Y]
end
subgraph Router["消息路由"]
R[路由规则]
end
subgraph Agents["Agent 实例"]
A1[个人Agent A]
A2[个人Agent B]
A3[团队Agent X]
A4[团队Agent Y]
end
subgraph Models["模型"]
M1[Claude Opus]
M2[Claude Sonnet]
M3[GPT-4o]
M4[Haiku]
end
U1 --> R
U2 --> R
U3 --> R
U4 --> R
R --> A1
R --> A2
R --> A3
R --> A4
A1 --> M1
A2 --> M2
A3 --> M3
A4 --> M4
多 Agent 配置
基本结构
{
"agents": {
"defaults": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspace"
},
"instances": {
"personal": {
"model": "anthropic/claude-opus-4-6",
"workspace": "~/.openclaw/workspaces/personal",
"thinkingLevel": "high"
},
"work": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspaces/work",
"skills": ["github", "jira", "confluence"]
},
"assistant": {
"model": "anthropic/claude-haiku-3.5",
"workspace": "~/.openclaw/workspaces/assistant"
}
}
}
}
Agent 配置项
配置项
说明
示例
model
使用的模型
anthropic/claude-opus-4-6
workspace
工作目录
~/.openclaw/workspaces/personal
thinkingLevel
思考级别
high, medium, low
skills
启用的技能
["gmail", "calendar"]
sandbox
沙箱配置
{ "mode": "non-main" }
contextWindow
上下文窗口
200000
Workspace 结构
~/.openclaw/
├── workspace/ # 默认 workspace
│ ├── AGENTS.md
│ ├── SOUL.md
│ └── skills/
├── workspaces/
│ ├── personal/ # 个人 Agent
│ │ ├── AGENTS.md
│ │ ├── SOUL.md
│ │ └── USER.md
│ ├── work/ # 工作 Agent
│ │ ├── AGENTS.md
│ │ └── skills/
│ └── assistant/ # 助手 Agent
│ └── AGENTS.md
└── openclaw.json # 主配置
消息路由规则
路由配置
{
"routing": {
"rules": [
{
"match": {
"channel": "telegram",
"sender": "123456789"
},
"agent": "personal"
},
{
"match": {
"channel": "discord",
"guild": "987654321"
},
"agent": "work"
},
{
"match": {
"session": "main"
},
"agent": "personal"
}
],
"default": "personal"
}
}
路由规则详解
匹配字段
说明
示例
channel
渠道类型
telegram, discord, slack
sender
发送者 ID
123456789
guild
Discord 服务器 ID
987654321
group
群组 ID
-100123456789
session
会话 ID
main, telegram:user:123
pattern
正则匹配
^work-
复杂路由示例
{
"routing": {
"rules": [
{
"name": "个人 Telegram",
"match": {
"channel": "telegram",
"sender": ["123456789", "987654321"]
},
"agent": "personal"
},
{
"name": "工作 Discord",
"match": {
"channel": "discord",
"guild": "111222333"
},
"agent": "work"
},
{
"name": "工作群组",
"match": {
"channel": "telegram",
"group": "-100111222333"
},
"agent": "work"
},
{
"name": "测试环境",
"match": {
"pattern": "^test-"
},
"agent": "assistant",
"config": {
"sandbox": { "mode": "always" }
}
}
],
"default": "personal"
}
}
路由优先级
flowchart TB
MSG[消息到达] --> R1{匹配规则 1?}
R1 -->|是| A1[Agent 1]
R1 -->|否| R2{匹配规则 2?}
R2 -->|是| A2[Agent 2]
R2 -->|否| R3{匹配规则 3?}
R3 -->|是| A3[Agent 3]
R3 -->|否| D[默认 Agent]
路由按顺序匹配,第一个匹配的规则生效。
会话隔离
隔离级别
级别
说明
使用场景
无隔离
共享上下文
单用户、单用途
渠道隔离
按渠道独立上下文
多渠道接入
用户隔离
按用户独立上下文
多用户共享
会话隔离
每个会话独立
完全隔离
Agent 隔离
每个 Agent 独立
多 Agent 部署
配置会话隔离
{
"sessions": {
"isolation": "user",
"persistence": true,
"retention": {
"maxAge": 604800000,
"maxSessions": 100
}
}
}
隔离配置详解
{
"sessions": {
"isolation": "user",
"storage": {
"type": "file",
"path": "~/.openclaw/sessions"
},
"encryption": {
"enabled": true,
"algorithm": "aes-256-gcm"
},
"retention": {
"maxAge": 604800000,
"maxSessions": 100,
"cleanupInterval": 86400000
}
}
}
配置项
说明
默认值
isolation
隔离级别
user
storage.type
存储类型
file
encryption.enabled
启用加密
true
retention.maxAge
最大保留时间
7 天
retention.maxSessions
最大会话数
100
团队协作场景
场景一:小型团队共享
{
"agents": {
"instances": {
"team": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspaces/team",
"skills": ["github", "jira", "slack"]
}
}
},
"routing": {
"rules": [
{
"match": { "channel": "slack" },
"agent": "team"
},
{
"match": { "channel": "discord" },
"agent": "team"
}
],
"default": "team"
}
}
场景二:部门隔离
{
"agents": {
"instances": {
"engineering": {
"model": "anthropic/claude-opus-4-6",
"workspace": "~/.openclaw/workspaces/engineering",
"skills": ["github", "jira", "pagerduty"],
"sandbox": { "mode": "off" }
},
"marketing": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspaces/marketing",
"skills": ["mailchimp", "analytics"],
"sandbox": { "mode": "non-main" }
},
"support": {
"model": "anthropic/claude-haiku-3.5",
"workspace": "~/.openclaw/workspaces/support",
"skills": ["zendesk", "intercom"],
"sandbox": { "mode": "always" }
}
}
},
"routing": {
"rules": [
{
"name": "Engineering Discord",
"match": { "channel": "discord", "guild": "111111111" },
"agent": "engineering"
},
{
"name": "Marketing Slack",
"match": { "channel": "slack", "team": "T22222222" },
"agent": "marketing"
},
{
"name": "Support Telegram",
"match": { "channel": "telegram", "group": "-100333333333" },
"agent": "support"
}
],
"default": "support"
}
}
场景三:多租户 SaaS
{
"agents": {
"dynamic": true,
"template": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspaces/tenants/{{tenantId}}",
"skills": ["base"],
"sandbox": { "mode": "always" }
},
"limits": {
"maxAgents": 100,
"maxSessionsPerAgent": 50,
"maxTokensPerMonth": 1000000
}
},
"routing": {
"rules": [
{
"name": "Tenant Routing",
"match": { "header": "X-Tenant-Id" },
"agent": "tenant_{{tenantId}}",
"createIfNotExists": true
}
]
}
}
权限与安全
Agent 权限控制
{
"agents": {
"instances": {
"trusted": {
"model": "anthropic/claude-opus-4-6",
"sandbox": { "mode": "off" },
"permissions": {
"filesystem": { "read": true, "write": true },
"network": { "outbound": true },
"process": { "execute": true }
}
},
"restricted": {
"model": "anthropic/claude-haiku-3.5",
"sandbox": { "mode": "always" },
"permissions": {
"filesystem": {
"read": ["~/allowed/"],
"write": ["~/allowed/"]
},
"network": {
"outbound": ["api.example.com"]
},
"process": { "execute": false }
}
}
}
}
}
沙箱模式
模式
说明
适用场景
off
无沙箱
可信 Agent
non-main
非主会话沙箱
团队共享
always
始终沙箱
不信任环境
沙箱配置
{
"sandbox": {
"mode": "non-main",
"container": {
"image": "openclaw/sandbox:latest",
"timeout": 30000,
"memory": "512m",
"cpu": "0.5"
},
"allowlist": ["read", "write", "edit", "bash"],
"denylist": ["browser", "canvas", "nodes"],
"networkPolicy": {
"allow": ["api.example.com"],
"deny": ["*"]
}
}
}
监控与管理
Agent 状态监控
# 列出所有 Agent 实例
openclaw agents list
# 输出示例:
# Name Model Sessions Status Last Active
# personal claude-opus-4-6 3 active 2 min ago
# work claude-sonnet-4 5 active 5 min ago
# assistant claude-haiku-3.5 2 idle 1 hour ago
# 查看 Agent 详情
openclaw agents show personal
# 查看路由规则
openclaw routing list
会话管理
# 列出所有会话
openclaw sessions list --agent personal
# 输出示例:
# Session ID Channel Sender Created Messages
# telegram:user:123 telegram 123456789 2026-02-26 42
# main cli - 2026-02-25 128
# 查看会话详情
openclaw sessions show telegram:user:123
# 导出会话
openclaw sessions export telegram:user:123 --format json
# 删除会话
openclaw sessions delete telegram:user:123
使用量统计
# 查看按 Agent 分组的使用量
openclaw usage --by-agent
# 输出示例:
# Agent Tokens In Tokens Out Cost Requests
# personal 125,000 45,000 $2.10 128
# work 89,000 32,000 $1.25 95
# assistant 15,000 8,000 $0.15 42
# 按时间范围统计
openclaw usage --by-agent --from 2026-02-01 --to 2026-02-28
最佳实践
1. 合理划分 Agent
划分维度
说明
示例
用途
按功能用途划分
coding, research, assistant
项目
按项目划分
project-alpha, project-beta
团队
按团队划分
engineering, marketing
模型
按模型划分
opus, sonnet, haiku
2. 配置继承
{
"agents": {
"defaults": {
"model": "anthropic/claude-sonnet-4",
"workspace": "~/.openclaw/workspace",
"thinkingLevel": "medium",
"sandbox": { "mode": "non-main" }
},
"instances": {
"opus": {
"model": "anthropic/claude-opus-4-6",
"thinkingLevel": "high"
// 继承 defaults 的 workspace 和 sandbox
},
"haiku": {
"model": "anthropic/claude-haiku-3.5",
"thinkingLevel": "minimal"
// 继承 defaults 的其他配置
}
}
}
}
3. 权限最小化
{
"agents": {
"instances": {
"restricted": {
"model": "anthropic/claude-haiku-3.5",
"sandbox": { "mode": "always" },
"permissions": {
"filesystem": {
"read": ["~/allowed/read"],
"write": ["~/allowed/write"]
},
"network": {
"outbound": []
},
"process": {
"execute": false
}
}
}
}
}
}
4. 监控和告警
{
"monitoring": {
"alerts": [
{
"condition": "usage > 1000000",
"action": "notify"
},
{
"condition": "error_rate > 0.1",
"action": "notify"
},
{
"condition": "response_time > 10000",
"action": "notify"
}
],
"webhook": "https://your-monitoring.com/webhook"
}
}
故障排查
路由不生效
# 查看当前路由规则
openclaw routing list
# 测试路由
openclaw routing test --channel telegram --sender 123456789
# 输出示例:
# Matched Rule: 个人 Telegram
# Target Agent: personal
# Route Path: default -> rule 1 -> personal
会话隔离问题
# 检查会话隔离配置
openclaw config get sessions.isolation
# 检查会话列表
openclaw sessions list --all
# 重置会话隔离
openclaw sessions reset --agent personal
权限问题
# 检查权限配置
openclaw agents permissions show personal
# 测试权限
openclaw agents test-permission personal --action filesystem.read --path ~/test
# 输出示例:
# Permission: filesystem.read
# Path: ~/test
# Result: ALLOWED
小结
多 Agent 架构是 OpenClaw 的高级功能,为团队和企业场景提供了强大的支持:
灵活路由:按渠道、用户、项目分配不同 Agent
会话隔离:保护隐私,避免上下文混乱
权限控制:精细化的权限管理
成本优化:按用途选择不同模型
可扩展性:支持动态创建和管理 Agent
本篇小结:
理解了多 Agent 架构的价值和场景
掌握了 Agent 配置和消息路由规则
学会了会话隔离的配置方法
了解了团队协作场景的最佳实践
掌握了权限控制和监控管理
更新记录:
2026-02-26:初版发布,基于 OpenClaw v2026.2
系列导航:
← 上一篇:Web Control UI 与 Canvas
→ 下一篇:安全配置最佳实践