OpenClaw 多代理路由系统:构建你的 AI 团队

OpenClaw 多代理路由系统:构建你的 AI 团队

💡

OpenClaw 的多代理路由系统让你能够创建多个专门的 AI 助手,每个代理拥有独立的工作空间、模型配置和工具权限。本文将深入讲解如何构建你的 AI 团队,实现工作/生活分离、专业化分工和团队协作。

# OpenClaw 多代理路由系统:构建你的 AI 团队


一、为什么需要多代理?

1.1 单代理的局限性

问题 说明 影响
上下文污染
工作和个人对话混在一起

AI 理解混乱 权限过大 所有通道都有完全访问权 安全风险 成本不可控 所有任务都用最强模型 费用过高 记忆混乱 不同场景的记忆互相干扰 回答不准确

1.2 多代理的优势

优势 说明 价值
场景隔离

工作/个人/学习完全分离

更好的上下文理解 权限控制 不同代理不同工具权限 更高的安全性 成本优化 简单任务用便宜模型 降低使用成本 专业分工 代码/写作/分析各有专长 更好的输出质量

1.3 典型多代理架构

                   ┌─────────────────┐
│ 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 |

| 唯一性| 每个代理 ID 必须唯一 | 不能重复 |

| 简洁性| 简短易记 | 避免过长 |

推荐命名:

{
  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- Discord 服务器

3. match.teamId- Slack 团队

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 助手》

(0)
AIgeoAIgeo
上一篇 2026年3月23日 下午10:39
下一篇 2026年3月23日 下午10:43

相关文章

  • ChatGPT 完整使用教程_从入门到精通

    ChatGPT 完整使用教程,包含基础使用、高级技巧和应用场景。

  • AI 视频生成工具实战:Runway、Pika、Sora 技术解析与应用场景

    视频生成的新纪元 如果说 2022 年是 AI 绘图的元年,那么 2024 年就是 AI 视频的爆发之年。从几秒钟的简单动画到分钟级的连贯视频,AI 视频生成技术的发展速度令人惊叹。对于内容创作者来说,这意味着全新的可能性。 我跟踪测试了市面上主流的 AI 视频生成工具,从 Runway 到 Pika,从 Stable Video Diffusion 到各大…

  • AI 写作工具完全指南:从入门到精通的实战教程

    AI 写作工具的崛起 内容创作正在经历一场革命。过去需要数小时甚至数天完成的文章,现在借助 AI 工具可能只需要几十分钟。但这并不意味着人类创作者会被取代,而是工作方式发生了根本性变化。 我在过去两年里深度使用了各种 AI 写作工具,从通用大模型到垂直写作助手,从免费工具到企业级方案。这些经验让我对如何有效使用 AI 写作工具有了系统性认识。今天把这些经验整…

  • OpenClaw 故障排除手册:常见问题快速解决

    OpenClaw 故障排除手册:常见问题快速解决 📚 AI 工具使用教程 ⏱️ 阅读时间:约 20 分钟 💡 使用 OpenClaw 时遇到问题?本手册汇总了最常见的问题和解决方案,帮助你快速诊断和修复问题。从安装问题到运行时错误,覆盖全方位故障排除。 📑 目录导航 📌 一、快速诊断流程 📌 二、安装问题 📌 三、Gateway 问题 📌 四、通道问题 📌 …

  • AI 绘图工具 Midjourney 与 Stable Diffusion 深度对比与使用指南

    AI 绘图时代的开启 2022 年,AI 绘图工具的出现让内容创作领域再次震动。从文字描述到精美图像,这个过程曾经需要专业设计师数小时的工作,现在普通人几分钟就能完成。但面对众多 AI 绘图工具,如何选择?如何使用?这是很多初学者面临的问题。 我系统性地使用过主流 AI 绘图工具,从 Midjourney 到 Stable Diffusion,从 DALL-…

  • OpenClaw 记忆系统详解:让 AI 真正记住你

    OpenClaw 记忆系统详解:让 AI 真正记住你 📚 AI 工具使用教程 ⏱️ 阅读时间:约 20 分钟 💡 记忆是 AI 助手的核心能力。本文深入讲解 OpenClaw 的记忆系统,包括短期会话记忆、长期 MEMORY.md、每日记忆文件等。学会配置和管理记忆,让你的 AI 助手真正了解你。 📑 目录导航 📌 一、记忆系统架构 📌 二、会话记忆配置 📌…