Featured image of post OpenCode 配置优化记录

OpenCode 配置优化记录

优化 OpenCode 配置以减少 token 消耗

语速

不知道大家是否意识到,实际上使用 MCP 等服务的时候,会大量消耗 token。这是由于一次提交的上下文数量过多,导致 token 消耗较大。

那么在使用 opencode 的时候,可以进行哪些优化呢?

我已经进行的优化

1. 启用 Compaction (自动压缩)

1
2
3
4
"compaction": {
  "auto": true,    // 上下文满时自动压缩
  "prune": true    // 移除旧的工具输出以节省空间
}

效果: 预估节省 20-30% token

2. 配置 Watcher 忽略模式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
"watcher": {
  "ignore": [
    "node_modules/**",
    "dist/**",
    ".git/**",
    "*.lock",
    "**/*.log",
    ".cache/**",
    "build/**",
    "__pycache__/**",
    ".venv/**",
    "venv/**"
  ]
}

效果: 减少不必要的文件监控,预估节省 5-10% token

3. 配置 Small Model

1
"small_model": "anthropic/claude-haiku-4-5"

效果: 轻量任务自动使用便宜模型,预估节省 10-20% token

我没有采用的优化项

这里的优化项是我认为暂时没有必要采用的。

5. 减少 MCP 服务 (高优先级)

MCP 工具描述可消耗 6000+ tokens。建议禁用不常用的:

1
2
3
4
"playwright_headless": { "enabled": false },
"zai-mcp-server": { "enabled": false },
"tmux-mcp": { "enabled": false },
"sentry": { "enabled": false }

预估节省: 30-50% token

6. 创建项目级 .ignore 文件

在项目根目录创建 .ignore:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
node_modules/
dist/
build/
*.lock
*.log
coverage/
.next/
__pycache__/
*.pyc
.venv/

7. 使用技巧

  • 使用 /compact 手动压缩长对话
  • 使用 /clear 清理历史
  • 完成任务后开始新会话,避免"上下文腐烂"

不能采用的优化项

这些优化项会导致风控

启用 Anthropic 缓存

1
2
3
4
5
6
7
8
"provider": {
  "anthropic": {
    "options": {
      "setCacheKey": true,
      "timeout": 600000
    }
  }
}

配置文件位置

  • 主配置: ~/.config/opencode/opencode.json
  • Oh-My-OpenCode: ~/.config/opencode/oh-my-opencode.json
  • Memory 插件: ~/.config/opencode/opencode-mem.jsonc

参考资料