Skip to content

第17章:避坑指南与最佳实践

💡 前人经验:这些是社区总结的最佳实践,帮你避开常见的坑


📋 目录


❌ 新手常犯的10个错误

错误1:不看文档就开始用

问题

  • 不了解基本概念就开始配置
  • 遇到问题不知道如何解决
  • 浪费大量时间试错

正确做法

  1. ✅ 先阅读第1章:认识OpenClaw
  2. ✅ 按照快速上手指南操作
  3. ✅ 遇到问题先查常见问题

时间节省:至少节省2-3小时的试错时间


错误2:使用最贵的模型做所有事情

问题

bash
# ❌ 错误:用GPT-4做简单任务
openclaw config set models.default "gpt-4"

# 结果:月费用500+元

正确做法

bash
# ✅ 正确:根据任务选择模型
# 简单任务用DeepSeek
openclaw config set models.default "deepseek-chat"

# 复杂任务用GPT-4
openclaw config set models.complex "gpt-4"

# 结果:月费用30-50元,节省90%

成本对比

任务类型错误选择正确选择成本差异
文件搜索GPT-4 ($0.03/1K)DeepSeek ($0.001/1K)30倍
简单问答GPT-4DeepSeek30倍
代码生成GPT-4DeepSeek30倍
复杂推理GPT-4GPT-4相同

错误3:不配置工作目录

问题

  • OpenClaw可以访问所有文件
  • 误删除重要文件的风险
  • 隐私泄露风险

正确做法

bash
# ✅ 配置专门的工作目录
openclaw config set workspace.path "~/Documents/OpenClaw"

# ✅ 限制访问范围
openclaw config set files.searchPaths '["~/Documents/OpenClaw", "~/Desktop"]'

# ✅ 排除敏感目录
openclaw config set files.excludePaths '[
  "~/.ssh",
  "~/Documents/Private",
  "~/Documents/Finance"
]'

错误4:API密钥明文存储

问题

bash
# ❌ 错误:在配置文件中明文存储
{
  "models": {
    "providers": {
      "openai": {
        "apiKey": "sk-1234567890abcdef"  // 明文!
      }
    }
  }
}

正确做法

bash
# ✅ 使用环境变量
export OPENAI_API_KEY="sk-xxx"
export DEEPSEEK_API_KEY="sk-xxx"

# ✅ 或使用密钥管理工具
openclaw config set models.providers.openai.apiKey --from-env OPENAI_API_KEY

# ✅ 设置文件权限
chmod 600 ~/.openclaw/config.json

错误5:不定期清理缓存

问题

  • 缓存占用大量磁盘空间
  • 内存占用越来越高
  • 响应速度变慢

正确做法

bash
# ✅ 定期清理缓存(每周一次)
openclaw cache clear --history
openclaw cache clear --index

# ✅ 配置自动清理
openclaw config set cache.autoClean true
openclaw config set cache.maxAge 7  # 7天

# ✅ 限制缓存大小
openclaw config set cache.maxSize 1000  # MB

错误6:忽略版本更新

问题

  • 错过重要功能更新
  • 错过安全补丁
  • 遇到已修复的bug

正确做法

bash
# ✅ 定期检查更新(每月一次)
openclaw update check

# ✅ 查看更新日志
openclaw changelog

# ✅ 更新到最新版本
openclaw update

# ✅ 订阅更新通知
# 关注GitHub Release: https://github.com/openclaw/openclaw/releases

错误7:不备份配置

问题

  • 配置丢失后需要重新设置
  • 无法恢复到之前的工作状态
  • 浪费大量时间

正确做法

bash
# ✅ 定期备份配置
# 方案1:手动备份
cp -r ~/.openclaw ~/.openclaw.backup.$(date +%Y%m%d)

# 方案2:使用Git管理
cd ~/.openclaw
git init
git add .
git commit -m "backup config"

# 方案3:自动备份脚本
cat > ~/backup-openclaw.sh << 'EOF'
#!/bin/bash
BACKUP_DIR=~/openclaw-backups
mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/openclaw-$(date +%Y%m%d-%H%M%S).tar.gz ~/.openclaw
# 保留最近7天的备份
find $BACKUP_DIR -name "openclaw-*.tar.gz" -mtime +7 -delete
EOF

chmod +x ~/backup-openclaw.sh

# 添加到crontab(每天备份)
# 0 2 * * * ~/backup-openclaw.sh

错误8:在生产环境测试新功能

问题

  • 新功能可能不稳定
  • 影响正常工作
  • 数据可能损坏

正确做法

bash
# ✅ 使用测试环境
# 方案1:创建测试配置
cp -r ~/.openclaw ~/.openclaw-test
export OPENCLAW_CONFIG_DIR=~/.openclaw-test

# 方案2:使用Docker
docker run -it openclaw/openclaw:latest

# 方案3:使用不同端口
openclaw gateway run --port 18790 --config ~/.openclaw-test/config.json

错误9:不监控API使用量

问题

  • API费用突然暴增
  • 不知道哪里消耗了额度
  • 预算超支

正确做法

bash
# ✅ 启用使用量监控
openclaw config set monitoring.enabled true

# ✅ 设置预算警告
openclaw config set monitoring.budget.daily 10  # 每天10元
openclaw config set monitoring.budget.monthly 300  # 每月300元

# ✅ 查看使用统计
openclaw stats usage --daily
openclaw stats usage --monthly

# ✅ 设置通知
openclaw config set monitoring.alerts.email "your@email.com"
openclaw config set monitoring.alerts.threshold 0.8  # 80%时警告

错误10:不使用Skills

问题

  • 手动实现已有的功能
  • 浪费时间重复造轮子
  • 功能不如专业Skills完善

正确做法

bash
# ✅ 先搜索是否有现成的Skills
openclaw skills search "file search"

# ✅ 安装必备Skills
clawhub install @openclaw/skill-file-search
clawhub install @openclaw/skill-web-search
clawhub install @openclaw/skill-calendar

# ✅ 定期浏览ClawHub
# 访问:https://clawhub.ai

🎯 模型选择避坑

场景1:日常对话

❌ 错误选择:GPT-4(贵且慢)

✅ 推荐选择

  1. DeepSeek-Chat(性价比最高)
  2. Kimi(中文友好)
  3. GLM-4(国产稳定)

配置示例

json
{
  "models": {
    "default": "deepseek-chat",
    "providers": {
      "deepseek": {
        "apiKey": "sk-xxx",
        "models": {
          "deepseek-chat": {
            "maxTokens": 4000,
            "temperature": 0.7
          }
        }
      }
    }
  }
}

场景2:代码生成

❌ 错误选择:通用对话模型

✅ 推荐选择

  1. DeepSeek-Coder(专业代码模型)
  2. Claude-3.5-Sonnet(代码能力强)
  3. GPT-4(复杂逻辑)

配置示例

json
{
  "models": {
    "code": "deepseek-coder",
    "providers": {
      "deepseek": {
        "models": {
          "deepseek-coder": {
            "maxTokens": 8000,
            "temperature": 0.2  // 代码生成用低温度
          }
        }
      }
    }
  }
}

场景3:长文档处理

❌ 错误选择:短上下文模型

✅ 推荐选择

  1. Kimi(200K上下文)
  2. Claude-3-Opus(200K上下文)
  3. GPT-4-Turbo(128K上下文)

配置示例

json
{
  "models": {
    "longContext": "kimi",
    "providers": {
      "moonshot": {
        "apiKey": "sk-xxx",
        "models": {
          "kimi": {
            "maxTokens": 200000
          }
        }
      }
    }
  }
}

场景4:多模态(图片理解)

❌ 错误选择:纯文本模型

✅ 推荐选择

  1. GPT-4-Vision
  2. Claude-3-Opus
  3. Gemini-Pro-Vision

配置示例

json
{
  "models": {
    "vision": "gpt-4-vision",
    "providers": {
      "openai": {
        "models": {
          "gpt-4-vision": {
            "maxTokens": 4000
          }
        }
      }
    }
  }
}

💰 成本控制避坑

策略1:分层使用模型

原则:简单任务用便宜模型,复杂任务用贵模型

实施方案

json
{
  "models": {
    "routing": {
      "enabled": true,
      "rules": [
        {
          "condition": "tokens < 500",
          "model": "deepseek-chat"  // 简单任务
        },
        {
          "condition": "tokens >= 500 && tokens < 2000",
          "model": "gpt-3.5-turbo"  // 中等任务
        },
        {
          "condition": "tokens >= 2000",
          "model": "gpt-4"  // 复杂任务
        }
      ]
    }
  }
}

成本节省:60-80%


策略2:使用缓存

原则:相同问题不重复调用API

实施方案

json
{
  "cache": {
    "enabled": true,
    "ttl": 3600,  // 1小时
    "maxSize": 1000,  // 最多缓存1000条
    "strategy": "lru"  // 最近最少使用
  }
}

成本节省:30-50%


策略3:批量处理

原则:合并多个小请求为一个大请求

❌ 错误做法

javascript
// 发送10次请求
for (let i = 0; i < 10; i++) {
  await openclaw.ask(`处理文件${i}`);
}
// 成本:10次API调用

✅ 正确做法

javascript
// 合并为1次请求
const files = Array.from({length: 10}, (_, i) => `文件${i}`);
await openclaw.ask(`批量处理这些文件:${files.join(', ')}`);
// 成本:1次API调用

成本节省:90%


策略4:设置预算限制

实施方案

bash
# 设置每日预算
openclaw config set budget.daily 10  # 10元/天

# 设置每月预算
openclaw config set budget.monthly 300  # 300元/月

# 超出预算时的行为
openclaw config set budget.onExceed "notify"  # 通知
# 或
openclaw config set budget.onExceed "stop"  # 停止服务

策略5:使用独享账号

适用场景:重度使用(每月>1000次调用)

成本对比

方案月调用次数按次付费独享账号节省
轻度使用100¥30¥200-170
中度使用500¥150¥200-50
重度使用2000¥600¥200+400
超重度5000¥1500¥200+1300

结论:月调用>1000次时,独享账号更划算


🔒 安全隐私注意事项

1. API密钥安全

必须做到

bash
# ✅ 使用环境变量
export OPENAI_API_KEY="sk-xxx"

# ✅ 设置文件权限
chmod 600 ~/.openclaw/config.json

# ✅ 不要提交到Git
echo ".openclaw/config.json" >> .gitignore

# ✅ 定期轮换密钥
# 每3个月更换一次API密钥

绝对不要

bash
# ❌ 明文存储在代码中
const apiKey = "sk-1234567890abcdef";

# ❌ 提交到公开仓库
git add config.json
git push

# ❌ 分享配置文件
# 不要把包含API密钥的配置文件发给别人

2. 数据隐私

敏感数据处理

json
{
  "privacy": {
    "enabled": true,
    "rules": [
      {
        "type": "phone",
        "action": "mask",  // 脱敏
        "pattern": "\\d{11}"
      },
      {
        "type": "email",
        "action": "mask"
      },
      {
        "type": "idcard",
        "action": "block"  // 阻止发送
      }
    ]
  }
}

文件访问控制

json
{
  "files": {
    "allowPaths": [
      "~/Documents/OpenClaw",
      "~/Desktop"
    ],
    "denyPaths": [
      "~/.ssh",
      "~/Documents/Private",
      "~/Documents/Finance",
      "~/Documents/Medical"
    ]
  }
}

3. 网络安全

使用HTTPS

json
{
  "gateway": {
    "ssl": {
      "enabled": true,
      "cert": "/path/to/cert.pem",
      "key": "/path/to/key.pem"
    }
  }
}

IP白名单

json
{
  "gateway": {
    "allowIPs": [
      "127.0.0.1",
      "192.168.1.0/24"
    ]
  }
}

4. 审计日志

启用审计

json
{
  "audit": {
    "enabled": true,
    "logLevel": "info",
    "logFile": "~/.openclaw/logs/audit.log",
    "retention": 90  // 保留90天
  }
}

定期检查

bash
# 查看最近的操作
tail -n 100 ~/.openclaw/logs/audit.log

# 搜索敏感操作
grep "delete" ~/.openclaw/logs/audit.log
grep "upload" ~/.openclaw/logs/audit.log

⚡ 性能优化最佳实践

1. 启用流式输出

配置

json
{
  "models": {
    "streaming": true,
    "streamingDelay": 50  // ms
  }
}

效果

  • 响应速度提升:50-70%
  • 用户体验更好:边生成边显示

2. 使用本地缓存

配置

json
{
  "cache": {
    "enabled": true,
    "type": "redis",  // 或 "memory"
    "redis": {
      "host": "localhost",
      "port": 6379
    }
  }
}

效果

  • 响应速度提升:80-90%
  • API调用减少:30-50%

3. 优化文件索引

配置

json
{
  "files": {
    "index": {
      "enabled": true,
      "incremental": true,  // 增量索引
      "schedule": "0 2 * * *",  // 每天凌晨2点
      "excludePatterns": [
        "node_modules",
        ".git",
        "*.log"
      ]
    }
  }
}

效果

  • 搜索速度提升:90%+
  • 磁盘占用减少:50%

4. 使用CDN加速

适用场景:云端部署

配置

json
{
  "cdn": {
    "enabled": true,
    "provider": "cloudflare",
    "domain": "openclaw.yourdomain.com"
  }
}

效果

  • 全球访问速度提升:60-80%
  • 服务器负载降低:40%

🧩 Skills使用最佳实践

1. 只安装需要的Skills

❌ 错误做法

bash
# 安装所有Skills
clawhub install --all

✅ 正确做法

bash
# 只安装需要的Skills
clawhub install @openclaw/skill-file-search
clawhub install @openclaw/skill-web-search
clawhub install @openclaw/skill-calendar

原因

  • 减少内存占用
  • 提升启动速度
  • 降低冲突风险

2. 定期更新Skills

配置自动更新

json
{
  "skills": {
    "autoUpdate": true,
    "updateSchedule": "0 3 * * 0"  // 每周日凌晨3点
  }
}

手动更新

bash
# 检查更新
openclaw skills outdated

# 更新所有Skills
openclaw skills update --all

# 更新特定Skills
openclaw skills update @openclaw/skill-file-search

3. 配置Skills优先级

配置

json
{
  "skills": {
    "priority": [
      "@openclaw/skill-file-search",  // 高优先级
      "@openclaw/skill-web-search",
      "@openclaw/skill-calendar"      // 低优先级
    ]
  }
}

效果

  • 提升响应速度
  • 减少冲突
  • 更精准的结果

📱 多平台集成最佳实践

1. 分离工作和个人

配置多个Agent

json
{
  "agents": {
    "work": {
      "model": "gpt-4",
      "workspace": "~/Documents/Work",
      "channels": ["feishu"]
    },
    "personal": {
      "model": "deepseek-chat",
      "workspace": "~/Documents/Personal",
      "channels": ["telegram"]
    }
  }
}

2. 配置消息过滤

避免信息过载

json
{
  "channels": {
    "feishu": {
      "filters": {
        "ignoreGroups": ["闲聊群", "通知群"],
        "onlyMentions": true,  // 只响应@消息
        "keywords": ["openclaw", "帮助"]
      }
    }
  }
}

3. 设置工作时间

配置

json
{
  "schedule": {
    "workHours": {
      "enabled": true,
      "timezone": "Asia/Shanghai",
      "hours": {
        "monday": ["09:00-18:00"],
        "tuesday": ["09:00-18:00"],
        "wednesday": ["09:00-18:00"],
        "thursday": ["09:00-18:00"],
        "friday": ["09:00-18:00"]
      }
    },
    "outOfHoursMessage": "我现在不在工作时间,紧急事项请发邮件"
  }
}

🔄 自动化工作流最佳实践

1. 使用幂等操作

原则:确保重复执行不会产生副作用

❌ 错误示例

javascript
// 每次执行都创建新文件
await createFile('report.txt', content);

✅ 正确示例

javascript
// 检查文件是否存在
if (!await fileExists('report.txt')) {
  await createFile('report.txt', content);
} else {
  await updateFile('report.txt', content);
}

2. 添加错误处理

配置

json
{
  "automation": {
    "errorHandling": {
      "retry": {
        "enabled": true,
        "maxAttempts": 3,
        "backoff": "exponential"
      },
      "notification": {
        "enabled": true,
        "channels": ["email", "feishu"]
      }
    }
  }
}

3. 记录执行日志

配置

json
{
  "automation": {
    "logging": {
      "enabled": true,
      "level": "info",
      "file": "~/.openclaw/logs/automation.log",
      "rotation": {
        "maxSize": "10M",
        "maxFiles": 10
      }
    }
  }
}

📚 相关资源


最后更新:2026年2月14日