AI 编程 3.0 · 值得看 2026-06-23 · 文章

丢掉沉重的记忆:Codex、Claude Code 与 OpenCode 的上下文压缩术

中文正文 在使用 AI Agent 深度参与编程任务时,你一定遇到过这种窘境:起初 AI 反应敏捷,指哪打哪;但随着对话轮次增加,它似乎开始变得越来越笨。上下文快用完的时候,AI 会着急完成导致效果不佳,社区中称作 Context Anxiety(上下文焦虑)。 为了维持对话,Agent 必须丢掉一部分记忆(压缩 - Compact)。怎么丢、丢掉谁、丢掉后怎么补救,成了衡量一个 Agent 运行时是否成熟的分水岭。 三款主流 CLI Agent 的压缩策略 Codex CLI(OpenAI) 采用"工作交接单"式的总结与替换策略。把之前的全部对话交给 LLM 写一份"工作交接摘要",然后用这份摘要替换掉原始历史。它提供本地路径(调用任意 LLM 生成摘要)和远程路径(调用 OpenAI 内部 API)。...

打开原文回到归档

中文正文

在使用 AI Agent 深度参与编程任务时,你一定遇到过这种窘境:起初 AI 反应敏捷,指哪打哪;但随着对话轮次增加,它似乎开始变得越来越笨。上下文快用完的时候,AI 会着急完成导致效果不佳,社区中称作 Context Anxiety(上下文焦虑)。

为了维持对话,Agent 必须丢掉一部分记忆(压缩 - Compact)。怎么丢、丢掉谁、丢掉后怎么补救,成了衡量一个 Agent 运行时是否成熟的分水岭。

三款主流 CLI Agent 的压缩策略

Codex CLI(OpenAI) 采用"工作交接单"式的总结与替换策略。把之前的全部对话交给 LLM 写一份"工作交接摘要",然后用这份摘要替换掉原始历史。它提供本地路径(调用任意 LLM 生成摘要)和远程路径(调用 OpenAI 内部 API)。压缩后所有消息变成 4 条:原封不动保留所有 User 消息,插入一条伪造的 Assistant 消息作为结构化交接总结。

Claude Code(Anthropic) 设计了三层递进机制:工具结果修剪 → Prompt Cache 友好策略 → 9 部分结构化 LLM 总结。它的核心洞察是工具结果占用 81% tokens 但修复后价值骤降,因此主动修剪而非等到溢出。

OpenCode 以非物理删除的时间戳标记隐藏配合 5 标题 LLM 摘要实现"阶梯治理"。它保留所有历史标记但折叠旧内容,仅在需要时才恢复。

核心洞察

最好的上下文管理不是无限扩大记忆容量,而是学会精密地遗忘。三款 Agent 各有侧重:Codex 追求简洁交接,Claude Code 追求精准价值判断,OpenCode 追求渐进式遗忘。

English Original

When using AI Agents for programming tasks, you inevitably hit a wall: the AI starts responsive but grows increasingly sluggish as conversation rounds accumulate. When context runs low, the AI hurries to complete tasks, leading to poor results — a phenomenon the community calls Context Anxiety.

To maintain the conversation, the Agent must shed some memories (compress). How to shed, what to shed, and how to recover afterward become the dividing lines that measure an Agent's runtime maturity.

Compression Strategies of Three Mainstream CLI Agents

Codex CLI (OpenAI) uses a "work handover note" style summarize-and-replace strategy: hand all previous conversation to an LLM to write a "handover summary," then replace the original history with it. It offers both local path (using any LLM) and remote path (OpenAI's internal API). After compression, all messages become 4 items: all User messages are preserved intact, and a fabricated Assistant message is inserted as a structured handover summary.

Claude Code (Anthropic) employs a three-layer progressive mechanism: tool result pruning → Prompt Cache-friendly strategy → 9-part structured LLM summary. Its core insight is that tool results occupy 81% of tokens but drop sharply in value after the bug is fixed, so it proactively prunes rather than waiting for overflow.

OpenCode uses non-physical deletion with timestamp marking combined with a 5-header LLM summary to achieve "stepped governance." It keeps all history markers but collapses old content, only restoring it when needed.

Core Insight

The best context management is not to infinitely expand memory capacity, but to learn to forget precisely. Each Agent has its own emphasis: Codex pursues clean handover, Claude Code pursues precise value judgment, and OpenCode pursues gradual forgetting.