让大模型长对话记忆更高效,通过图结构管理状态并智能删减冗余内容。
Contextual Memory Virtualisation: DAG-Based State Management and Structurally Lossless Trimming for LLM Agents
- 用有向无环图建模对话状态,支持多会话复用和版本管理。
- 三遍修剪算法保留所有对话内容,平均降耗20%,最高达86%。
- 适合频繁调用工具的编程类任务,10轮内即可回本,提升推理效率。
当大语言模型执行长序列推理任务时,其上下文窗口会积累大量状态信息,包括架构映射、权衡决策和代码规范等。这些理解在会话达到上下文上限后常因损失性压缩而丢失。本文提出上下文记忆虚拟化(CMV),将累积的理解视为受版本控制的状态。借鉴操作系统虚拟内存机制,CMV将会话历史建模为有向无环图(DAG),并定义了快照、分支与修剪原语,支持跨独立并行会话的上下文复用。我们设计了一种三遍结构无损修剪算法,在完整保留用户消息与助手回复的前提下,通过去除机械冗余内容(如原始工具输出、base64图片、元数据)实现平均20%、最高86%的令牌缩减。单用户案例研究覆盖76个真实编程会话,表明在提示缓存条件下修剪仍具经济性,混合工具使用场景平均减少39%消耗,10轮内即达盈亏平衡。参考实现已开源:https://github.com/CosmoNaught/claude-code-cmv。
原文摘要 · Abstract (English)
As large language models engage in extended reasoning tasks, they accumulate significant state -- architectural mappings, trade-off decisions, codebase conventions -- within the context window. This understanding is lost when sessions reach context limits and undergo lossy compaction. We propose Contextual Memory Virtualisation (CMV), a system that treats accumulated LLM understanding as version-controlled state. Borrowing from operating system virtual memory, CMV models session history as a Directed Acyclic Graph (DAG) with formally defined snapshot, branch, and trim primitives that enable context reuse across independent parallel sessions. We introduce a three-pass structurally lossless trimming algorithm that preserves every user message and assistant response verbatim while reducing token counts by a mean of 20% and up to 86% for sessions with significant overhead by stripping mechanical bloat such as raw tool outputs, base64 images, and metadata. A single-user case-study evaluation across 76 real-world coding sessions demonstrates that trimming remains economically viable under prompt caching, with the strongest gains in mixed tool-use sessions, which average 39% reduction and reach break-even within 10 turns. A reference implementation is available at https://github.com/CosmoNaught/claude-code-cmv.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。