What is Compaction
Compaction is the practice of summarizing context contents when approaching limits, then reinitializing a new context window with the summary. This distills the contents of a context window in a high-fidelity manner, enabling the agent to continue with minimal performance degradation.
Compaction typically serves as the first lever in context optimization. The art lies in selecting what to keep versus what to discard.
Compaction Implementation
Compaction works by identifying sections that can be compressed, generating summaries that capture essential points, and replacing full content with summaries. Priority for compression goes to tool outputs (replace with summaries), old turns (summarize early conversation), retrieved docs (summarize if recent versions exist), and never compress system prompt.
Summary Generation
Effective summaries preserve different elements depending on message type:
Tool outputs: Preserve key findings, metrics, and conclusions. Remove verbose raw output.
Conversational turns: Preserve key decisions, commitments, and context shifts. Remove filler and back-and-forth.
Retrieved documents: Preserve key facts and claims. Remove supporting evidence and elaboration.
什么是Compaction
Compaction是在接近上下文限制时总结上下文内容,然后用总结内容重新初始化新上下文窗口的实践。它能以高保真度提炼上下文窗口的内容,使Agent能在性能损失极小的情况下继续运行。
Compaction通常是上下文优化的首要手段。其精髓在于选择保留和舍弃的内容。
Compaction实现方式
Compaction的工作原理是识别可压缩的部分,生成能捕捉要点的总结,并用总结替代完整内容。压缩优先级为:工具输出(用总结替代)、早期对话回合(总结早期对话)、检索到的文档(若有最新版本则总结),且绝不要压缩系统提示词。
总结生成
有效的总结会根据消息类型保留不同元素:
工具输出:保留关键发现、指标和结论。移除冗余的原始输出。
对话回合:保留关键决策、承诺和上下文转换。移除填充内容和来回讨论。
检索到的文档:保留关键事实和主张。移除支持证据和详细阐述。