多智能体路由
在一个运行的 Gateway(网关) 中运行多个 隔离的 智能体——每个智能体都有自己的工作区、状态目录(agentDirGateway(网关))和会话历史记录——以及多个渠道账户(例如两个 WhatsApp)。入站消息通过绑定路由到正确的智能体。
此处的 智能体(agent) 是完整的、针对特定人设的范围:工作区文件、身份验证配置文件、模型注册表和会话存储。agentDir 是磁盘上的状态目录,用于在 ~/.openclaw/agents/<agentId>/SlackWhatsApp 保存此针对特定智能体的配置。绑定(binding) 将渠道账户(例如 Slack 工作区或 WhatsApp 号码)映射到这些智能体之一。
什么是“一个智能体”?
Section titled “什么是“一个智能体”?”智能体(agent) 是一个具有完整范围的“大脑”,拥有自己的:
- 工作区(Workspace)(文件、AGENTS.md/SOUL.md/USER.md、本地笔记、人设规则)。
- 状态目录(State directory)(
agentDir),用于存放身份验证配置文件、模型注册表和针对特定智能体的配置。 - 会话存储(Session store)(聊天历史记录 + 路由状态),位于
~/.openclaw/agents/<agentId>/sessions下。
身份验证配置文件是 针对特定智能体(per-agent) 的。每个智能体从自己的以下位置读取:
~/.openclaw/agents/<agentId>/agent/auth-profiles.jsonSkills 会从每个代理的工作区以及 ~/.openclaw/skills 等共享根目录加载,然后在配置时根据有效的代理 Skills 允许列表进行过滤。使用 agents.defaults.skills 作为共享基线,使用 agents.list[].skills 进行每个代理的替换。请参阅 Skills: per-agent vs shared 和 Skills: agent skill allowlists。
Gateway 可以并排托管一个代理(默认)或多个代理。
路径(快速地图)
Section titled “路径(快速地图)”- 配置:
~/.openclaw/openclaw.json(或OPENCLAW_CONFIG_PATH) - 状态目录:
~/.openclaw(或OPENCLAW_STATE_DIR) - 工作区:
~/.openclaw/workspace(或~/.openclaw/workspace-<agentId>) - 代理目录:
~/.openclaw/agents/<agentId>/agent(或agents.list[].agentDir) - 会话:
~/.openclaw/agents/<agentId>/sessions
单代理模式(默认)
Section titled “单代理模式(默认)”如果您不进行任何操作,OpenClaw 将运行单个代理:
agentId默认为main。- 会话的键为
agent:main:<mainKey>。 - 工作区默认为
~/.openclaw/workspace(或在设置了OPENCLAW_PROFILE时为~/.openclaw/workspace-<profile>)。 - 状态默认为
~/.openclaw/agents/main/agent。
使用代理向导添加一个新的隔离代理:
openclaw agents add work然后添加 bindings (或者让向导来完成)以路由传入消息。
使用以下命令验证:
openclaw agents list --bindings创建每个代理工作区
使用向导或手动创建工作区:
Terminal window openclaw agents add codingopenclaw agents add social每个代理都有自己的工作区,包含
SOUL.md、AGENTS.md和可选的USER.md,以及位于 `~/.openclaw/agents/下的专用agentDir` 和会话存储。创建渠道账号
添加代理、账号和绑定
在
agents.list下添加代理,在 `channels..accounts
下添加渠道账号,并使用bindings` 将它们连接起来(示例如下)。重启并验证
Terminal window openclaw gateway restartopenclaw agents list --bindingsopenclaw channels status --probe
多个代理 = 多个人物,多种个性
Section titled “多个代理 = 多个人物,多种个性”使用多个代理时,每个 agentId 都会变成一个完全隔离的人物设定:
- 不同的电话号码/账号(针对每个渠道
accountId)。 - 不同的个性(每个代理的工作区文件,如
AGENTS.md和SOUL.md)。 - 独立的认证 + 会话(除非明确启用,否则不会相互串扰)。
这允许多个人共享一个 Gateway(网关) 服务器,同时保持他们的 AI “大脑”和数据相互隔离。
跨代理 QMD 记忆搜索
Section titled “跨代理 QMD 记忆搜索”如果一个代理应该搜索另一个代理的 QMD 会话记录,请在 agents.list[].memorySearch.qmd.extraCollections 下添加额外的集合。仅当每个代理都应继承相同的共享记录集合时,才使用 agents.defaults.memorySearch.qmd.extraCollections。
{ agents: { defaults: { workspace: "~/workspaces/main", memorySearch: { qmd: { extraCollections: [{ path: "~/agents/family/sessions", name: "family-sessions" }], }, }, }, list: [ { id: "main", workspace: "~/workspaces/main", memorySearch: { qmd: { extraCollections: [{ path: "notes" }], // resolves inside workspace -> collection named "notes-main" }, }, }, { id: "family", workspace: "~/workspaces/family" }, ], }, memory: { backend: "qmd", qmd: { includeDefaultMemory: false }, },}额外的集合路径可以在代理之间共享,但当路径位于代理工作区之外时,集合名称保持显式。工作区内的路径仍属于代理作用域,因此每个代理保留自己的记录搜索集。
一个 WhatsApp 号码,多个人(私信拆分)
Section titled “一个 WhatsApp 号码,多个人(私信拆分)”您可以在保持一个 WhatsApp 账户的同时,将不同的 WhatsApp 私信路由到不同的代理。使用 peer.kind: "direct"WhatsApp 匹配发送者 E.164(例如 WhatsAppWhatsApp+15551234567)。回复仍来自同一个 WhatsApp 号码(没有每个代理的发送者身份)。
示例:
{ 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 账户的全局设置(配对/允许列表),而不是针对每个代理的。
- 对于共享群组,将群组绑定到一个代理或使用广播群组。
路由规则(消息如何选择代理)
Section titled “路由规则(消息如何选择代理)”绑定是确定性的,且最具体者优先:
peer match
精确的私信/群组/渠道 id。
parentPeer match
线程继承。
guildId + roles
Discord 角色路由。
guildId
Discord。
teamId
Slack。
accountId match for a 渠道
每个账户的后备选项。
渠道级匹配
accountId: "*"。默认代理
回退到
agents.list[].default,否则回退到列表中的第一个条目,默认值:main。
决胜局与 AND 语义
- 如果在同一层级有多个绑定匹配,则配置顺序中靠前的那个获胜。
- 如果一个绑定设置了多个匹配字段(例如
peer+guildId),则所有指定的字段都是必需的(AND语义)。
账号范围详情
- 省略
accountId的绑定仅匹配默认账号。 - 使用
accountId: "*"OpenClaw 作为跨所有账号的渠道范围回退。 - 如果您稍后为同一个代理添加了带有显式账号 ID 的相同绑定,OpenClaw 会将现有的仅渠道绑定升级为账号范围绑定,而不是复制它。
多个账号 / 电话号码
Section titled “多个账号 / 电话号码”支持 多个账号 的渠道(如 WhatsApp)使用 WhatsAppaccountId 来标识每个登录。每个 accountId 可以路由到不同的代理,因此一台服务器可以承载多个电话号码而不会混淆会话。
如果希望在省略 accountId 时拥有渠道范围的默认账号,请设置 channels.<channel>.defaultAccountOpenClaw(可选)。如果未设置,OpenClaw 将回退到 default(如果存在),否则回退到第一个配置的账号 ID(已排序)。
支持此模式的常见渠道包括:
whatsapp、telegram、discord、slack、signal、imessageirc、line、googlechat、mattermost、matrix、nextcloud-talkzalo、zalouser、nostr、feishu
agentId:一个“大脑”(工作区、每代理身份认证、每代理会话存储)。accountIdWhatsApp:一个渠道账户实例(例如 WhatsApp 账户"personal"对比"biz")。binding:通过(channel, accountId, peer)以及可选的公会/团队 ID 将传入消息路由到agentId。- 直接聊天会折叠到
agent:<agentId>:<mainKey>(每代理的“主”会话;session.mainKey)。
Discord每个代理的 Discord 机器人
每个 Discord 机器人账户映射到一个唯一的 accountId。将每个账户绑定到一个代理,并保持每个机器人的允许列表。
{ 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: { groupPolicy: "allowlist", accounts: { default: { token: "DISCORD_BOT_TOKEN_MAIN", guilds: { "123456789012345678": { channels: { "222222222222222222": { allow: true, requireMention: false }, }, }, }, }, coding: { token: "DISCORD_BOT_TOKEN_CODING", guilds: { "123456789012345678": { channels: { "333333333333333333": { allow: true, requireMention: false }, }, }, }, }, }, }, },}- 邀请每个机器人加入公会并启用消息内容意图。
- 令牌位于 `channels.discord.accounts.
.token中(默认账户可以使用DISCORD_BOT_TOKEN`)。
Telegram每个代理的 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...", dmPolicy: "pairing", }, alerts: { botToken: "987654:XYZ...", dmPolicy: "allowlist", allowFrom: ["tg:123456789"], }, }, }, },}- 使用 BotFather 为每个代理创建一个机器人并复制每个令牌。
- 令牌位于 `channels.telegram.accounts.
.botToken中(默认账户可以使用TELEGRAM_BOT_TOKEN`)。
WhatsApp每个代理的 WhatsApp 号码
在启动网关之前链接每个账户:
openclaw channels login --channel whatsapp --account personalopenclaw channels login --channel whatsapp --account biz~/.openclaw/openclaw.json (JSON5):
{ agents: { list: [ { id: "home", default: true, name: "Home", workspace: "~/.openclaw/workspace-home", agentDir: "~/.openclaw/agents/home/agent", }, { id: "work", name: "Work", workspace: "~/.openclaw/workspace-work", agentDir: "~/.openclaw/agents/work/agent", }, ], },
// Deterministic routing: first match wins (most-specific first). bindings: [ { agentId: "home", match: { channel: "whatsapp", accountId: "personal" } }, { agentId: "work", match: { channel: "whatsapp", accountId: "biz" } },
// Optional per-peer override (example: send a specific group to work agent). { agentId: "work", match: { channel: "whatsapp", accountId: "personal", }, }, ],
// Off by default: agent-to-agent messaging must be explicitly enabled + allowlisted. tools: { agentToAgent: { enabled: false, allow: ["home", "work"], }, },
channels: { whatsapp: { accounts: { personal: { // Optional override. Default: ~/.openclaw/credentials/whatsapp/personal // authDir: "~/.openclaw/credentials/whatsapp/personal", }, biz: { // Optional override. Default: ~/.openclaw/credentials/whatsapp/biz // authDir: "~/.openclaw/credentials/whatsapp/biz", }, }, }, },}按渠道拆分:将 WhatsApp 路由到一个快速的日常代理,将 Telegram 路由到一个 Opus 代理。
{ agents: { list: [ { id: "chat", name: "Everyday", workspace: "~/.openclaw/workspace-chat", model: "anthropic/claude-sonnet-4-6", }, { 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" } }, ],}注意事项:
- 如果您在一个渠道下有多个账户,请在绑定中添加
accountId(例如{ channel: "whatsapp", accountId: "personal" })。 - 要在将其他对话保留在 chat 上的同时,将单个私信/组路由到 Opus,请为该对等方添加
match.peer绑定;对等方匹配始终优先于全渠道规则。
将 WhatsApp 保留在快速代理上,但将一个私信路由到 Opus:
{ agents: { list: [ { id: "chat", name: "Everyday", workspace: "~/.openclaw/workspace-chat", model: "anthropic/claude-sonnet-4-6", }, { id: "opus", name: "Deep Work", workspace: "~/.openclaw/workspace-opus", model: "anthropic/claude-opus-4-6", }, ], }, bindings: [ { agentId: "opus", match: { channel: "whatsapp", peer: { kind: "direct", id: "+15551234567" } }, }, { agentId: "chat", match: { channel: "whatsapp" } }, ],}对等方绑定始终优先,因此请将它们置于全渠道规则之上。
将一个专用的家庭代理绑定到单个 WhatsApp 群组,并配置提及门控和更严格的工具策略:
{ agents: { list: [ { id: "family", name: "Family", workspace: "~/.openclaw/workspace-family", identity: { name: "Family Bot" }, groupChat: { mentionPatterns: ["@family", "@familybot", "@Family Bot"], }, sandbox: { mode: "all", scope: "agent", }, tools: { allow: [ "exec", "read", "sessions_list", "sessions_history", "sessions_send", "sessions_spawn", "session_status", ], deny: ["write", "edit", "apply_patch", "browser", "canvas", "nodes", "cron"], }, }, ], }, bindings: [ { agentId: "family", match: { channel: "whatsapp", }, }, ],}注意事项:
- 工具允许/拒绝列表是 tools(工具),而不是 skills(技能)。如果技能需要运行二进制文件,请确保允许
exec并且该二进制文件存在于沙箱中。 - 若要实施更严格的门控,请设置
agents.list[].groupChat.mentionPatterns并为该渠道启用群组允许列表。
按代理配置的沙箱和工具
Section titled “按代理配置的沙箱和工具”每个代理都可以拥有自己的沙箱和工具限制:
{ agents: { list: [ { id: "personal", workspace: "~/.openclaw/workspace-personal", sandbox: { mode: "off", // No sandbox for personal agent }, // No tool restrictions - all tools available }, { id: "family", workspace: "~/.openclaw/workspace-family", sandbox: { mode: "all", // Always sandboxed scope: "agent", // One container per agent docker: { // Optional one-time setup after container creation setupCommand: "apt-get update && apt-get install -y git curl", }, }, tools: { allow: ["read"], // Only read tool deny: ["exec", "write", "edit", "apply_patch"], // Deny others }, }, ], },}优势:
- 安全隔离:限制不受信任代理的工具。
- 资源控制:将特定代理置于沙箱中,同时将其他代理保留在宿主机上。
- 灵活策略:每个代理拥有不同的权限。
有关详细示例,请参阅 Multi-agent sandbox and tools。
- ACP agents — 运行外部编码工具
- Channel routing — 消息如何路由到代理
- Presence — 代理的状态和可用性
- Session — 会话隔离和路由
- Sub-agents — 生成后台代理运行