OpenClaw 多代理路由系统:构建你的 AI 团队
OpenClaw 的多代理路由系统让你能够创建多个专门的 AI 助手,每个代理拥有独立的工作空间、模型配置和工具权限。本文将深入讲解如何构建你的 AI 团队,实现工作/生活分离、专业化分工和团队协作。
一、为什么需要多代理?
1.1 单代理的局限性
| 问题 | 说明 | 影响 |
|---|---|---|
| 上下文污染 |
| AI 理解混乱 | 权限过大 | 所有通道都有完全访问权 | 安全风险 | 成本不可控 | 所有任务都用最强模型 | 费用过高 | 记忆混乱 |
| 不同场景的记忆互相干扰 | 回答不准确 |
1.2 多代理的优势
| 优势 | 说明 | 价值 | 场景隔离 |
工作/个人/学习完全分离
| 更好的上下文理解 | 权限控制 | 不同代理不同工具权限 | 更高的安全性 | 成本优化 | 简单任务用便宜模型 | 降低使用成本 | 专业分工 |
| 代码/写作/分析各有专长 | 更好的输出质量 |
1.3 典型多代理架构
<code class="language-"┌─────────────────┐│ OpenClaw │ │ Gateway │ └────────┬────────┘ │ ┌────────────────────┼────────────────────┐ │ │ │ ┌───────▼───────┐ ┌───────▼───────┐ ┌───────▼───────┐ │ 工作代理 │ │ 个人代理 │ │ 开发代理 │ │ (Sonnet) │ │ (GPT-4o) │ │ (Opus) │ │ 只读工作区 │ │ 完全访问 │ │ 代码工具 │ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │ │ │ Slack/Teams WhatsApp Discord/GitHub
二、多代理基础配置
2.1 创建第一个多代理配置
配置文件位置:
~/.openclaw/openclaw.json基础配置:
{
agents: {
defaults: {
workspace: "~/.openclaw/workspace" // 默认工作区
},
list: [
{
id: "main", // 主代理 ID
name: "主助手",
workspace: "~/.openclaw/workspace-main",
model: "anthropic/claude-sonnet-4-5-20260514"
},
{
id: "personal", // 个人代理 ID
name: "个人助手",
workspace: "~/.openclaw/workspace-personal",
model: "openai/gpt-4o"
}
]
}
}
重启 Gateway:
openclaw gateway restart
2.2 代理 ID 命名规范
| 命名 | 说明 | 示例 | 语义化 |
work, personal, dev || 唯一性
| 简洁性
推荐命名:
{
agents: {
list: [
{ id: "work", name: "工作助手" },
{ id: "personal", name: "个人助手" },
{ id: "dev", name: "开发助手" },
{ id: "research", name: "研究助手" }
]
}
}
2.3 设置默认代理
第一个代理自动成为默认:
{
agents: {
list: [
{ id: "main", default: true }, // 明确指定默认
{ id: "work" },
{ id: "personal" }
]
}
}
三、通道绑定配置
3.1 基础绑定
将不同通道绑定到不同代理:
{
agents: {
list: [
{ id: "work", workspace: "~/.openclaw/workspace-work" },
{ id: "personal", workspace: "~/.openclaw/workspace-personal" }
]
},
bindings: [
{
agentId: "work",
match: { channel: "slack" }
},
{
agentId: "work",
match: { channel: "telegram", peer: { kind: "group", id: "work-group" } }
},
{
agentId: "personal",
match: { channel: "whatsapp" }
},
{
agentId: "personal",
match: { channel: "telegram" }
}
]
}
3.2 绑定匹配规则
匹配优先级(从高到低):
1. match.peer
2. match.guildId
3. match.teamId
4. match.accountId
5. match.accountId: ""
6. 默认代理
示例:
{
bindings: [
// 最高优先级:特定群组
{
agentId: "work-team",
match: {
channel: "discord",
guildId: "123456789",
peer: { kind: "channel", id: "dev-channel" }
}
},
// 次优先级:整个服务器
{
agentId: "work",
match: { channel: "discord", guildId: "123456789" }
},
// 最低优先级:默认
{
agentId: "personal",
match: { channel: "discord" }
}
]
}
3.3 多账号绑定
为同一通道的不同账号绑定不同代理:
{
channels: {
whatsapp: {
accounts: {
personal: {},
work: {}
}
}
},
bindings: [
{
agentId: "personal",
match: { channel: "whatsapp", accountId: "personal" }
},
{
agentId: "work",
match: { channel: "whatsapp", accountId: "work" }
}
]
}
四、代理专业化配置
4.1 工作代理配置
特点:
{
agents: {
list: [
{
id: "work",
name: "工作助手",
workspace: "~/.openclaw/workspace-work",
model: "anthropic/claude-sonnet-4-5-20260514",
sandbox: {
mode: "all",
workspaceAccess: "ro", // 只读
scope: "session"
},
tools: {
allow: [
"read",
"sessions_list",
"message",
"web_search",
"web_fetch"
],
deny: [
"write",
"edit",
"exec"
]
},
identity: {
name: "工作助手",
theme: "professional assistant",
emoji: "💼"
}
}
]
}
}
4.2 开发代理配置
特点:
{
agents: {
list: [
{
id: "dev",
name: "开发助手",
workspace: "~/.openclaw/workspace-dev",
model: "anthropic/claude-opus-4-5-20260514",
sandbox: {
mode: "all",
workspaceAccess: "rw", // 读写
scope: "agent"
},
tools: {
allow: [
"read",
"write",
"edit",
"exec",
"process",
"browser",
"sessions_spawn"
]
},
identity: {
name: "开发助手",
theme: "senior engineer",
emoji: "👨💻"
}
}
]
}
}
4.3 个人代理配置
特点:
{
agents: {
list: [
{
id: "personal",
name: "个人助手",
workspace: "~/.openclaw/workspace-personal",
model: "openai/gpt-4o",
sandbox: {
mode: "all",
workspaceAccess: "rw",
scope: "session"
},
tools: {
allow: [""] // 允许所有工具
},
identity: {
name: "小 Cl",
theme: "friendly helper",
emoji: "🦞"
}
}
]
}
}
4.4 公共代理配置
特点:
{
agents: {
list: [
{
id: "public",
name: "公共助手",
workspace: "~/.openclaw/workspace-public",
model: "openai/gpt-4o-mini",
sandbox: {
mode: "all",
workspaceAccess: "none", // 无文件系统访问
scope: "session"
},
tools: {
allow: [
"sessions_list",
"sessions_history",
"message",
"web_search"
],
deny: [
"read",
"write",
"edit",
"exec",
"browser",
"nodes"
]
},
identity: {
name: "AI 助手",
theme: "public helper",
emoji: "🤖"
}
}
]
}
}
五、高级路由策略
5.1 基于内容的路由
根据消息内容自动选择代理:
{
agents: {
list: [
{ id: "code", name: "代码专家" },
{ id: "writing", name: "写作专家" },
{ id: "general", name: "通用助手" }
]
},
bindings: [
{
agentId: "code",
match: { channel: "discord" },
conditions: {
keywords: ["代码", "bug", "function", "class", "import"]
}
},
{
agentId: "writing",
match: { channel: "discord" },
conditions: {
keywords: ["文章", "报告", "邮件", "翻译"]
}
},
{
agentId: "general",
match: { channel: "discord" }
}
]
}
5.2 基于时间的路由
{
agents: {
list: [
{ id: "work-hours", name: "工作时间助手" },
{ id: "off-hours", name: "非工作时间助手" }
]
},
routing: {
schedule: [
{
time: "09:00-18:00",
days: ["mon", "tue", "wed", "thu", "fri"],
agentId: "work-hours"
},
{
time: "18:00-09:00",
agentId: "off-hours"
}
]
}
}
5.3 基于负载的路由
{
agents: {
list: [
{ id: "primary", model: "claude-opus" },
{ id: "secondary", model: "claude-sonnet" },
{ id: "tertiary", model: "gpt-4o-mini" }
]
},
routing: {
loadBalance: {
strategy: "round-robin", // round-robin | least-loaded
thresholds: {
primary: 0.5, // 50% 负载时切换到 secondary
secondary: 0.7 // 70% 负载时切换到 tertiary
}
}
}
}
六、代理间通信
6.1 启用代理间消息
{
tools: {
agentToAgent: {
enabled: true,
allow: ["work", "personal", "dev"] // 允许通信的代理列表
}
}
}
6.2 跨代理任务委派
// 在 dev 代理中
const result = await sessions_spawn({
agentId: "work",
task: "请帮我总结这份文档的要点"
});
6.3 代理协作场景
场景:代码审查
1. 用户发送代码给 dev 代理
2. dev 代理调用 work 代理审查代码规范
3. dev 代理调用 personal 代理检查是否有敏感信息
4. dev 代理汇总所有反馈返回给用户
七、监控与管理
7.1 查看代理状态
列出所有代理
openclaw agents list
查看特定代理状态
openclaw agents status work
查看代理会话
openclaw sessions list --agent work
7.2 代理使用统计
{
agents: {
usage: {
work: {
messages: 1234,
tokens: 500000,
cost: 15.50
},
personal: {
messages: 5678,
tokens: 200000,
cost: 5.20
}
}
}
}
7.3 代理备份
备份特定代理
tar -czf work-backup.tar.gz ~/.openclaw/agents/work/
恢复代理
tar -xzf work-backup.tar.gz -C ~/.openclaw/agents/
八、最佳实践
8.1 安全建议
1. 最小权限原则:每个代理只授予必要的工具权限
2. 工作区隔离:不同代理使用不同的工作区
3. 定期审计:检查代理的使用日志
4. 备份配置:定期备份代理配置
8.2 性能优化
1. 合理分配模型:简单任务用便宜模型
2. 缓存共享:配置共享缓存减少重复请求
3. 会话管理:定期清理不活跃的会话
4. 负载均衡:高负载时自动切换代理
8.3 成本控制
1. 预算限制:为每个代理设置预算
2. 使用告警:达到预算 80% 时告警
3. 模型降级:预算超支时自动降级模型
4. 使用分析:定期分析各代理的使用情况
九、总结
多代理配置检查清单:
- [ ] 定义清晰的代理角色
- [ ] 配置独立的工作区
- [ ] 设置合适的模型
- [ ] 配置通道绑定
- [ ] 限制工具权限
- [ ] 设置身份人设
- [ ] 配置备份策略
关键要点:
- 多代理实现场景隔离和专业分工
- 通道绑定决定消息路由
- 工具权限控制安全风险
- 定期监控和优化配置
下一篇预告:《OpenClaw 安全与访问控制:保护你的 AI 助手》
/* 基础样式 */
.article-container {
max-width: 100%;
margin: 0 auto;
padding: 15px;
font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, sans-serif;
line-height: 1.8;
color: #333;
background: #fff;
}
/* 文章头部 */
.article-header {
border-bottom: 3px solid #4a90e2;
padding-bottom: 20px;
margin-bottom: 25px;
}
.main-title {
font-size: 1.8em;
color: #1a1a1a;
margin-bottom: 12px;
font-weight: 700;
line-height: 1.3;
}
.article-meta {
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: center;
}
.category-badge {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
padding: 4px 12px;
border-radius: 16px;
font-size: 0.85em;
font-weight: 500;
}
.read-time {
color: #666;
font-size: 0.85em;
}
/* 导读框 */
.intro-box {
background: linear-gradient(135deg, #f0f4ff 0%, #e8f0fe 100%);
border-left: 4px solid #4a90e2;
padding: 18px;
margin: 20px 0;
border-radius: 0 8px 8px 0;
display: flex;
gap: 12px;
align-items: flex-start;
}
.intro-icon {
font-size: 1.8em;
flex-shrink: 0;
}
.intro-text {
margin: 0;
color: #444;
font-size: 1em;
line-height: 1.6;
}
/* 目录 */
.toc-container {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
padding: 15px;
margin: 25px 0;
}
.toc-title {
font-size: 1.2em;
color: #333;
margin-bottom: 12px;
font-weight: 600;
}
.toc-list {
list-style: none;
padding: 0;
margin: 0;
}
.toc-list li {
margin: 6px 0;
}
.toc-list a {
color: #4a90e2;
text-decoration: none;
display: block;
padding: 6px 10px;
border-radius: 4px;
font-size: 0.95em;
transition: all 0.2s;
}
.toc-list a:hover {
background: #4a90e2;
color: #fff;
}
/* 文章内容 */
.article-content {
font-size: 1em;
}
.article-title {
font-size: 1.6em;
color: #1a1a1a;
margin: 35px 0 18px;
padding-bottom: 8px;
border-bottom: 2px solid #4a90e2;
}
.section-title {
font-size: 1.4em;
color: #2c3e50;
margin: 30px 0 12px;
padding-left: 12px;
border-left: 4px solid #4a90e2;
}
.subsection-title {
font-size: 1.2em;
color: #34495e;
margin: 22px 0 10px;
}
.subsubsection-title {
font-size: 1.05em;
color: #666;
margin: 18px 0 8px;
}
/* 代码块 – 支持横向滚动 */
.code-wrapper {
margin: 18px 0;
border-radius: 6px;
overflow: hidden;
}
.code-block {
background: #1e1e1e;
padding: 15px;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
margin: 0;
border-radius: 6px;
}
.code-block code {
color: #d4d4d4;
font-family: “Fira Code”, “Consolas”, “Monaco”, monospace;
font-size: 0.85em;
line-height: 1.6;
white-space: pre;
}
.inline-code {
background: #f5f5f5;
color: #e83e8c;
padding: 2px 6px;
border-radius: 3px;
font-family: “Fira Code”, “Consolas”, monospace;
font-size: 0.9em;
}
/* 表格 – 响应式滚动 */
.table-wrapper {
margin: 20px 0;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.styled-table {
width: 100%;
border-collapse: collapse;
min-width: 600px;
font-size: 0.9em;
}
.styled-table thead {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
}
.styled-table th {
padding: 12px 15px;
text-align: left;
font-weight: 600;
white-space: nowrap;
}
.styled-table td {
padding: 12px 15px;
border-bottom: 1px solid #e1e4e8;
vertical-align: top;
}
.styled-table tbody tr {
transition: background 0.2s;
}
.styled-table tbody tr:hover {
background: #f8f9fa;
}
.styled-table tbody tr:nth-child(even) {
background: #fafbfc;
}
.styled-table tbody tr:nth-child(odd) {
background: #fff;
}
/* 列表 */
.styled-list {
list-style: none;
padding: 0;
margin: 15px 0;
}
.list-item {
padding: 6px 0 6px 22px;
position: relative;
color: #333;
}
.list-item::before {
content: “•”;
position: absolute;
left: 0;
color: #4a90e2;
font-weight: bold;
font-size: 1.2em;
}
.checkbox-item {
padding: 6px 0 6px 32px;
position: relative;
color: #333;
}
.checkbox-item.checked {
color: #28a745;
}
/* 信息框 */
.info-box {
background: #e8f4fd;
border-left: 4px solid #4a90e2;
padding: 15px;
margin: 18px 0;
border-radius: 0 6px 6px 0;
color: #2c3e50;
}
/* 分隔线 */
.section-divider {
border: none;
height: 2px;
background: linear-gradient(to right, transparent, #4a90e2, transparent);
margin: 35px 0;
}
/* 文章尾部 */
.article-footer {
margin-top: 40px;
padding-top: 25px;
border-top: 2px solid #e9ecef;
}
.tags-section {
margin-bottom: 20px;
}
.tag-label {
font-weight: 600;
color: #666;
margin-right: 8px;
}
.tag {
display: inline-block;
background: #f0f4ff;
color: #4a90e2;
padding: 4px 10px;
border-radius: 12px;
margin: 3px;
font-size: 0.85em;
text-decoration: none;
transition: all 0.2s;
}
.tag:hover {
background: #4a90e2;
color: #fff;
}
.back-to-top {
text-align: center;
padding: 15px;
background: #f8f9fa;
border-radius: 6px;
}
.back-to-top a {
color: #4a90e2;
text-decoration: none;
font-weight: 500;
}
/* 移动端优化 */
@media (max-width: 768px) {
.article-container {
padding: 12px;
}
.main-title {
font-size: 1.4em;
}
.article-title {
font-size: 1.3em;
}
.section-title {
font-size: 1.2em;
}
.code-block {
padding: 12px;
font-size: 0.8em;
}
.styled-table {
font-size: 0.85em;
min-width: 500px;
}
.styled-table th,
.styled-table td {
padding: 10px 12px;
}
.intro-box {
padding: 15px;
}
.intro-icon {
font-size: 1.5em;
}
}
@media (max-width: 480px) {
.main-title {
font-size: 1.3em;
}
.article-meta {
flex-direction: column;
gap: 8px;
}
.category-badge,
.read-time {
font-size: 0.8em;
}
.code-block {
font-size: 0.75em;
padding: 10px;
}
.styled-table {
min-width: 450px;
font-size: 0.8em;
}
.styled-table th,
.styled-table td {
padding: 8px 10px;
}
}
// 移动端表格提示
document.addEventListener(‘DOMContentLoaded’, function() {
var tables = document.querySelectorAll(‘.table-wrapper’);
tables.forEach(function(table) {
if (!table.querySelector(‘.scroll-hint’)) {
var hint = document.createElement(‘div’);
hint.className = ‘scroll-hint’;
hint.innerHTML = ‘👈 左右滑动查看更多’;
hint.style.cssText = ‘text-align:center;padding:8px;background:#f8f9fa;color:#666;font-size:12px;border-radius:0 0 8px 8px;’;
table.parentNode.insertBefore(hint, table.nextSibling);
}
});
});