OpenClaw 是一个开源的 AI Agent 编排平台,提供 Agent 运行时、多渠道接入、技能系统和 MCP 集成等能力。与 LangChain/LlamaIndex 等偏重"LLM 调用抽象"的框架不同,OpenClaw 定位更偏向于Agent 的生产运行时——关注会话管理、多渠道消息路由、持久化记忆和技能的动态加载。
本文基于 OpenClaw 的公开文档和实际使用经验,提供从架构理解到高级用法的完整指南。
⚠️ 说明: OpenClaw 仍在快速迭代中,部分功能可能随版本变化。建议以官方文档为准。
最后更新: 2026-03-15 | 分类: AI Agent 平台
OpenClaw 的核心设计围绕三个抽象:
| 组件 | 功能 | 说明 |
|---|---|---|
| Gateway | 核心服务 | 管理 Agent 生命周期、路由消息 |
| Agent Runtime | Agent 执行环境 | LLM 调用、工具执行、记忆管理 |
| Channel Router | 渠道路由 | 将消息路由到对应的 Agent 和会话 |
| Skill Loader | 技能加载 | 动态加载和管理 Agent 技能 |
| Session Manager | 会话管理 | 持久化会话上下文、记忆 |
| MCP Client | MCP 协议客户端 | 连接外部 MCP Server |
| 要求 | 说明 |
|---|---|
| Node.js | v20.0+(推荐 v22 LTS) |
| 操作系统 | Linux(推荐)、macOS、Windows(WSL2) |
| 内存 | 最低 512MB,推荐 2GB+ |
| 磁盘 | 200MB+(含依赖和日志) |
| 网络 | 需要访问 LLM API(如 OpenRouter、Anthropic、OpenAI) |
# 1. 安装 OpenClaw CLI(全局安装)
npm install -g openclaw
# 2. 验证安装
openclaw --version
# 3. 初始化配置向导
openclaw init
# 4. 启动 Gateway 服务
openclaw gateway start
openclaw init 向导会引导你完成:
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
# macOS (使用 Homebrew)
brew install node@22
# 验证
node --version # 应输出 v22.x.x
npm --version # 应输出 10.x.x
# 全局安装
npm install -g openclaw
# 验证安装
openclaw --version
openclaw help
# 创建配置目录和工作区
openclaw init
# 这会创建以下目录结构:
# ~/.openclaw/
# ├── config/ # 全局配置
# ├── workspace/ # 默认工作区
# ├── logs/ # 运行日志
# └── skills/ # 技能安装目录
# 方法 1:交互式配置(推荐)
openclaw configure
# 方法 2:直接设置环境变量
export OPENROUTER_API_KEY="sk-or-..."
# 或
export ANTHROPIC_API_KEY="sk-ant-..."
# 或
export OPENAI_API_KEY="sk-..."
推荐使用 OpenRouter:一个 API Key 访问所有主流模型(Claude、GPT、Gemini、Llama 等),自动路由和降级。
# 创建 Agent 配置
mkdir -p ~/.openclaw/agents/my-agent
cat > ~/.openclaw/agents/my-agent/agent.yaml << 'EOF'
agent:
name: my-assistant
model: openrouter/anthropic/claude-sonnet-4
system_prompt: |
你是一个专业、友善的 AI 助手。
tools:
- web_search
- read
- write
- exec
workspace: /root/.openclaw/workspace
EOF
# 1. 从 @BotFather 获取 Telegram Bot Token
# 2. 添加 Telegram 插件
openclaw plugin add telegram
# 3. 配置 Token
openclaw configure --section telegram
# 按提示输入 Bot Token
# 4. 设置允许的聊天 ID
# 在 agent.yaml 中添加:
# channels:
# - telegram:
# allowed_chats: [YOUR_CHAT_ID]
# 前台运行(开发/调试)
openclaw gateway start --dev
# 后台运行(生产)
openclaw gateway start
# 使用 systemd(Linux 生产环境)
sudo tee /etc/systemd/system/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/.openclaw
ExecStart=/usr/bin/openclaw gateway start
Restart=always
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable openclaw
sudo systemctl start openclaw
FROM node:22-slim
# 安装 OpenClaw
RUN npm install -g openclaw
# 创建配置目录
RUN mkdir -p /root/.openclaw/config /root/.openclaw/workspace
# 复制配置
COPY config/ /root/.openclaw/config/
# 暴露 HTTP 端口
EXPOSE 3000
# 启动
CMD ["openclaw", "gateway", "start"]
# 构建并运行
docker build -t my-openclaw .
docker run -d \
-p 3000:3000 \
-e OPENROUTER_API_KEY=sk-or-... \
-v openclaw-data:/root/.openclaw \
--name openclaw \
my-openclaw
在开始使用前,理解三个核心抽象:
| 概念 | 类比 | 说明 |
|---|---|---|
| Agent | 一个人 | 拥有独立的"性格"(system prompt)、"工具箱"(tools)和"记忆"(memory) |
| Channel | 手机号码 | Agent 与外界通信的渠道(Telegram 号码、Discord 频道等) |
| Session | 一段对话 | Agent 与某个用户/群组的一次交互上下文 |
# 1. 确保 Gateway 运行中
openclaw gateway status
# 2. 通过 HTTP API 测试
curl -X POST http://localhost:3000/api/chat \
-H "Content-Type: application/json" \
-d '{
"message": "你好,介绍一下你自己",
"session": "test-session"
}'
# 3. 通过 Telegram 测试
# 在 Telegram 中给你的 Bot 发送消息
工作区是 Agent 的"工作桌面",存放文件、笔记、报告等:
# 查看工作区
ls ~/.openclaw/workspace/
# Agent 可以通过工具读写工作区文件
# - read: 读取文件
# - write: 创建/覆盖文件
# - edit: 精确编辑文件
# - exec: 执行命令
# 常见工作区结构:
# workspace/
# ├── SOUL.md # Agent "性格"定义
# ├── AGENTS.md # Agent 行为指南
# ├── USER.md # 用户偏好
# ├── MEMORY.md # 长期记忆
# ├── memory/ # 日记
# └── reports/ # 产出物
技能是可复用的能力模块,可以从 ClawHub 社区安装:
# 搜索技能
clawhub search weather
# 安装技能
clawhub install weather
# 查看已安装技能
clawhub list
# Agent 会自动识别和使用已安装的技能
# 技能定义在 SKILL.md 中,包含触发条件和使用方法
MCP(Model Context Protocol)允许 Agent 连接外部工具和服务:
# 安装 MCP 插件
openclaw plugin add mcporter
# 添加 MCP Server(以文件系统为例)
openclaw mcp add filesystem \
--command npx \
--args "-y,@modelcontextprotocol/server-filesystem,/data"
# 查看已连接的 MCP 工具
openclaw mcp list
# Agent 现在可以读写 /data 目录了
主 Agent 可以 spawn 子 Agent 执行特定任务,结果自动返回:
用户: "帮我研究一下 Rust 的内存安全机制,写一份报告"
↓
主 Agent: spawn subagent → "研究 Rust 内存安全,写报告"
↓
子 Agent: 独立执行研究任务
↓
子 Agent 完成 → 结果自动返回给主 Agent
↓
主 Agent: 将报告发送给用户
OpenClaw 的 Agent 通过配置文件定义,核心配置项:
# Agent 配置示例
agent:
name: my-agent
model: openrouter/anthropic/claude-sonnet-4
system_prompt: |
你是一个专业的技术顾问...
# 记忆配置
memory:
backend: file # 或 redis
max_tokens: 4000
summarization: true # 自动摘要
# 工具配置
tools:
- web_search
- read
- write
- exec
# 技能目录
skills:
- /path/to/skills/
OpenClaw 的会话有以下关键概念:
agent:{name}:{channel}:{chat_id}会话隔离策略:
OpenClaw 支持在同一 Gateway 下运行多个 Agent:
# 多 Agent 配置
agents:
- name: chief-editor
model: openrouter/anthropic/claude-sonnet-4
channels:
- telegram:
token: ${TELEGRAM_TOKEN}
allowed_chats: [5967921069]
- name: probe-researcher
model: openrouter/anthropic/claude-sonnet-4
channels:
- http:
port: 8081
Subagent 机制: 主 Agent 可以 spawn 子 Agent 执行特定任务,结果自动返回。
| 渠道 | 类型 | 特殊功能 |
|---|---|---|
| Telegram | 即时通讯 | Inline buttons、Reaction、Topic |
| Discord | 即时通讯 | Slash commands、Embed、Thread |
| Signal | 即时通讯 | 端到端加密、Group |
| 即时通讯 | Business API | |
| HTTP API | REST | 自定义集成 |
| WebSocket | 实时 | 自定义前端 |
# 安装 Telegram 插件
openclaw plugin add telegram
# 配置 Token
openclaw configure --section telegram
# 设置允许的聊天
# 在 Agent 配置中指定 allowed_chats
OpenClaw 支持灵活的路由配置:
OpenClaw 的技能是可复用的能力模块,每个技能是一个目录:
SKILL.md 示例:
# Weather Skill
## 触发条件
当用户询问天气、温度、预报时激活。
## 使用方法
使用 wttr.in API 获取天气信息:
- 当前天气: curl "wttr.in/{city}?format=j1"
- 格式化输出: curl "wttr.in/{city}?lang=zh"
## 注意事项
- 不需要 API Key
- 支持中文城市名
技能加载机制: Agent 在处理消息时,扫描可用技能的 SKILL.md,根据描述决定是否加载对应技能。
OpenClaw 支持 MCP (Model Context Protocol) 作为工具扩展:
# MCP Server 配置
mcp:
servers:
- name: filesystem
command: npx
args: ["-y", "@modelcontextprotocol/server-filesystem", "/data"]
- name: postgres
command: npx
args: ["-y", "@modelcontextprotocol/server-postgres"]
env:
DATABASE_URL: ${DATABASE_URL}
MCP vs 内置工具:
memory:
backend: redis # 推荐生产环境
max_history: 50 # 保留最近 50 条消息
summarization: true # 超出限制时自动摘要
summary_interval: 20 # 每 20 条消息摘要一次
cross_session: false # 是否跨会话共享记忆
最佳实践:
file backend,简单可靠redis backend,支持分布式模型选择: 不同任务用不同模型
# 主 Agent 用强模型,子 Agent 用轻模型
agent:
model: openrouter/anthropic/claude-sonnet-4
subagents:
model: openrouter/google/gemini-2.0-flash
并发控制:
runtime:
max_concurrent: 10 # 最大并发会话数
timeout: 120 # 单次调用超时(秒)
retry: 3 # 重试次数
缓存策略:
cache:
enabled: true
ttl: 3600 # 缓存 1 小时
backend: redis
allowed_chats 限制可访问的用户/群组tools:
exec:
allowed_commands: ["git", "npm", "python"]
denied_commands: ["rm -rf", "sudo"]
# 查看 Gateway 状态
openclaw gateway status
# 查看日志
openclaw logs --follow
# 检查 Agent 会话
openclaw sessions list
# 查看特定会话上下文
openclaw sessions view <session-id>
# 导出会话历史
openclaw sessions export <session-id> --format markdown
单机部署:
# 开发环境
openclaw gateway start --dev
# 生产环境 (systemd)
sudo systemctl enable openclaw
sudo systemctl start openclaw
容器部署:
FROM node:22-slim
RUN npm install -g openclaw
COPY config/ /root/.openclaw/config/
EXPOSE 3000
CMD ["openclaw", "gateway", "start"]
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
EADDRINUSE 错误 |
端口被占用 | openclaw gateway stop 先停止旧进程,或用 lsof -i :3000 找占用进程 |
EACCES 权限错误 |
需要 root 权限 | 使用 sudo 或配置 systemd 以非 root 用户运行 |
MODULE_NOT_FOUND |
依赖未安装 | 重新安装:npm install -g openclaw --force |
| Gateway 启动后无响应 | API Key 未配置 | 检查 openclaw configure 或环境变量是否设置正确 |
# 诊断 Gateway 问题
openclaw gateway status # 查看状态
openclaw logs --follow # 实时查看日志
openclaw gateway restart # 尝试重启
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| Telegram Bot 无响应 | Chat ID 未授权 | 检查 allowed_chats 配置,确认包含你的 Chat ID |
| HTTP API 返回 500 | LLM API 错误 | 检查 API Key 是否有效,余额是否充足 |
| 回复超时 | 模型响应慢 | 增加 timeout 配置(默认 120s),或换更快的模型 |
| 回复为空 | System prompt 问题 | 检查 Agent 配置中 system_prompt 是否为空 |
# 查看最近日志中的错误
openclaw logs --level error --limit 50
# 测试 Agent 配置
openclaw agent test my-agent --message "你好"
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 安装后技能不生效 | Gateway 未重启 | 运行 openclaw gateway restart |
| 技能搜索无结果 | 网络问题 | 检查能否访问 clawhub.com |
| SKILL.md 语法错误 | 格式不正确 | 检查 SKILL.md 是否包含必需的 # 触发条件 和 # 使用方法 章节 |
# 检查 MCP Server 状态
openclaw mcp list
# 测试 MCP Server 连接
openclaw mcp test <server-name>
# 查看 MCP 日志
openclaw logs --filter mcp
# 常见原因及修复:
# 1. npx 命令未找到 → 确保 Node.js 已正确安装
# 2. 包下载失败 → 检查网络,可配置 npm 镜像
# 3. 权限不足 → 确保 Server 有权访问目标目录/数据库
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 内存占用持续增长 | Session 积累 | 设置 max_history 限制,启用 summarization |
| 响应越来越慢 | 上下文过长 | 启用自动摘要,减少 max_history |
| CPU 占用过高 | 并发会话过多 | 降低 max_concurrent 配置 |
# 优化配置示例
runtime:
max_concurrent: 5
timeout: 60
memory:
max_history: 30
summarization: true
summary_interval: 15
# 系统状态
openclaw gateway status # Gateway 运行状态
openclaw --version # 版本信息
openclaw diagnose # 完整诊断(检查配置、依赖、网络)
# 日志
openclaw logs # 查看最近日志
openclaw logs --follow # 实时跟踪
openclaw logs --level error # 只看错误
openclaw logs --filter telegram # 按渠道过滤
# 会话
openclaw sessions list # 活跃会话列表
openclaw sessions view <id> # 查看会话详情
openclaw sessions kill <id> # 终止会话
# 配置
openclaw configure # 交互式配置
openclaw config show # 查看当前配置
openclaw config validate # 验证配置合法性
如果以上方法都无法解决问题:
提交 Issue 时请附带:
openclaw --version 输出openclaw diagnose 结果openclaw logs --level error --limit 30)本指南基于 OpenClaw 公开文档和实际使用经验编写,如有更新请以 docs.openclaw.ai 为准。