Agent实战笔记 | OpenClaw:一台机器运行多个隔离 Agent 的正确姿势
一句话概括:OpenClaw 是目前 GitHub 上最热门的开源个人 AI 助手项目(37 万+ Stars),其核心架构是一个本地 Gateway 守护进程,通过 bindings 路由机制将不同消息渠道、不同账号、不同联系人精准分配到完全隔离的 Agent 实例。每个 Agent 拥有独立的工作空间、会话历史、认证信息和人格配置。GitHub: https://github.com/openclaw/openclaw
为什么值得关注
1. 真正的”一机多脑”架构大多数 AI Agent 框架是”一个进程 = 一个 Agent”,要跑多个 Agent 就得启多个进程、管理多个端口、处理端口冲突。OpenClaw 的设计截然不同:一个 Gateway 进程内部就能托管多个完全隔离的 Agent,每个 Agent 有独立的 workspace、session store、auth profile。这不是简单的”多会话”,而是真正的”多实例”——相当于在一台 VPS 上同时运行了多个互不干扰的 AI 助手。
2. 基于 bindings 的智能路由你可以配置:WhatsApp 上的工作联系人自动路由到”工作 Agent”,Telegram 群聊路由到”家庭 Agent”,Discord 频道路由到”代码 Agent”。路由规则支持按渠道(channel)、账号(account)、对话类型(DM/group)、甚至具体发送者(peer ID)精确匹配。这解决了多 Agent 场景下最核心的问题——”谁该跟谁说话”。
3. 从单一助手到家庭/团队共享平台OpenClaw 支持 20+ 消息平台(WhatsApp、Telegram、Discord、Slack、Signal、iMessage、飞书、微信等),配合多 Agent 路由,可以让家人、团队成员共享同一台 Gateway 服务器,各自拥有完全私密的 AI 助手。每个人看到的内容、对话历史、工具权限完全隔离——这在家庭共享 VPS 或小型团队场景中极具实用价值。
核心能力拆解
架构概览
┌────────────────────────────────────────────────────────────┐│ OpenClaw Gateway (单一进程) ││ ││ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ││ │ Agent A │ │ Agent B │ │ Agent C │ │ Agent D │ ││ │ "个人" │ │ "工作" │ │ "家庭" │ │ "代码" │ ││ │ │ │ │ │ │ │ │ ││ │ workspace │ │ workspace │ │ workspace │ │ workspace │ ││ │ sessions │ │ sessions │ │ sessions │ │ sessions │ ││ │ auth- │ │ auth- │ │ auth- │ │ auth- │ ││ │ profiles │ │ profiles │ │ profiles │ │ profiles │ ││ │ SOUL.md │ │ SOUL.md │ │ SOUL.md │ │ SOUL.md │ ││ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ ││ │ │ │ │ ││ └─────────────┴──────┬──────┴─────────────┘ ││ │ ││ ┌────────▼────────┐ ││ │ Binding Router│ ││ │ (bindings[]) │ ││ └────────┬────────┘ ││ │ ││ ┌───────────┐ ┌──────────▼──────────┐ ┌───────────────┐ ││ │ WhatsApp │ │ Telegram │ │ Discord │ ││ │ (2 bots) │ │ (1 bot) │ │ (2 bots) │ ││ └───────────┘ └─────────────────────┘ └───────────────┘ │└────────────────────────────────────────────────────────────┘
关键概念拆解:
|
|
|
|
|---|---|---|
| Gateway |
|
|
| Agent |
|
~/.openclaw/agents/<id>/ |
| Workspace |
|
~/.openclaw/workspace-<id> |
| Binding |
|
openclaw.json
bindings[] |
| Channel |
|
channels.<name> |
| Session |
|
~/.openclaw/agents/<id>/sessions/ |
人格定制体系
每个 Agent 的 workspace 中可以放置以下文件来定义其行为:
-
SOUL.md — Agent 的核心人格和性格定义(”你是谁”) -
AGENTS.md — 行为规则和工作方式(”你怎么做事”) -
USER.md — 用户信息和使用偏好(”你在为谁服务”) -
其他自定义文件 — 笔记、模板、参考文档等
这意味着你可以让”工作 Agent”严格遵循商务沟通风格,而”个人 Agent”则随意轻松——它们共享同一个 Gateway,但拥有完全不同的性格和能力边界。
实战教程
一、安装配置(3 分钟上手)
环境要求: Node.js 24(推荐)或 22.16+
# 安装 OpenClawnpm install -g openclaw@latest# 交互式引导配置(推荐首次使用)openclaw onboard --install-daemon# 验证安装openclaw doctor
onboard 命令会引导你完成:模型提供商选择(支持 OpenAI、Anthropic、Google 等)、渠道配置、认证设置。完成后 Gateway 会自动作为系统服务运行(launchd/systemd)。
二、创建多 Agent 工作流
场景设定
假设你是一个独立开发者,希望:
-
有一个”个人助手”处理日常事务(WhatsApp) -
有一个”代码助手”帮你做 Code Review 和技术问题(Discord) -
有一个”家庭助手”回答家人的问题(Telegram 群聊)
步骤 1:创建 Agent
# 创建三个 Agentopenclaw agents add personalopenclaw agents add codingopenclaw agents add family# 验证openclaw agents list --bindings
每个 Agent 会自动获得独立的工作空间目录和状态目录:
-
~/.openclaw/workspace-personal -
~/.openclaw/workspace-coding -
~/.openclaw/workspace-family
步骤 2:配置人格
# 为每个 Agent 定制人格cat > ~/.openclaw/workspace-personal/SOUL.md << 'EOF'你是用户的个人助手,风格友好、简洁、务实。擅长日程管理、信息检索、日常问答。回答要简洁,不要过度解释。EOFcat > ~/.openclaw/workspace-coding/SOUL.md << 'EOF'你是一个资深软件工程师助手,擅长代码审查、架构设计、Debug。回答要严谨,给出具体代码示例。遵循 Clean Code 原则,指出潜在的安全隐患。EOFcat > ~/.openclaw/workspace-family/SOUL.md << 'EOF'你是家庭助手,用温暖、耐心的语气回答问题。避免使用技术术语,用通俗易懂的语言。适合回答生活常识、学习辅导、日常问题。EOF
步骤 3:配置路由(bindings)
编辑 ~/.openclaw/openclaw.json,添加多 Agent 配置:
{ // 全局默认设置 agents: { defaults: { workspace: "~/.openclaw/workspace", model: { primary: "anthropic/claude-sonnet-4-6", fallbacks: ["openai/gpt-5.4"], }, }, // 定义所有 Agent list: [ { id: "personal", name: "个人助手", workspace: "~/.openclaw/workspace-personal", // 继承默认模型 }, { id: "coding", name: "代码助手", workspace: "~/.openclaw/workspace-coding", // 代码助手用更强的模型 model: "anthropic/claude-opus-4-6", // 沙箱隔离(安全考虑) sandbox: { mode: "all", scope: "agent", }, }, { id: "family", name: "家庭助手", workspace: "~/.openclaw/workspace-family", // 限制工具权限 tools: { allow: ["read", "sessions_list", "sessions_history", "sessions_send"], deny: ["exec", "write", "edit", "browser", "cron"], }, }, ], }, // 渠道配置 channels: { whatsapp: { enabled: true, // DM 策略:需要配对码 dmPolicy: "pairing", allowFrom: ["+86138xxxxxxxx"], }, discord: { enabled: true, botToken: "YOUR_DISCORD_BOT_TOKEN", dmPolicy: "pairing", }, telegram: { enabled: true, botToken: "YOUR_TELEGRAM_BOT_TOKEN", dmPolicy: "pairing", }, }, // 核心:bindings 路由规则 bindings: [ // WhatsApp → 个人助手 { agentId: "personal", match: { channel: "whatsapp" } }, // Discord → 代码助手 { agentId: "coding", match: { channel: "discord" } }, // Telegram → 家庭助手 { agentId: "family", match: { channel: "telegram" } }, ],}
注意:binding 的匹配顺序很重要。越精确的规则(如指定 peer ID)应该放在越前面,通用规则(如整个 channel)放在后面。
步骤 4:重启并验证
# 重启 Gateway 使配置生效openclaw gateway restart# 检查 Agent 状态和路由openclaw agents list --bindings# 检查渠道连接openclaw channels status --probe
三、关键配置说明
按发送者精确路由
如果你有多个家庭成员共用 WhatsApp,可以按发送者路由:
{ bindings: [ // 特定号码 → 专属 Agent { agentId: "personal", match: { channel: "whatsapp", peer: { kind: "direct", id: "+86138xxxxxxxx" } }, }, { agentId: "family", match: { channel: "whatsapp", peer: { kind: "direct", id: "+86139xxxxxxxx" } }, }, // 其他 WhatsApp 消息 → 默认 Agent { agentId: "personal", match: { channel: "whatsapp" } }, ],}
按群聊路由
为 Discord 的特定频道绑定不同的 Agent:
{ bindings: [ // #code-review 频道 → 代码助手 { agentId: "coding", match: { channel: "discord", peer: { kind: "group", id: "CHANNEL_ID_123" } }, }, // #general 频道 → 个人助手 { agentId: "personal", match: { channel: "discord", peer: { kind: "group", id: "CHANNEL_ID_456" } }, }, ],}
按账号路由(同渠道多账号)
如果你在 WhatsApp 上连接了两个号码(一个工作、一个个人):
{ channels: { whatsapp: { accounts: { personal: { phone: "+86138xxxxxxxx" }, work: { phone: "+86137xxxxxxxx" }, }, }, }, bindings: [ { agentId: "personal", match: { channel: "whatsapp", accountId: "personal" } }, { agentId: "coding", match: { channel: "whatsapp", accountId: "work" } }, ],}
Skill 推荐
OpenClaw 拥有强大的 Skills 生态(官方 ClawHub 注册中心收录了 5,400+ 技能)。以下是多 Agent 协作场景下最实用的 5 个 Skill:
1. openclaw-skill-weather — 天气查询
|
|
|
|---|---|
| 用途 |
|
| 适用 Agent |
|
| 推荐理由 |
|
// 在 agent workspace 中安装后自动可用// 使用方式:直接问 "今天天气怎么样?"
2. openclaw-skill-calendar — 日历管理
|
|
|
|---|---|
| 用途 |
|
| 适用 Agent |
|
| 推荐理由 |
|
# 配置 Google Calendar API 凭证openclaw skill install calendar# 然后在 agent 对话中:"帮我看看明天下午有什么会议"
3. openclaw-skill-code-review — 代码审查
|
|
|
|---|---|
| 用途 |
|
| 适用 Agent |
|
| 推荐理由 |
|
openclaw skill install code-review# 使用:在 Discord #code-review 频道发送 "review PR #42"
4. openclaw-skill-home-assistant — 智能家居控制
|
|
|
|---|---|
| 用途 |
|
| 适用 Agent |
|
| 推荐理由 |
|
// 配置 Home Assistant URL 和 API Token{ skills: { homeAssistant: { url: "http://192.168.1.100:8123", token: "YOUR_HA_TOKEN", }, },}
5. openclaw-skill-web-research — 网络研究
|
|
|
|---|---|
| 用途 |
|
| 适用 Agent |
|
| 推荐理由 |
|
openclaw skill install web-research# 使用:"帮我查一下 OpenClaw 最新版本的更新日志"
Skill 管理提示:OpenClaw 的 Skill 安装使用
openclaw skill install <name>,所有 Skill 存储在~/.openclaw/skills/(全局共享)和每个 Agent 的 workspace 中(Agent 专属)。通过agents.list[].skills可以为每个 Agent 配置独立的 Skill 白名单。
我的工作流
场景:独立开发者的 24 小时自动化工作流
目标:用一台低配 VPS(2C4G)运行三个 Agent,覆盖个人、工作、家庭场景。
完整配置:
// ~/.openclaw/openclaw.json — 精简化配置{ agents: { defaults: { workspace: "~/.openclaw/workspace", model: { primary: "anthropic/claude-sonnet-4-6", fallbacks: ["openrouter/google/gemini-2.5-flash"], }, heartbeat: { every: "2h" }, }, list: [ { id: "personal", name: "小陆助手", workspace: "~/.openclaw/workspace-personal", skills: ["calendar", "weather", "web-research"], }, { id: "coding", name: "代码助手", workspace: "~/.openclaw/workspace-coding", model: "anthropic/claude-opus-4-6", skills: ["code-review", "github", "web-research"], sandbox: { mode: "all", scope: "agent" }, tools: { allow: ["exec", "read", "write", "edit", "sessions_list", "sessions_history", "sessions_send"], }, }, { id: "family", name: "家庭助手", workspace: "~/.openclaw/workspace-family", skills: ["weather", "home-assistant"], tools: { allow: ["read", "sessions_list", "sessions_send"], deny: ["exec", "write", "edit", "browser"], }, }, ], }, channels: { telegram: { enabled: true, botToken: process.env.TELEGRAM_BOT_TOKEN, dmPolicy: "pairing", }, discord: { enabled: true, botToken: process.env.DISCORD_BOT_TOKEN, dmPolicy: "pairing", }, }, bindings: [ { agentId: "personal", match: { channel: "telegram", peer: { kind: "direct", id: "tg:my_tg_id" } } }, { agentId: "coding", match: { channel: "discord" } }, { agentId: "family", match: { channel: "telegram" } }, ], // 定时任务:每天早上 8 点生成日报 cron: [ { name: "daily-report", schedule: "0 8 * * *", agentId: "personal", prompt: "生成今天的日报,包括天气、日历、最近的项目进展。发送到 Telegram。", }, ], session: { reset: { mode: "daily", atHour: 4, // 每天凌晨 4 点重置会话 }, },}
一天的实际运行流程:
|
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
资源占用实测参考:
-
单 Gateway 进程:约 200-400MB 内存(无 Agent 对话时) -
每个活跃 Agent 会话:额外 50-100MB -
沙箱容器(Docker):每个约 150-300MB -
2C4G VPS 运行 3 个 Agent + 2 个渠道连接,日常无压力
避坑指南
1. agentDir 重复导致认证冲突
问题:多个 Agent 共享同一个 agentDir 会导致 OAuth token 和会话数据互相覆盖。
解决方案:确保每个 Agent 的 agentDir(即 ~/.openclaw/agents/<agentId>/)唯一。使用 openclaw agents add <name> 命令创建会自动分配独立目录,不要手动复制。
2. Binding 匹配顺序陷阱
问题:通用 binding(如 { channel: "telegram" })放在精确 binding(如指定 peer ID)前面,会导致精确规则永远不生效。
解决方案:OpenClaw 的 binding 匹配是顺序优先的。将精确规则(peer-level)放在前面,通用规则(channel-level)放在最后。
// ❌ 错误顺序bindings: [ { agentId: "family", match: { channel: "telegram" } }, // 会匹配所有 Telegram 消息 { agentId: "personal", match: { channel: "telegram", peer: { id: "tg:123" } } }, // 永远不会触发]// ✅ 正确顺序bindings: [ { agentId: "personal", match: { channel: "telegram", peer: { id: "tg:123" } } }, { agentId: "family", match: { channel: "telegram" } },]
3. DM 配对策略导致消息被拒
问题:默认 dmPolicy: "pairing" 意味着所有陌生消息都会收到配对码而非正常回复。
解决方案:
-
个人使用:设置 dmPolicy: "open"+allowFrom: ["*"] -
家庭使用:设置 dmPolicy: "allowlist"+ 列出已知用户 ID -
公共使用:保持 pairing,然后通过openclaw pairing approve <channel> <code>手动审批
4. 配置文件严格验证
问题:OpenClaw 对配置文件的验证非常严格,任何未知的 key 或格式错误都会导致 Gateway 拒绝启动。
解决方案:
# 修改配置后立即验证openclaw doctor --fix# 如果 Gateway 启动失败,查看日志openclaw logs
注意:Gateway 启动失败后只会响应诊断命令(
doctor、logs、health、status),不会处理消息。
5. 沙箱模式下的工具权限
问题:Agent 在沙箱中运行时,默认会限制大部分工具(deny browser、canvas、nodes、cron)。如果 Skill 需要执行二进制文件,必须确保 exec 在 allowlist 中。
解决方案:检查 Agent 的 tools.allow / tools.deny 配置,确保所需工具在 allowlist 内。同时确认沙箱容器中安装了必要的二进制。
6. WhatsApp 多账号限制
问题:一个 WhatsApp 号码同时只能连接一个 Gateway 实例。如果你想在同一台机器上用两个 WhatsApp 号码连接不同 Agent,需要使用 channels.whatsapp.accounts 分别配置。
解决方案:在配置中使用 accounts 字段定义多个 WhatsApp 账号,每个账号绑定不同的 phone number:
{ channels: { whatsapp: { accounts: { personal: { phone: "+86138xxxxxxxx" }, work: { phone: "+86137xxxxxxxx" }, }, }, },}
然后通过 openclaw channels login --channel whatsapp --account personal 分别登录。
适合/不适合人群
✅ 适合
-
独立开发者/自由职业者:一台 VPS 搞定个人助手 + 代码助手,低成本 -
家庭用户:全家共享一个 Gateway,每人拥有独立 Agent 和隐私隔离 -
小型团队:按频道/项目分配不同 Agent,工具权限可精确控制 -
技术爱好者:喜欢折腾、想要完全掌控自己 AI 助手的数据和运行环境
❌ 不适合
-
企业级需求:需要 SSO、RBAC、审计日志等能力时,OpenClaw 的定位更偏个人/小团队 -
零配置用户:虽然 onboard向导已经很友好,但多 Agent 场景仍需手动编辑 JSON5 配置 -
纯云端 SaaS 偏好者:OpenClaw 是自托管方案,需要自己维护服务器和升级 -
只需要单会话聊天的用户:如果你只需要一个 AI 聊天窗口,ChatGPT/Claude Web 版更简单直接
关注「小陆的AI实战营」,每天一款值得关注的 AI 开源项目;每周 Agent 实战笔记,带你从实战角度掌握 AI 工具链。
夜雨聆风