第3课:多个助手#

为什么需要这个#

想象一下,你只有一个秘书,既要处理工作问题,又要处理私人问题,还要和你的家人沟通。这很不方便。最好分工:一个助手负责工作,一个负责家庭,一个负责公共机器人。在 OpenClaw 中,这叫做 multi-agent — 一个系统中多个独立的"大脑"。

什么是"代理"#

代理(Agent) 是一个完全独立的助手,拥有自己的:

  • 工作空间(workspace)— 文件、AGENTS.md、个性设置
  • 状态存储(agentDir)— 授权信息、历史记录
  • 对话历史(sessions)— 每个代理只记住自己的聊天
  • 技能(skills)— 自己的一套能力

默认情况下,OpenClaw 使用一个代理 — main。但你可以添加任意数量。

如何添加新代理#

步骤1:创建代理#

openclaw agents add work
openclaw agents add family

每个代理都会获得自己的文件夹和设置。

步骤2:绑定频道(bindings)#

绑定 是一种规则,告诉系统:“来自这个频道的消息发给这个代理”。

~/.openclaw/openclaw.json 中的配置示例:

{
  agents: {
    list: [
      { id: "main", workspace: "~/.openclaw/workspace-main" },
      { id: "work", workspace: "~/.openclaw/workspace-work" }
    ]
  },
  bindings: [
    { agentId: "main", match: { channel: "whatsapp" } },
    { agentId: "work", match: { channel: "telegram" } }
  ]
}

在这个例子中:WhatsApp → 主代理,Telegram → 工作代理。

步骤3:重启并验证#

openclaw gateway restart
openclaw agents list --bindings

路由规则#

OpenClaw 如何决定将消息发给哪个代理:

  1. 特定聊天(peer)— 最精确:“这个号码 → 这个代理”
  2. 频道账户(accountId)— “这个 WhatsApp 账户的所有消息 → 这个代理”
  3. 频道 — “所有 Telegram 消息 → 这个代理”
  4. 默认 — 设置了 default: true 的代理,或列表中的第一个

更精确的规则总是优先于通用规则。

配置示例#

不同 Telegram 机器人处理不同任务#

{
  agents: {
    list: [
      { id: "main", workspace: "~/.openclaw/workspace-main" },
      { id: "alerts", workspace: "~/.openclaw/workspace-alerts" }
    ]
  },
  bindings: [
    { agentId: "main", match: { channel: "telegram", accountId: "default" } },
    { agentId: "alerts", match: { channel: "telegram", accountId: "alerts" } }
  ],
  channels: {
    telegram: {
      accounts: {
        default: { botToken: "123456:ABC..." },
        alerts: { botToken: "987654:XYZ..." }
      }
    }
  }
}

一个 WhatsApp,不同的人对应不同的代理#

{
  bindings: [
    {
      agentId: "alex",
      match: { channel: "whatsapp", peer: { kind: "direct", id: "+15551230001" } }
    },
    {
      agentId: "mia",
      match: { channel: "whatsapp", peer: { kind: "direct", id: "+15551230002" } }
    }
  ]
}

快速聊天 + 深度工作#

{
  agents: {
    list: [
      { id: "chat", model: "anthropic/claude-sonnet-4-5" },
      { id: "opus", model: "anthropic/claude-opus-4-6" }
    ]
  },
  bindings: [
    { agentId: "chat", match: { channel: "whatsapp" } },
    { agentId: "opus", match: { channel: "telegram" } }
  ]
}

子代理(Sub-agents)#

子代理 是主代理创建的后台助手,用于执行单独的任务。就像你的助手对实习生说:“你去处理这个,我先做别的。”

子代理:

  • 单独的会话中工作(不干扰主对话)
  • 完成后将结果报告回你的聊天
  • 可以使用不同的模型(简单任务用更便宜的模型)

管理子代理#

/subagents list              — 查看活跃的子代理
/subagents kill all           — 停止所有子代理
/subagents log 1              — 查看第一个的日志
/subagents spawn work "查找关于..."  — 手动启动

配置#

{
  agents: {
    defaults: {
      subagents: {
        maxConcurrent: 8,        // 最大同时数量
        maxSpawnDepth: 2,        // 允许嵌套
        archiveAfterMinutes: 60  // 一小时后归档
      }
    }
  }
}

嵌套子代理#

默认情况下,子代理不能创建自己的子代理。但如果设置 maxSpawnDepth: 2,就可以形成链:

你 → 主代理 → 子代理(协调者)→ 子子代理(执行者)

最大深度:5层(但建议用2层)。

隔离与安全#

每个代理:

  • 自己的授权文件(不与其他代理共享密码)
  • 可以有自己的沙箱和工具限制
  • 看不到其他代理的对话历史

示例 — 有限制的公共代理:

{
  agents: {
    list: [
      {
        id: "public",
        sandbox: { mode: "all" },
        tools: {
          allow: ["read"],
          deny: ["write", "exec", "browser"]
        }
      }
    ]
  }
}

课程总结#

  • 代理 是一个独立的"大脑",拥有自己的文件、历史和设置
  • 绑定(bindings) 将频道的消息路由到正确的代理
  • 更精确的路由规则优先于通用规则
  • 子代理 是用于并行任务的后台助手
  • 每个代理可以有自己的沙箱和工具集
  • 使用 openclaw agents list --bindings 检查设置