Manuscript Offprint

arXiv:2609.40118

Persistent Context Graphs for Efficient Memory Compaction in LLM Agents

Jingbo Yang, Kwei-Herng Lai, Xiaowen Wang, Zhaoxuan Tan, Pei Zhou, Mengting Wan, Yaar Harari, Evgeniy Gabrilovich, Shiyu Chang

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

As LLM capabilities advance, agents are tackling increasingly complex tasks over longer horizons. Their growing interaction histories make memory compaction essential for staying within context windows and reducing prefill cost. Existing methods summarize the history or compress its KV cache, often adding model computation to preserve information for future requests. A new user request can change which history matters, but reassessing that history with the model requires re-encoding it if the KV cache has expired. Past attention provides signals of historical importance and dependencies between messages, while relevance to the current task must be assessed using the new user request. We introduce RECAP, a memory compaction method that stores attention-derived importance scores and dependency links in a lightweight, persistent context graph. For each new request, RECAP combines stored importance with relevance cues from the request and follows dependency links to select messages and their supporting context, without additional model calls for selection. Compared with Codex's default summarization-based compaction, RECAP reduces estimated latency for compaction and cold restoration by approximately 95% on both Qwen3-Coder and gpt-oss. It also roughly halves the historical context per call on SWE-Together at comparable task quality and improves accuracy on the code tasks of Lost-in-Conversation over full history by 19.8 and 41.2 points. Code is available at https://github.com/UCSB-NLP-Chang/ReCAP.

1 INTRODUCTION

Advances in LLM capabilities are enabling agents to solve increasingly complex tasks over longer horizons (e.g., repository-level coding and autonomous research). Such workflows can run for tens of minutes to hours, accumulating user instructions, intermediate decisions, and tool outputs as execution proceeds. The resulting history can eventually exceed the model's finite context window. It also raises inference cost: measurements of agentic workloads report inputs tens of times longer than their generated outputs, making repeated context encoding a substantial part of execution (Lu et al., 2026). Modern agent harnesses and recent research therefore adopt memory compaction, which consolidates session memory into a shorter context (Kang et al., 2025). By limiting the history carried into subsequent calls, compaction allows the agent to continue beyond a single context window while reducing repeated prefilling.

Existing approaches compact agent memory either by summarizing the history into a shorter prompt or by retaining a subset or compressed form of the model's KV cache (Kang et al., 2025; Li et al., 2024; Zweiger et al., 2026). Doing so efficiently across an interactive session involves three linked challenges. ① What to retain depends on future use. A new user request can change the task's direction or revisit an earlier decision, so compaction performed before it arrives must preserve information for needs not yet known. Query-aware KV eviction, for instance, degrades when later queries differ from the one that guided eviction (Li et al., 2025; Kim et al., 2026). ② Anticipating future use requires additional computation. Deciding which history a later request will use requires queries that resemble that request, which do not yet exist when compaction runs. Attention Matching (Zweiger et al., 2026), for example, synthesizes such reference queries by re-prefilling the

*Correspondence to: Jingbo Yangjingbo@ucsb.edu.

Figure

Figure 1: RECAP preserves attention-derived importance and dependency links in a persistent context graph. For each new request, it combines stored importance with the request's relevance cues to select historical blocks and their supporting context, retaining protected contexts.

context or simulating interactions and fits the compact cache to them, adding model computation to every compaction. 3◯\textcircled{3} Cache expiration adds reconstruction cost. In about 4,300 Claude Code and Codex sessions, user think time accounts for 92% of session time, and requests after hour-long gaps almost always miss the serving cache (Zhu et al., 2026). Compacted KV states must then be stored and reloaded (Gao et al., 2025), and a compactor that waits for the request must re-encode the history to obtain fresh attention.

Each forward pass of the agent already computes attention over its history, yet this signal is discarded once the request completes. We study how coding agents attend to their histories across user turns and find that attention concentrates on a small part of the history, that past attention predicts which history the next turn reuses, and that the arriving request redirects attention ( §3.2§3.2). In light of these findings, we propose RECAP, a memory compaction method that keeps this attention rather than discarding it and organizes it in a lightweight, persistent context graph (Figure 1): nodes are history blocks annotated with accumulated importance, and edges link each block to the earlier blocks it depends on. When a new request arrives, RECAP selects from the full history by combining stored importance with the request's identifier overlap and following dependency edges, so selection adapts to the actual request and can restore previously omitted blocks ( ①①). The graph is built from attention that inference computes anyway, and selection uses only graph traversal and string matching, so compaction adds no model call ( ②②). Because the graph persists independently of the KV cache, RECAP can select after the cache expires without re-encoding the full history ( ③③).

Our contributions are as follows:

• We show that attention from ordinary agent inference concentrates on a small part of the history, predicts which history the next turn reuses, and is redirected by the arriving request ( §3.2§3.2).

• We propose RECAP, which persists this attention in a context graph of block importance and dependencies and queries it for each new request to select context without an additional model call, independently of the KV cache ( §4§4).

• On SWE-Together (Wu et al., 2026b) and the code tasks of Lost-in-Conversation (Laban et al., 2026) with two model families, RECAP reduces the estimated latency of compaction and cold restoration by approximately 95% relative to Codex's default summarization, roughly halves the historical context on SWE-Together at comparable quality, and improves Lost-in-Conversation accuracy over full-history conditioning by 19.8 and 41.2 points ( §5§5).

References (70)