RECAP: Training Long-horizon LLM Agents with Context Compaction
- Source
- arxiv.org
- Author
- Yulun Jiang, Maria Brbic, Michael Moor
- Date

Context
Long-running agents eventually fill their , so many harnesses compact: the agent writes a summary of its work so far, the older history is replaced by that summary, and the agent carries on. A bad summary can quietly drop something the agent needed, and Compact-Bench, a separate benchmark, measured that summaries from 14 different models broke between 5% and 64% of coding repairs that succeeded with the full history. RECAP, from researchers at EPFL and ETH Zurich, is a reinforcement-learning method that trains an agent to write summaries that preserve what it would have done with its full history (arXiv).
The usual approach trains summaries only through the final task reward: the whole run succeeds or fails, and every step, summary included, gets the same credit. That signal is weak for a summary whose effect plays out over dozens of later steps. RECAP adds a more direct one. After each , the same model is asked how likely it would be to produce each of its next actions twice, once given the summary and once given the original full history, and the gap between the two, a divergence over every token of those actions, measures how much the summary distorted its behavior. That gap trains two things at once. The summary is rewarded for keeping it small, and the actions taken after compaction are nudged toward what the full-history version of the model would have done, a form of self- where the model with more information acts as its own teacher. Both signals come from runs the method already collects, so it needs no extra environment interactions, and the authors report training time roughly on par with the baseline.
The authors trained Qwen3.5-4B, a small open model from Qwen, with a 32K-token working context and up to three compactions per run, on 300 deep-research questions from BrowseComp-Plus and 300 coding tasks from SWE-smith. Against the same training with task reward only, RECAP raised accuracy on 150 held-out BrowseComp-Plus questions from 58.2% to 62.8% and on all 500 tasks from 46.8% to 51.1%, averaged over four evaluation runs. That closes much of the gap to the same model trained and run with a 128K window and no compaction, which scored 63.8% and 55.5%. The trained agents also made fewer tool calls, and the advantage grew when the window shrank to 16K and compaction happened more often. Each signal alone did less: rewarding summaries alone gave mixed results (accuracy fell 1.5 points on BrowseComp-Plus and rose 1.1 on SWE-bench Verified) and much longer summaries on BrowseComp-Plus, while self-distillation alone reached 60.3% and 47.1%.
The evidence covers one 4B model on two benchmarks, which the authors acknowledge. The BrowseComp-Plus gain is less certain than the coding one, because RECAP's accuracy there had a standard deviation of 3.9 points across its four evaluation runs, against 1.5 for the baseline. On SWE-bench Verified the result also depends on how heavily the new signal is weighted: across the four weights tested, accuracy ranged from 46.3% to 51.1%, and the reported setting was chosen on a separate validation set. What the paper does establish is a usable training signal: judge a summary by whether the agent behaves the same after reading it, not only by whether the whole task eventually succeeds. Whether that signal pays off for larger models and longer tasks is still untested.
- RECAP's objective measures how far the agent's later behavior diverges when it acts from the compressed summary instead of the original interaction history, rather than scoring the summary text itself.
- The authors show the gradient of that distortion objective splits into two signals: a distortion-based reward for writing the summary and self-distillation for the actions taken after compaction.
- Reported gains over task-reward-only RL are 4.6 points on BrowseComp-Plus and 4.3 on SWE-bench Verified, and ablations credit supervising both summary writing and post-compaction behavior together.
- Context and context window — Context is the information available for a generation; the context window is the capacity available to hold it.
- Compaction — Replacing older content in the model's context with a summary of it, usually keeping recent messages, so that a session can go on.
- distillation — Training a small, cheap model to imitate a big one's outputs, keeping much of the capability at a fraction of the cost.
- SWE-bench — The standard benchmark for AI coding agents: real GitHub issues from real repositories, scored by whether the agent's patch passes the project's own tests.
articleMEMENTO: Learning Context Management for Long Context Processing AgentsYunfan Zhang, Taehoon Hwang, Kathleen McKeown, Smaranda Muresan
articleLost in Compaction: Evaluating Side-Constraint Loss under Context CompactionZhiqi Wang, Yichi Zhang, Dongwon Lee, Yuchen Yang
articleCompact-Bench: Measuring What Context Compaction Costs Long-Horizon Coding AgentsRuopei Sun, Jianfeng Cai, Kangwen Zhao, Wengang Zhou, Houqiang Li