词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!

当对话太长时,OpenCode 会自动压缩历史消息,腾出空间继续对话。
/compact 命令的使用时机和效果Error: Request too large for context window 时Context 百分比 = (input + output + reasoning + cache.read + cache.write) / model.limit.context * 100input: 输入给模型的 tokenoutput: 模型输出的 tokenreasoning: 推理模型的思考过程(如 Claude Thinking)cache.read: 从缓存读取的 tokencache.write: 写入缓存的 tokensrc/session/compaction.ts:32-48):input + output + cache.read + cache.writemin(20,000, 模型最大输出)input_limit - reservedtoken_count >= 180,000 时触发压缩compactedcompaction)PermissionNext.fromConfig({ "*": "deny" }))"Provide a detailed prompt for continuing our conversation above.
Focus on information that would be helpful for continuing the conversation,
including what we did, what we're doing, which files we're working on,
and what we're going to do next considering new session will not have access to our conversation."summary: truecompaction 标记,记录压缩点src/provider/models.ts),每次启动时会更新本地缓存。| 模型 | Provider | Context | Output | 触发阈值* |
|---|---|---|---|---|
| Claude Sonnet 4.5 | Anthropic | 200,000 | 64,000 | ~168,000 |
| Claude Opus 4.5 | Anthropic | 200,000 | 64,000 | ~168,000 |
| Gemini 3 Pro | 1,000,000 | 64,000 | ~968,000 | |
| Gemini 3 Flash | 1,048,576 | 65,536 | ~1,016,536 | |
| GPT 5.2 Pro | OpenAI | 400,000 | 128,000 | ~368,000 |
| GPT 5.2 | OpenAI | 400,000 | 128,000 | ~368,000 |
| GLM 4.7 | ZhipuAI | 204,800 | 131,072 | ~172,728 |
| DeepSeek Chat | DeepSeek | 128,000 | 8,192 | ~119,808 |
*触发阈值 = context - min(output, 32,000),仅供参考,实际值以 models.dev 为准
Context
145,234 tokens 73% used/compact/summarize<leader>c(默认是 ,c)Compacting... 提示Context
45,678 tokens 23% used| 场景 | 能否看到原始对话 |
|---|---|
| 用户翻页查看 | ✅ 能(数据库未删除) |
| 发送给模型 | ❌ 不能(只发送压缩点之后的消息) |
/compact 命令| 位置 | 优先级 | 说明 |
|---|---|---|
./opencode.json | 高 | 项目级配置(推荐用于项目特定设置) |
~/.config/opencode/opencode.json | 低 | 全局配置(推荐用于个人偏好) |
OPENCODE_CONFIG 环境变量 | 最高 | 自定义配置文件路径 |
OPENCODE_CONFIG_DIR 环境变量 | - | 自定义配置目录 |
{
"compaction": {
"reserved": 30000
}
}min(20000, 模型最大输出){
"compaction": {
"auto": false
}
}{
"compaction": {
"prune": false
}
}{
"provider": {
"openai": {
"models": {
"gpt-5.2": {
"limit": {
"context": 100000,
"output": 10000
}
}
}
}
}
}| 现象 | 原因 | 解决 |
|---|---|---|
| 压缩后模型记不住细节 | 压缩摘要丢失了关键信息 | 手动补充关键信息,或重启对话 |
| 压缩了还是超限 | 模型输出太长,超过预留空间 | 减少 max_tokens,或换 context 更大的模型 |
| Context 百分比显示不正确 | models.dev 数据过期,本地缓存未更新 | 删除缓存,重启 OpenCode |
| 不想用压缩 | 禁用了自动压缩,但还是触发 | 检查环境变量 OPENCODE_DISABLE_AUTOCOMPACT 是否生效 |
(input + output + reasoning + cache.read + cache.write) / model.limit.context * 100token_count >= (input_limit - reserved),其中 reserved 默认 20,000compaction.auto、compaction.prune 和 compaction.reserved 配置provider.models 中覆盖 limit.context 和 limit.output下一课我们学习 5.21 思考深度配置。 你会学到: 为单个模型设置思考预算 用变体机制控制推理深度 用 Ctrl+T 快速切换
更新时间:2026-02-14(v1.1.57)
| 功能 | 文件路径 | 行号 |
|---|---|---|
| 压缩核心逻辑 | src/session/compaction.ts | 全文件 |
| 自动触发判断(含 reserved) | src/session/compaction.ts | 32-48 |
| Prune 裁剪逻辑 | src/session/compaction.ts | 58-90 |
| Context 百分比计算 | src/cli/cmd/tui/routes/session/sidebar.tsx | 51-61 |
| 压缩配置项 | src/config/config.ts | 927-932 |
| 模型 limit Schema | src/provider/provider.ts | 498-501 |
| 用户自定义 limit | src/provider/provider.ts | 722-724 |
| Compaction Agent | src/agent/agent.ts | 122-136 |
| Compaction Prompt | src/agent/prompt/compaction.txt | 全文件 |
| TUI 显示消息(不过滤) | src/session/index.ts | 492-506 |
| 发送给模型时过滤 | src/session/prompt.ts | 298 |
| filterCompacted 实现 | src/session/message-v2.ts | 794-809 |
COMPACTION_BUFFER = 20,000:默认预留空间(v1.1.57 新增)PRUNE_PROTECT = 40,000:保护最近 40K tokens 的工具输出PRUNE_MINIMUM = 20,000:最小裁剪阈值