🎖️ 任务奖励 — 学完你能做到
能写出生产级的 openclaw.json 配置,包括多模型切换和 Cron 任务隔离
📖 本章目标:掌握核心配置文件的结构与高级用法,实现成本最优配置
4.1 格式基础须知
| 注意事项 | 说明 |
|---|---|
| 格式 | JSON5,支持注释(//)和 trailing comma,不是标准 JSON |
| 错误影响 | 任何未知 key 或类型错误都会阻止 Gateway 启动 |
| 生效方式 | 修改后必须重启 Gateway 才能生效 |
| 推荐方式 | 用 openclaw config set 命令修改,比手动编辑更安全 |
# 修改前先验证 JSON 合法性
cat ~/.openclaw/openclaw.json | python3 -m json.tool
# 自动诊断修复
openclaw doctor --fix
4.2 完整配置结构(含注释)
{
"identity": {
"name": "Clawd",
"emoji": "🦞"
},
"gateway": {
"port": 18789,
"bind": "loopback", // 只监听 localhost,不要改成 0.0.0.0
"auth": {
"mode": "token",
"token": "${OPENCLAW_TOKEN}" // 用环境变量,不要硬编码
}
},
"agent": {
"workspace": "~/.openclaw/workspace",
"model": {
"primary": "anthropic/claude-sonnet-4-6",
"fallbacks": ["openai/gpt-5.2", "anthropic/claude-haiku-4-5"]
},
"contextTokens": 50000, // 限制最大上下文,防止爆炸
"heartbeat": {
"every": "1h",
"target": "last",
"delivery": { "directMessage": false }
}
}
}
4.3 Auth 轮换——OAuth 订阅优先,API Key 兜底
"auth": {
"profiles": {
"anthropic:sub": { "mode": "oauth", "email": "you@example.com" },
"anthropic:api": { "mode": "api_key" },
"openai:default": { "mode": "api_key" }
},
"order": {
"anthropic": ["anthropic:sub", "anthropic:api"],
"openai": ["openai:default"]
}
}
💡 省钱技巧
OAuth 订阅(如 Claude.ai Pro)成本 $20/月固定。API Key 按 token 计费。
把 OAuth 排在 auth 顺序第一位,流量先走订阅,超出限制再切 API Key——这是最省钱的组合。
4.4 接入本地 Ollama 模型(零 API 成本)
Step 1:安装并启动 Ollama
brew install ollama
ollama pull deepseek-coder:6.7b
ollama serve # 默认监听 localhost:11434
Step 2:在 openclaw.json 配置本地模型
"models": {
"mode": "merge",
"providers": {
"ollama": {
"baseUrl": "http://localhost:11434",
"apiKey": "ollama-local",
"api": "openai-completions",
"models": [{
"id": "ollama/deepseek-coder:6.7b",
"cost": { "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0 },
"contextWindow": 130000,
"maxTokens": 8000
}]
}
}
}
⚠️ 本地模型局限性
| 限制 | 说明 |
|---|---|
| 硬件要求 | 需要 GPU / 足够 RAM(7B 模型约需 8GB) |
| 推理速度 | 比云端模型慢 |
| Heartbeat Bug | 用本地模型做 heartbeat 有已知 Bug |
| 缓存 | 不支持 prompt caching |
💡 推荐用途:Cron 定时任务和简单摘要(不联网任务)
4.5 常用配置命令
# 查看某项配置
openclaw config get agent.model
# 修改配置(推荐用这个,而不是手动编辑 JSON)
openclaw config set agent.thinking false
openclaw config set agent.contextTokens 50000
openclaw config set gateway.dmScope per-peer
# 列出所有配置
openclaw config list
# 修改后重启生效
openclaw gateway restart