返回

10. OpenClaw 多 Agent 路由與工作階段隔離

本文深入講解 OpenClaw 的多 Agent 架構,包括如何設定多個 Agent 實例、訊息路由規則、工作階段隔離策略、以及團隊協作場景的最佳實務,幫助你建構企業級的 AI 助手系統。

適用於 OpenClaw v2026.2 | 本文適合團隊使用或需要隔離上下文的使用者。

TL;DR: 多 Agent 設定:"agents": {"instances": {"personal": {...}, "work": {...}}}。路由規則:"routing": {"rules": [{"match": {"channel": "telegram"}, "agent": "personal"}]}。工作階段隔離:"sessions": {"isolation": "user"}。沙箱模式:"sandbox": {"mode": "non-main"}。適合多使用者、多專案、多用途場景。

為什麼需要多 Agent?

單 Agent 的局限

場景 單 Agent 問題 多 Agent 解決方案
多使用者 上下文混亂,隱私問題 每使用者獨立 Agent
多專案 專案資訊混在一起 每專案獨立 Agent
多用途 一個 Agent 多種角色 專門化的 Agent
多團隊 權限難以控制 按團隊隔離 Agent
成本控制 無法區分用量 每個模型獨立計費

多 Agent 使用場景

flowchart TB
    subgraph Users["使用者"]
        U1[使用者A]
        U2[使用者B]
        U3[團隊X]
        U4[團隊Y]
    end
    
    subgraph Router["訊息路由"]
        R[路由規則]
    end
    
    subgraph Agents["Agent 實例"]
        A1[個人Agent A]
        A2[個人Agent B]
        A3[團隊Agent X]
        A4[團隊Agent Y]
    end
    
    subgraph Models["模型"]
        M1[Claude Opus]
        M2[Claude Sonnet]
        M3[GPT-4o]
        M4[Haiku]
    end
    
    U1 --> R
    U2 --> R
    U3 --> R
    U4 --> R
    R --> A1
    R --> A2
    R --> A3
    R --> A4
    A1 --> M1
    A2 --> M2
    A3 --> M3
    A4 --> M4

多 Agent 設定

基本結構

{
  "agents": {
    "defaults": {
      "model": "anthropic/claude-sonnet-4",
      "workspace": "~/.openclaw/workspace"
    },
    "instances": {
      "personal": {
        "model": "anthropic/claude-opus-4-6",
        "workspace": "~/.openclaw/workspaces/personal",
        "thinkingLevel": "high"
      },
      "work": {
        "model": "anthropic/claude-sonnet-4",
        "workspace": "~/.openclaw/workspaces/work",
        "skills": ["github", "jira", "confluence"]
      },
      "assistant": {
        "model": "anthropic/claude-haiku-3.5",
        "workspace": "~/.openclaw/workspaces/assistant"
      }
    }
  }
}

Agent 設定項

設定項 說明 範例
model 使用的模型 anthropic/claude-opus-4-6
workspace 工作目錄 ~/.openclaw/workspaces/personal
thinkingLevel 思考級別 high, medium, low
skills 啟用的技能 ["gmail", "calendar"]
sandbox 沙箱設定 { "mode": "non-main" }
contextWindow 上下文視窗 200000

Workspace 結構

~/.openclaw/
├── workspace/              # 預設 workspace
│   ├── AGENTS.md
│   ├── SOUL.md
│   └── skills/
├── workspaces/
│   ├── personal/          # 個人 Agent
│   │   ├── AGENTS.md
│   │   ├── SOUL.md
│   │   └── USER.md
│   ├── work/              # 工作 Agent
│   │   ├── AGENTS.md
│   │   └── skills/
│   └── assistant/         # 助手 Agent
│       └── AGENTS.md
└── openclaw.json          # 主設定

訊息路由規則

路由設定

{
  "routing": {
    "rules": [
      {
        "match": {
          "channel": "telegram",
          "sender": "123456789"
        },
        "agent": "personal"
      },
      {
        "match": {
          "channel": "discord",
          "guild": "987654321"
        },
        "agent": "work"
      },
      {
        "match": {
          "session": "main"
        },
        "agent": "personal"
      }
    ],
    "default": "personal"
  }
}

路由規則詳解

匹配欄位 說明 範例
channel 頻道類型 telegram, discord, slack
sender 發送者 ID 123456789
guild Discord 伺服器 ID 987654321
group 群組 ID -100123456789
session 工作階段 ID main, telegram:user:123
pattern 正規表示式匹配 ^work-

複雜路由範例

{
  "routing": {
    "rules": [
      {
        "name": "個人 Telegram",
        "match": {
          "channel": "telegram",
          "sender": ["123456789", "987654321"]
        },
        "agent": "personal"
      },
      {
        "name": "工作 Discord",
        "match": {
          "channel": "discord",
          "guild": "111222333"
        },
        "agent": "work"
      },
      {
        "name": "工作群組",
        "match": {
          "channel": "telegram",
          "group": "-100111222333"
        },
        "agent": "work"
      },
      {
        "name": "測試環境",
        "match": {
          "pattern": "^test-"
        },
        "agent": "assistant",
        "config": {
          "sandbox": { "mode": "always" }
        }
      }
    ],
    "default": "personal"
  }
}

路由優先順序

flowchart TB
    MSG[訊息到達] --> R1{匹配規則 1?}
    R1 -->|是| A1[Agent 1]
    R1 -->|否| R2{匹配規則 2?}
    R2 -->|是| A2[Agent 2]
    R2 -->|否| R3{匹配規則 3?}
    R3 -->|是| A3[Agent 3]
    R3 -->|否| D[預設 Agent]

路由按順序匹配,第一個匹配的規則生效。

工作階段隔離

隔離級別

級別 說明 使用場景
無隔離 共用上下文 單使用者、單用途
頻道隔離 按頻道獨立上下文 多頻道接入
使用者隔離 按使用者獨立上下文 多使用者共用
工作階段隔離 每個工作階段獨立 完全隔離
Agent 隔離 每個 Agent 獨立 多 Agent 部署

設定工作階段隔離

{
  "sessions": {
    "isolation": "user",
    "persistence": true,
    "retention": {
      "maxAge": 604800000,
      "maxSessions": 100
    }
  }
}

隔離設定詳解

{
  "sessions": {
    "isolation": "user",
    "storage": {
      "type": "file",
      "path": "~/.openclaw/sessions"
    },
    "encryption": {
      "enabled": true,
      "algorithm": "aes-256-gcm"
    },
    "retention": {
      "maxAge": 604800000,
      "maxSessions": 100,
      "cleanupInterval": 86400000
    }
  }
}
設定項 說明 預設值
isolation 隔離級別 user
storage.type 儲存類型 file
encryption.enabled 啟用加密 true
retention.maxAge 最大保留時間 7 天
retention.maxSessions 最大工作階段數 100

團隊協作場景

場景一:小型團隊共用

{
  "agents": {
    "instances": {
      "team": {
        "model": "anthropic/claude-sonnet-4",
        "workspace": "~/.openclaw/workspaces/team",
        "skills": ["github", "jira", "slack"]
      }
    }
  },
  "routing": {
    "rules": [
      {
        "match": { "channel": "slack" },
        "agent": "team"
      },
      {
        "match": { "channel": "discord" },
        "agent": "team"
      }
    ],
    "default": "team"
  }
}

場景二:部門隔離

{
  "agents": {
    "instances": {
      "engineering": {
        "model": "anthropic/claude-opus-4-6",
        "workspace": "~/.openclaw/workspaces/engineering",
        "skills": ["github", "jira", "pagerduty"],
        "sandbox": { "mode": "off" }
      },
      "marketing": {
        "model": "anthropic/claude-sonnet-4",
        "workspace": "~/.openclaw/workspaces/marketing",
        "skills": ["mailchimp", "analytics"],
        "sandbox": { "mode": "non-main" }
      },
      "support": {
        "model": "anthropic/claude-haiku-3.5",
        "workspace": "~/.openclaw/workspaces/support",
        "skills": ["zendesk", "intercom"],
        "sandbox": { "mode": "always" }
      }
    }
  },
  "routing": {
    "rules": [
      {
        "name": "Engineering Discord",
        "match": { "channel": "discord", "guild": "111111111" },
        "agent": "engineering"
      },
      {
        "name": "Marketing Slack",
        "match": { "channel": "slack", "team": "T22222222" },
        "agent": "marketing"
      },
      {
        "name": "Support Telegram",
        "match": { "channel": "telegram", "group": "-100333333333" },
        "agent": "support"
      }
    ],
    "default": "support"
  }
}

場景三:多租戶 SaaS

{
  "agents": {
    "dynamic": true,
    "template": {
      "model": "anthropic/claude-sonnet-4",
      "workspace": "~/.openclaw/workspaces/tenants/{{tenantId}}",
      "skills": ["base"],
      "sandbox": { "mode": "always" }
    },
    "limits": {
      "maxAgents": 100,
      "maxSessionsPerAgent": 50,
      "maxTokensPerMonth": 1000000
    }
  },
  "routing": {
    "rules": [
      {
        "name": "Tenant Routing",
        "match": { "header": "X-Tenant-Id" },
        "agent": "tenant_{{tenantId}}",
        "createIfNotExists": true
      }
    ]
  }
}

權限與安全

Agent 權限控制

{
  "agents": {
    "instances": {
      "trusted": {
        "model": "anthropic/claude-opus-4-6",
        "sandbox": { "mode": "off" },
        "permissions": {
          "filesystem": { "read": true, "write": true },
          "network": { "outbound": true },
          "process": { "execute": true }
        }
      },
      "restricted": {
        "model": "anthropic/claude-haiku-3.5",
        "sandbox": { "mode": "always" },
        "permissions": {
          "filesystem": { 
            "read": ["~/allowed/"],
            "write": ["~/allowed/"]
          },
          "network": { 
            "outbound": ["api.example.com"]
          },
          "process": { "execute": false }
        }
      }
    }
  }
}

沙箱模式

模式 說明 適用場景
off 無沙箱 可信 Agent
non-main 非主工作階段沙箱 團隊共用
always 始終沙箱 不信任環境

沙箱設定

{
  "sandbox": {
    "mode": "non-main",
    "container": {
      "image": "openclaw/sandbox:latest",
      "timeout": 30000,
      "memory": "512m",
      "cpu": "0.5"
    },
    "allowlist": ["read", "write", "edit", "bash"],
    "denylist": ["browser", "canvas", "nodes"],
    "networkPolicy": {
      "allow": ["api.example.com"],
      "deny": ["*"]
    }
  }
}

監控與管理

Agent 狀態監控

# 列出所有 Agent 實例
openclaw agents list

# 輸出範例:
# Name        Model                  Sessions  Status   Last Active
# personal    claude-opus-4-6        3         active   2 min ago
# work        claude-sonnet-4        5         active   5 min ago
# assistant   claude-haiku-3.5       2         idle     1 hour ago

# 查看 Agent 詳情
openclaw agents show personal

# 查看路由規則
openclaw routing list

工作階段管理

# 列出所有工作階段
openclaw sessions list --agent personal

# 輸出範例:
# Session ID              Channel    Sender      Created      Messages
# telegram:user:123       telegram   123456789   2026-02-26   42
# main                    cli        -           2026-02-25   128

# 查看工作階段詳情
openclaw sessions show telegram:user:123

# 匯出工作階段
openclaw sessions export telegram:user:123 --format json

# 刪除工作階段
openclaw sessions delete telegram:user:123

使用量統計

# 查看按 Agent 分組的使用量
openclaw usage --by-agent

# 輸出範例:
# Agent       Tokens In    Tokens Out   Cost      Requests
# personal    125,000     45,000      $2.10     128
# work        89,000      32,000      $1.25     95
# assistant   15,000      8,000       $0.15     42

# 按時間範圍統計
openclaw usage --by-agent --from 2026-02-01 --to 2026-02-28

最佳實務

1. 合理劃分 Agent

劃分維度 說明 範例
用途 按功能用途劃分 coding, research, assistant
專案 按專案劃分 project-alpha, project-beta
團隊 按團隊劃分 engineering, marketing
模型 按模型劃分 opus, sonnet, haiku

2. 設定繼承

{
  "agents": {
    "defaults": {
      "model": "anthropic/claude-sonnet-4",
      "workspace": "~/.openclaw/workspace",
      "thinkingLevel": "medium",
      "sandbox": { "mode": "non-main" }
    },
    "instances": {
      "opus": {
        "model": "anthropic/claude-opus-4-6",
        "thinkingLevel": "high"
        // 繼承 defaults 的 workspace 和 sandbox
      },
      "haiku": {
        "model": "anthropic/claude-haiku-3.5",
        "thinkingLevel": "minimal"
        // 繼承 defaults 的其他設定
      }
    }
  }
}

3. 權限最小化

{
  "agents": {
    "instances": {
      "restricted": {
        "model": "anthropic/claude-haiku-3.5",
        "sandbox": { "mode": "always" },
        "permissions": {
          "filesystem": {
            "read": ["~/allowed/read"],
            "write": ["~/allowed/write"]
          },
          "network": {
            "outbound": []
          },
          "process": {
            "execute": false
          }
        }
      }
    }
  }
}

4. 監控和告警

{
  "monitoring": {
    "alerts": [
      {
        "condition": "usage > 1000000",
        "action": "notify"
      },
      {
        "condition": "error_rate > 0.1",
        "action": "notify"
      },
      {
        "condition": "response_time > 10000",
        "action": "notify"
      }
    ],
    "webhook": "https://your-monitoring.com/webhook"
  }
}

故障排查

路由不生效

# 查看目前路由規則
openclaw routing list

# 測試路由
openclaw routing test --channel telegram --sender 123456789

# 輸出範例:
# Matched Rule: 個人 Telegram
# Target Agent: personal
# Route Path: default -> rule 1 -> personal

工作階段隔離問題

# 檢查工作階段隔離設定
openclaw config get sessions.isolation

# 檢查工作階段列表
openclaw sessions list --all

# 重設工作階段隔離
openclaw sessions reset --agent personal

權限問題

# 檢查權限設定
openclaw agents permissions show personal

# 測試權限
openclaw agents test-permission personal --action filesystem.read --path ~/test

# 輸出範例:
# Permission: filesystem.read
# Path: ~/test
# Result: ALLOWED

小結

多 Agent 架構是 OpenClaw 的進階功能,為團隊和企業場景提供了強大的支援:

  • 靈活路由:按頻道、使用者、專案分配不同 Agent
  • 工作階段隔離:保護隱私,避免上下文混亂
  • 權限控制:精細化的權限管理
  • 成本最佳化:按用途選擇不同模型
  • 可擴展性:支援動態建立和管理 Agent

本篇小結

  • 理解了多 Agent 架構的價值和場景
  • 掌握了 Agent 設定和訊息路由規則
  • 學會了工作階段隔離的設定方法
  • 了解了團隊協作場景的最佳實務
  • 掌握了權限控制和監控管理

更新記錄

  • 2026-02-26:初版發布,基於 OpenClaw v2026.2

系列導航