乐于分享
好东西不私藏

OpenClaw 科普系列(20):高级配置——进阶功能与调优技巧

OpenClaw 科普系列(20):高级配置——进阶功能与调优技巧

前几期我们聊了备份与恢复,确保你的 Agent 数据安全。现在让我们来探索 OpenClaw 的进阶功能——那些能让你的 Agent 更强大、更高效的配置选项。

本期我们来深入了解 OpenClaw 的高级配置:多 Agent 管理、性能调优、自定义工具以及其他进阶技巧。

多 Agent 管理

OpenClaw 支持运行多个 Agent,每个 Agent 可以有独立的配置、工具策略和工作区。

配置多个 Agent

{
  agents: {
    list: [
      {
        id: "personal",
        name: "个人助手",
        workspace: "~/.openclaw/workspace-personal",
        model: "anthropic/claude-sonnet-4",
        sandbox: { mode: "off" },
        tools: {
          profile: "maximal",
        },
      },
      {
        id: "work",
        name: "工作助手",
        workspace: "~/.openclaw/workspace-work",
        model: "openai/gpt-5.2",
        sandbox: {
          mode: "all",
          workspaceAccess: "ro",
        },
        tools: {
          profile: "balanced",
          deny: ["exec", "browser"],
        },
      },
      {
        id: "public",
        name: "公开助手",
        workspace: "~/.openclaw/workspace-public",
        model: "google/gemini-3-pro",
        sandbox: {
          mode: "all",
          workspaceAccess: "none",
        },
        tools: {
          profile: "messaging",
          deny: ["read", "write", "exec", "browser"],
        },
      },
    ],
  },
}

Agent 选择

默认 Agent

{
  agents: {
    default: "personal",  // 默认使用的 Agent
  },
}

运行时切换

/agent work

按渠道路由

{
  channels: {
    whatsapp: {
      groups: {
        "family-group-id": { agent: "personal" },
        "work-group-id": { agent: "work" },
      },
    },
  },
}

性能调优

模型配置优化

模型回退策略

{
  agents: {
    defaults: {
      model: {
        primary: "anthropic/claude-sonnet-4",
        fallbacks: [
          "openai/gpt-5.2",
          "google/gemini-3-pro",
        ],
      },
    },
  },
}

图像模型配置

{
  agents: {
    defaults: {
      imageModel: "google/gemini-3-pro-vision",
      imageGenerationModel: "fal/fal-ai/flux/dev",
    },
  },
}

会话维护调优

{
  session: {
    maintenance: {
      mode: "enforce",           // warn | enforce
      pruneAfter: "30d",         // 清理 30 天前的会话
      maxEntries: 500,           // 最多保留 500 个会话
      rotateBytes: "10mb",       // sessions.json 超过 10MB 时轮转
      maxDiskBytes: "1gb",       // 磁盘预算上限
      highWaterBytes: "800mb",   // 达到 80% 时开始清理
    },
  },
}

心跳频率调整

{
  agents: {
    defaults: {
      heartbeat: {
        every: "30m",           // 默认 30 分钟
        target: "last",         // 发送到最后活跃的渠道
        activeHours: {
          start: "08:00",
          end: "22:00",
        },
      },
    },
  },
}

高级工具配置

自定义安全二进制文件

{
  tools: {
    exec: {
      safeBins: ["git", "docker", "npm", "python3"],
      safeBinProfiles: {
        git: { allowList: ["status", "log", "diff", "clone", "pull"] },
        docker: { requireApproval: true },
      },
    },
  },
}

浏览器高级配置

{
  browser: {
    enabled: true,
    profile: "openclaw",           // 浏览器配置名称
    headless: false,               // 是否无头模式
    ssrfPolicy: {
      dangerouslyAllowPrivateNetwork: false,  // 禁止访问内网
      hostnameAllowlist: ["*.example.com"],
    },
  },
}

文件系统限制

{
  tools: {
    fs: {
      workspaceOnly: true,         // 只允许访问工作区
      allowOutsideWorkspace: [     // 例外路径
        "/tmp",
        "/var/log",
      ],
    },
  },
}

频道高级配置

WhatsApp 高级设置

{
  channels: {
    whatsapp: {
      dmPolicy: "pairing",
      groups: {
        "*": {
          requireMention: true,
          requireReply: false,
          allowedMentions: ["@openclaw", "@bot"],
        },
      },
      autoReconnect: true,
      reconnectInterval: 5000,
    },
  },
}

Telegram 多账户

{
  channels: {
    telegram: {
      accounts: [
        {
          id: "personal",
          token: "${TELEGRAM_PERSONAL_BOT_TOKEN}",
          dmPolicy: "pairing",
        },
        {
          id: "work",
          token: "${TELEGRAM_WORK_BOT_TOKEN}",
          dmPolicy: "allowlist",
          allowFrom: ["colleague1", "colleague2"],
        },
      ],
    },
  },
}

Secret 管理

环境变量引用

{
  gateway: {
    auth: {
      token: "${OPENCLAW_GATEWAY_TOKEN}",
    },
  },
  channels: {
    telegram: {
      token: "${TELEGRAM_BOT_TOKEN}",
    },
  },
}

SecretRef 配置

{
  secrets: {
    providers: [
      {
        id: "default",
        kind: "file",
        path: "~/.openclaw/secrets.json",
      },
    ],
  },
  gateway: {
    auth: {
      token: {
        source: "secret",
        provider: "default",
        id: "gateway.token",
      },
    },
  },
}

插件系统

安装插件

openclaw plugins install @openclaw/plugin-name

配置插件

{
  plugins: {
    allow: ["@openclaw/lobster", "@openclaw/llm-task"],
    entries: {
      "@openclaw/lobster": {
        enabled: true,
        config: {
          timeout: 30000,
        },
      },
    },
  },
}

调试配置

详细日志

{
  logging: {
    level: "debug",
    file: "/var/log/openclaw/gateway.log",
    redactSensitive: "tools",
    redactPatterns: [
      "api[_-]?key",
      "token[=:]\\s*\\w+",
    ],
  },
}

诊断模式

# 启动时启用详细模式
openclaw gateway --verbose --ws-log full

# 运行时诊断

openclaw doctor
openclaw doctor --fix

实用技巧

快速切换配置

使用环境变量覆盖配置:

OPENCLAW_GATEWAY_PORT=18888 openclaw gateway
OPENCLAW_GATEWAY_TOKEN=new-token openclaw gateway

配置验证

# 验证配置语法
openclaw config validate

# 检查配置问题

openclaw doctor

# 安全审计

openclaw security audit

会话管理

# 列出所有会话
openclaw sessions --json

# 清理旧会话

openclaw sessions cleanup --dry-run
openclaw sessions cleanup --enforce

# 查看特定会话历史

openclaw sessions history --sessionKey agent:main:main

批量操作

# 批量更新配置
openclaw config set agents.defaults.model.primary "anthropic/claude-opus-4"

# 批量添加允许列表

openclaw approvals allowlist add --all-agents "/usr/bin/git"

总结

OpenClaw 的高级配置让你能够:

  • 多 Agent 管理:为不同场景配置专门的 Agent
  • 性能调优:优化模型选择、会话维护、心跳频率
  • 精细控制:自定义安全二进制文件、浏览器策略、文件系统限制
  • 安全增强:Secret 管理、多账户配置、审计日志
  • 灵活扩展:插件系统、环境变量覆盖、诊断工具

掌握这些高级配置,你可以:

  • • 为个人、工作、公开场景分别配置合适的 Agent
  • • 优化性能和资源使用
  • • 加强安全性和隔离性
  • • 快速诊断和解决问题

OpenClaw 的设计哲学是"默认简单,进阶强大"——基础配置开箱即用,高级功能按需启用。

下一期,我们将进入第五部分,开始 实战开发——教你如何编写一个 Skill。