从入门到精通 — 让 AI Agent 稳定运行
OpenClaw 是一个复杂的 AI Agent 平台,涉及模型调用、工具执行、状态管理等多个环节。任何一个环节出问题,都可能导致 Agent 行为异常。
本指南将带你从入门到精通,掌握 OpenClaw 故障排查的核心技能。
发送消息后,Agent 没有任何反应,或者一直显示"正在思考..."
# 步骤 1:检查 Agent 状态
openclaw agent status my-agent
# 步骤 2:查看日志
openclaw logs my-agent --last 50
# 步骤 3:常见原因和修复
# 原因 1:模型 API 未配置
export OPENAI_API_KEY="sk-..."
openclaw config set model.provider openai
# 原因 2:模型 API 错误
# 检查 API Key 是否有效、余额是否充足
# 原因 3:Context Window 满了
openclaw context clear my-agent
# 原因 4:Agent 卡死(死循环)
openclaw agent restart my-agent
Agent 尝试调用工具(如 web_search)但失败,返回 "Tool execution failed"
# 步骤 1:检查工具是否安装
openclaw skills list | grep web_search
# 步骤 2:测试工具
openclaw tool test web_search --query "test"
# 步骤 3:查看工具日志
openclaw tool logs web_search
# 常见原因:
# 1. 工具未安装 → openclaw skills install clawhub/web-search
# 2. 工具依赖缺失 → 检查工具的 README
# 3. 权限不足 → openclaw permissions add my-agent tool_execute
# 4. 网络问题 → 检查防火墙、代理设置
Agent 不记得之前的对话,或者记忆内容错误
# 步骤 1:检查 Memory 配置
openclaw config get memory
# 步骤 2:检查 Memory 后端
openclaw memory status
# 步骤 3:测试记忆存储和检索
openclaw memory test
# 常见原因:
# 1. Memory Skill 未安装 → openclaw skills install clawhub/memory-core
# 2. 记忆后端未启动(如 Redis)→ 启动后端服务
# 3. Context Window 策略问题 → 调整 context.strategy
# 4. 记忆 TTL 过短 → 增加 memory.ttl
# 查看实时日志
openclaw logs my-agent --follow
# 日志示例:
[2026-07-04 01:05:23] [INFO] Agent my-agent started
[2026-07-04 01:05:24] [DEBUG] Loading context (145,234 tokens)
[2026-07-04 01:05:25] [INFO] Calling model: gpt-5.6-sol
[2026-07-04 01:05:27] [DEBUG] Model response received (2,345 tokens)
[2026-07-04 01:05:28] [INFO] Executing tool: web_search
[2026-07-04 01:05:30] [ERROR] Tool execution failed: API rate limit
[2026-07-04 01:05:31] [WARN] Retrying tool execution (1/3)
[2026-07-04 01:05:33] [INFO] Tool execution succeeded
| 级别 | 说明 | 用途 |
|---|---|---|
| ERROR | 错误 | 需要立即修复的问题 |
| WARN | 警告 | 潜在问题,需要关注 |
| INFO | 信息 | 正常操作记录 |
| DEBUG | 调试 | 详细诊断信息 |
| TRACE | 追踪 | 非常详细的追踪信息 |
# ~/.openclaw/config.yaml
logging:
level: debug # 或 trace
output: file # 或 console, both
file: /var/log/openclaw/agent.log
rotation: daily # 日志轮转
retention: 7d # 保留 7 天
# 生成性能报告
openclaw analyze performance my-agent --last 24h
# 报告示例:
Performance Report for my-agent
=====================================
1. Model Latency: 1,250ms (avg)
- GPT-5.6 Sol: 1,400ms
- GPT-5.6 Turbo: 800ms
→ Recommendation: Use Turbo for non-reasoning tasks
2. Token Usage: 8.5M tokens (24h)
- Input: 6.2M (73%)
- Output: 2.3M (27%)
→ Recommendation: Enable token caching
3. Tool Execution: 1,200 calls (24h)
- web_search: 450 calls (avg 1.2s)
- memory: 350 calls (avg 0.3s)
- write: 200 calls (avg 0.5s)
→ Recommendation: Parallelize tool calls
4. Error Rate: 2.3%
- Rate limit: 15 errors
- Timeout: 8 errors
- Invalid request: 5 errors
→ Recommendation: Implement retry with backoff
# 运行自动诊断
openclaw doctor
# 输出示例:
OpenClaw System Diagnosis
========================
✅ Config file: valid
✅ Model API: connected (latency: 120ms)
✅ Memory backend: connected (Redis 6.2.6)
⚠️ Token usage: high (95% of budget)
→ Recommendation: Enable token caching
⚠️ Memory TTL: short (1 day)
→ Recommendation: Increase to 30 days
❌ Skill "web-scraper": failed dependencies
→ Fix: run `openclaw skills fix web-scraper`
Summary: 2 errors, 2 warnings, 2 recommendations
# 以调试模式启动 Agent
openclaw agent run my-agent --debug
# 使用 Node.js 调试器
node --inspect $(which openclaw) agent run my-agent
# 在 Chrome DevTools 中调试
# 1. 打开 chrome://inspect
# 2. 点击 "Open dedicated DevTools for Node"
# 3. 设置断点、单步执行、查看变量
| 资源 | 链接 | 用途 |
|---|---|---|
| OpenClaw GitHub | github.com/openclaw/openclaw | 源码、Issues、PRs |
| OpenClaw Discord | discord.gg/openclaw | 实时社区支持 |
| ClawHub | clawhub.com | Skills 市场和文档 |
| OpenClaw 文档 | docs.openclaw.ai | 官方文档和教程 |
| Stack Overflow | [openclaw] 标签 | 问答和搜索 |
# 生成 Bug 报告
openclaw bug-report --include-logs --include-config
# 会生成包含以下内容的报告:
# - OpenClaw 版本
# - 操作系统信息
# - 配置文件(脱敏后)
# - 最近的错误日志
# - 复现步骤
# 然后到 GitHub Issues 提交
# https://github.com/openclaw/openclaw/issues/new