CompactionRL: Reinforcement Learning with Context Compaction for Long-Horizon Agents

Yujiang Li, Zhenyu Hou, Yi Jing, Jie Tang, Yuxiao DongarXiv 2026

TL;DR

CompactionRL is a new reinforcement learning framework that trains long-horizon LLM agents to dynamically compress their own interaction histories when context limits are reached. By jointly optimizing task execution and summary generation using a modified PPO formulation, CompactionRL significantly extends an agent's effective operational horizon without requiring larger physical context windows.

What Problem Does This Paper Address?

As LLM agents are increasingly deployed on complex, long-horizon tasks—such as software engineering or terminal-based problem solving—they generate massive interaction histories filled with tool outputs, code blocks, and error messages. These trajectories quickly exhaust the model's finite context window. While simply scaling up context windows is a common engineering response, it is computationally expensive and often suffers from degraded information retrieval performance (the "lost in the middle" effect).

Context compaction—summarizing past interactions to free up space—is a natural alternative. However, prior approaches treat compaction as an inference-time heuristic rather than a learned skill. Integrating compaction into Reinforcement Learning (RL) pipelines introduces severe challenges:

  1. Variable Trajectory Segments: Dynamic compaction splits a single rollout into a variable number of segments, making popular group-relative RL methods (like GRPO) ill-suited due to length and segment-count imbalances.
  2. Broken Credit Assignment: Treating compacted segments as independent training samples distorts temporal credit assignment, making final task rewards appear artificially close to early actions.

Methods

The authors introduce CompactionRL, a value-based PPO framework designed to jointly train trajectory generation and context summarization under a single, unified task reward.

Overview of the CompactionRL framework compared to standard RL

When the remaining context budget drops below a threshold ($T_{\text{comp}}$), the model is prompted to generate a summary ($S_t$) that preserves goals, completed actions, and unresolved errors. The context is then reconstructed using this summary plus a small tail of recent turns.

To make RL training stable under this setup, CompactionRL introduces two key innovations:

  • Token-Level Loss Normalization: Instead of averaging losses at the sequence or segment level (which would disproportionately weight rollouts with frequent compactions), the framework normalizes across all generated tokens in a batch. This ensures every trainable token carries equal weight.
  • Cross-Trajectory Generalized Advantage Estimation (GAE): To fix the credit assignment problem across compaction boundaries, the authors apply a trajectory-position correction. The local advantage of an earlier segment is discounted by $(\gamma\lambda)^{N_{>s}}$, where $N_{>s}$ is the number of optimized tokens appearing in all subsequent segments of that rollout. This accurately preserves the token's true temporal distance to the final outcome.

Datasets

  • Training: The models were trained using open-source agent trajectories from SWE-Dev.
  • Evaluation: Performance was benchmarked using a random 200-instance subset of SWE-bench Verified (software engineering problems) and the full set of Terminal-Bench 2.0 (command-line interactions).

Key Insights and Ideas

  • Summarization is a Core Policy Action: The authors show that changing only the model generating the summary leads to massive swings in final task success (up to 6.5 absolute points on SWE-bench). This proves that compaction is a critical decision-making process—what the agent chooses to remember directly dictates its future capabilities.
  • RL Changes Summary Dynamics: Interestingly, when summary generation is optimized via RL, the summaries actually become longer and more detailed over time. This contrasts with heuristic compressors that optimize for brevity. Under RL, the agent learns that retaining explicit technical details (like exact file paths or specific error logs) is vital for downstream task completion.

Key Results

CompactionRL was evaluated across two model scales and delivered consistent performance gains over baseline models using static inference-time compaction:

Figure 1: Left: Context compaction allows execution to continue under a fixed context window by initiating a new trace from a compressed summary once the context budget is exhausted. Without compaction, execution terminates upon exhaustion of the context budget. Right: CompactionRL consistently outperforms baseline models on SWE-bench Verified and Terminal-Bench 2.0 under compaction evaluation at both GLM-4.7-Flash(30B-A3B) and GLM-4.5-Air(106B-A30B) scales.
  • GLM-4.5-Air (106B-A30B): Achieved a 66.8% Pass@1 on SWE-bench Verified (+7.0 absolute gain) and 24.5% on Terminal-Bench 2.0 (+3.1 absolute gain).
  • GLM-4.7-Flash (30B-A3B): Reached 56.0% Pass@1 on SWE-bench Verified (+5.5 gain) and 20.2% on Terminal-Bench 2.0 (+6.8 gain).

Due to its success, CompactionRL has been integrated into the production RL pipeline for training the frontier open model, GLM-5.2 (750B-A40B).

Limitations and Future Work

Despite its strong performance, CompactionRL exhibits a train-test mismatch: the performance gains disappear if compaction is disabled during inference, meaning the model becomes dependent on its compacted history format. Additionally, the cross-trajectory GAE remains an approximation and may not fully capture highly complex, long-term dependencies spanning across multiple compaction boundaries. Finally, the evaluation was heavily focused on code-centric environments; validating this approach on broader agent domains with diverse observation spaces remains an open area for future research.

Summary updated

BibTeX
@misc{li2026compactionrl,
  author = {Yujiang Li and Zhenyu Hou and Yi Jing and Jie Tang and Yuxiao Dong},
  title = {{CompactionRL: Reinforcement Learning with Context Compaction for Long-Horizon Agents}},
  year = {2026},
  eprint = {2607.05378},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.05378},
}
Figure