Context Compaction Is Becoming a Training Objective

Every long agent session ends the same way. Context fills, a summary fires, and the run continues on a compressed memory of what came before. In Claude Code the trigger is /compact, or the automatic pass that runs as you approach the window limit. Operators treat the moment as friction to manage: protect the state that matters, hope the summary keeps it, move on. A cluster of recent research says that instinct is about to age. Compaction is moving from an inference-time patch you engineer around into a training-time objective the model optimizes directly. When a model is rewarded for producing trajectories that survive their own compression, context management stops being your babysitting job and starts becoming a property you select for.

TL;DR

  • Today, context compaction lives at inference time. Anthropic itself frames it as a context-engineering technique: summarize a conversation nearing the window limit and reinitialize with the summary.2 The runtime toolkit around it, /compact, auto-compaction, context editing, the memory tool, all wrap a model that does not know it is being compacted.34
  • CompactionRL trains the model to expect compaction. It jointly optimizes task execution and summary generation with reinforcement learning, so the agent learns from compacted trajectories rather than being interrupted by them.1
  • The numbers are real. On GLM-4.5-Air it reaches 66.8% Pass@1 on SWE-bench Verified, a 7.0-point gain, and 24.5% on Terminal-Bench 2.0.1 The benchmarks are contested and current, not toys.89
  • It is not one paper. Memory-R1 trains ADD/UPDATE/DELETE memory operations with RL; MemAct treats context curation as actions the policy takes and optimizes them end to end.67 Three independent groups reached for the same move.
  • The operator takeaway holds either way. Treat the compaction seam as a design surface, not an accident: decide what lives in durable memory versus transient context, guard the boundary with a PreCompact hook, and start reading a model’s trained compaction competence as a spec line, not a footnote.5

The Patch Everyone Already Manages

Context is a finite resource, and every operator running long sessions has learned to ration it. Anthropic’s own engineering writing names the failure mode precisely: as the token count climbs, recall degrades, a decay they call context rot.2 The same post defines the countermeasure in plain terms. Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitializing a new context window with the summary.2

Read that definition again and notice where the work happens. It happens around the model, at inference time, by machinery the model is not party to. Claude Code makes the machinery concrete. The /compact command frees up context by summarizing the conversation so far, and you can pass focus instructions to steer what the summary keeps.4 Auto-compaction runs the same pass automatically as context approaches the limit; it is on by default through the autoCompactEnabled setting, and you set the effective window it triggers against with CLAUDE_CODE_AUTO_COMPACT_WINDOW or turn it off with DISABLE_AUTO_COMPACT.4 On the platform side, context editing automatically clears stale tool calls and results as you approach token limits, and a file-based memory tool lets the model store information outside the window entirely.3

These are good tools. Anthropic reports that context editing alone improved a long-horizon agent evaluation by 29%, and pairing it with the memory tool cut token consumption by 84% over a hundred-turn run.3 The point is not that the runtime approach is weak. The point is what it assumes. Every one of these techniques manages context as an external property of a session, applied to a model that was trained on uncompacted trajectories and meets compaction only at deployment. The model produces a long trajectory the way it always has. Something else decides when to summarize, what to keep, and how to resume. The model then wakes up inside a compressed context it did not author and was never trained to expect.

That gap, between how the model was trained and how it is run, is the seam the new research is closing.

What CompactionRL Actually Changes

CompactionRL, from the GLM team at Zhipu, starts from the same problem statement and inverts the fix. Instead of bolting compaction onto inference, it makes compaction part of what the model is trained to do. The method jointly optimizes task execution and summary generation, using token-level loss normalization and cross-trajectory generalized advantage estimation so the agent can learn from compacted long-horizon trajectories rather than being derailed by them.1

Strip the machinery and the shift is easy to state. In the runtime approach, a summarizer sits outside the policy and the model tolerates whatever it produces. In CompactionRL, the summary is generated by the same policy that does the task, and both are rewarded together. The model is trained to write summaries it can act on and to act well on summaries it wrote. Compaction stops being an interruption and becomes a move the agent has practiced.

The results land on current benchmarks. Built on the open GLM-4.5-Air model, CompactionRL reaches 66.8% Pass@1 on SWE-bench Verified, a 7.0-point absolute gain, and 24.5% on Terminal-Bench 2.0.1 On the smaller GLM-4.7-Flash it adds 5.5 and 6.8 points on the two benchmarks.1 Both benchmarks are real and hard: SWE-bench Verified is a human-validated 500-issue subset for resolving real GitHub issues, and Terminal-Bench 2.0 is 89 human-verified command-line tasks where frontier agents still score under two thirds.89 SWE-bench Verified is worth a caveat, since OpenAI publicly stepped back from it in early 2026 over test flaws and contamination, so treat it as the most-cited agentic-coding benchmark rather than an unimpeachable one.8 The gains survive that caveat because they are within-model deltas: the same base model, trained to compact, beats itself.

The most telling line in the paper is not a benchmark. CompactionRL is deployed in the reinforcement learning pipeline for training the next open GLM model.1 Compaction has moved from a thing you do to a model into a thing a model is built with.

Not a One-Off

One paper is a result. Three independent groups reaching for the same move is a direction. Alongside CompactionRL, two other 2025 papers treat context management as something to train rather than something to wrap.

Memory-R1 equips an agent with a memory manager that learns structured operations, ADD, UPDATE, DELETE, and NOOP, through reinforcement learning, so the decision of what to remember and what to discard becomes a learned policy instead of a fixed heuristic.6 It reaches its results with only 152 training examples, which suggests the capability is closer to latent than to expensive.6 MemAct goes further into the framing this essay is about. It formulates context management as in-place editing operations, deletion and insertion, and jointly optimizes information retention and task performance through end-to-end reinforcement learning.7 Memory as action, in their words: curation of the working context is not a preprocessing step, it is part of the policy.

Read together, the three papers describe one migration. The runtime toolkit, context editing, external memory, scheduled summarization, is the current answer to a finite window. The training-time approach makes the same behaviors intrinsic to the model, learned against the reward that actually matters, which is finishing the task. When the same idea shows up in memory operations, in context editing, and in trajectory compaction within a single year, the individual papers matter less than the vector they share.

What This Means For How You Build Today

None of this ships to your harness tomorrow, and the honest operator question is what to do while it arrives. The answer is not to wait. The migration reprices the work you already do around context, and a few moves position you for the version that is coming.

Treat the compaction boundary as a design surface. Right now most operators discover their compaction behavior by accident, noticing after the fact that a summary dropped a decision from turn three. Make it deliberate. Decide up front what belongs in durable memory that survives any reset, your rules, your project conventions, the task contract, and what is transient context that a summary is allowed to compress. In Claude Code the durable layer is your CLAUDE.md and rules files, which reload after compaction through the InstructionsLoaded event, and a file-based memory store for state that must outlive the window.35 Everything else is fair game for the summarizer.

Guard the seam with a hook, not hope. Claude Code exposes a PreCompact hook that fires before compaction and can steer or block it, and a PostCompact hook for cleanup after.5 If a class of state must never be summarized away, a PreCompact hook is where you enforce it deterministically, rather than trusting a focus instruction to be honored. The discipline is the same one that makes hooks the right tool for anything that must always execute: you move a guarantee out of the prompt and into code.

Start reading trained compaction competence as a spec line. As the CompactionRL direction matures, models will differ not only in context window size but in how well they were trained to work compressed. Window size has been the headline number for two years, the 200K-versus-1M comparison that anchors every model card. That number is about to share the stage with a quieter one: how gracefully a model degrades when it has to summarize itself. When you evaluate a model for long-horizon work, put a genuinely long task in front of it, one that forces at least one compaction, and watch what survives. That behavior is becoming a property worth selecting for.

Structure long work so the seams fall in clean places. A model trained to compact still compacts better across a finished subtask than across a half-written one. Until the training catches up everywhere, you get most of the benefit for free by shaping work so natural checkpoints, a passing test, a committed change, a closed subtask, line up with where compaction is likely to fire. That is good harness design regardless, and it is exactly the structure the trained models are learning to expect.

The Position

Context window size stops being the constraint that defines harness architecture. For two years the design conversation has started from a token budget: how much fits, what to evict, when to summarize. That framing treats the model as a fixed vessel and context as a resource you pour carefully. The CompactionRL direction dissolves the vessel. When the model is trained to manage its own compression, the window stops being a hard wall you engineer against and becomes a soft gradient the model was taught to walk down.

The runtime toolkit does not disappear. Context editing, memory tools, and manual /compact remain the right controls for the parts of context management that are genuinely yours: which facts are authoritative, which files are the source of truth, what the task actually is. The migration is narrower and more interesting than full automation. It moves the mechanical half of context management, the summarize-and-resume plumbing, into the model, and leaves the editorial half, deciding what matters, with you. Context engineering bifurcates into what the model handles and what you still own, and the boundary between them is the new place where good harness design lives.

The tell is already in the CompactionRL paper. Compaction earned a place in the training pipeline of a frontier model, next to the reward signals for coding and reasoning. Capabilities that reach the training loop do not usually leave it. The operators who win the next year are the ones who stop treating compaction as an accident to survive and start treating it as a contract to design.

Key Takeaways

  • Compaction is migrating from inference time to training time. CompactionRL, Memory-R1, and MemAct independently use reinforcement learning to make context management a learned behavior rather than an external wrapper.167
  • The runtime tools are the current answer, not the final one. /compact, auto-compaction, context editing, and the memory tool manage context around a model that was not trained to expect compression.34
  • Separate durable memory from transient context, deliberately. Put rules, conventions, and the task contract in the layer that survives a reset; let everything else be compressible.35
  • Enforce the boundary with a PreCompact hook. Move any must-not-summarize guarantee out of a focus instruction and into deterministic code.5
  • Read compaction competence as a spec line. Test a candidate model on a task long enough to force a compaction, and watch what survives. Window size is no longer the only number that matters.

FAQ

What is context compaction?

Compaction is summarizing a conversation that is nearing the context window limit and reinitializing a new window from the summary, so a long-running agent can continue past the point where the raw history would overflow.2 It trades verbatim history for a compressed representation that fits.

Does Claude Code compact context automatically?

Yes. Auto-compaction is on by default and runs as context approaches the limit. You can steer the effective window with CLAUDE_CODE_AUTO_COMPACT_WINDOW, disable it with DISABLE_AUTO_COMPACT, or trigger a pass manually with /compact, optionally passing focus instructions for the summary.4

What is CompactionRL?

A reinforcement learning method from the GLM team that trains long-horizon agents to work with compaction by jointly optimizing task execution and summary generation, so the model learns from compacted trajectories. It improves GLM-4.5-Air by 7.0 points on SWE-bench Verified and is deployed in the training pipeline for the next GLM model.1

Can you train a model to manage its own context?

That is exactly what the recent research demonstrates. Memory-R1 trains explicit memory operations with RL, MemAct treats context editing as policy actions, and CompactionRL trains trajectory compaction directly. All three make context management intrinsic to the model rather than a runtime add-on.167

Does training a model to compact make context windows irrelevant?

No, but it changes what the number means. A larger window still helps, yet a model trained to compact well can go further within any window than one that meets compaction only at deployment. Trained compaction competence becomes a second axis alongside raw window size.

What should I do about agent context compaction now?

Design the compaction seam instead of discovering it. Decide what lives in durable memory versus transient context, guard the boundary with a PreCompact hook, structure long tasks so compaction falls across finished subtasks, and evaluate new models on tasks long enough to force a summarization.5

Sources


  1. Yujiang Li, Zhenyu Hou, Yi Jing, Jie Tang, Yuxiao Dong. “CompactionRL: Reinforcement Learning with Context Compaction for Long-Horizon Agents.” arXiv:2607.05378, July 2026. https://arxiv.org/abs/2607.05378. Reports Pass@1 of 66.8% on SWE-bench Verified (+7.0) and 24.5% on Terminal-Bench 2.0 for GLM-4.5-Air, and +5.5 / +6.8 for GLM-4.7-Flash; states the method is deployed in the RL pipeline for training GLM-5.2. 

  2. Prithvi Rajasekaran, Ethan Dixon, Carly Ryan, Jeremy Hadfield. “Effective context engineering for AI agents.” Anthropic Engineering, September 29, 2025. https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents. Defines compaction and the “context rot” degradation, and frames context engineering as curating the optimal set of tokens during inference. 

  3. “Managing context on the Claude Developer Platform.” Anthropic, September 29, 2025. https://claude.com/blog/context-management. Context editing “automatically clears stale tool calls and results” near token limits; the memory tool stores information in a file-based system outside the context window. Reports a 29% improvement from context editing alone, 39% combined with memory, and 84% token reduction on a 100-turn web-search evaluation. 

  4. Claude Code documentation: Commands, Settings, and Environment variables. https://code.claude.com/docs/en/commands, https://code.claude.com/docs/en/settings, https://code.claude.com/docs/en/env-vars. /compact [instructions] summarizes and continues; autoCompactEnabled (default true) governs automatic compaction; CLAUDE_CODE_AUTO_COMPACT_WINDOW sets the token window used for the trigger; DISABLE_AUTO_COMPACT turns it off. 

  5. Claude Code Hooks reference. https://code.claude.com/docs/en/hooks. PreCompact fires before compaction and supports a blocking decision; PostCompact fires after; InstructionsLoaded fires when CLAUDE.md or rules files load, including after compaction (matcher value compact). 

  6. Sikuan Yan, Xiufeng Yang, Zuchao Huang, et al. “Memory-R1: Enhancing Large Language Model Agents to Manage and Utilize Memories via Reinforcement Learning.” arXiv:2508.19828, August 2025. https://arxiv.org/abs/2508.19828. Trains a Memory Manager to learn ADD, UPDATE, DELETE, and NOOP operations via RL, using only 152 training examples. 

  7. Yuxiang Zhang, Jiangming Shu, Ye Ma, Xueyuan Lin, Shangxi Wu, Jitao Sang. “Memory as Action: Autonomous Context Curation for Long-Horizon Agentic Tasks.” arXiv:2510.12635, October 2025. https://arxiv.org/abs/2510.12635. Formulates context management as in-place editing operations optimized end to end with reinforcement learning. 

  8. “Introducing SWE-bench Verified.” OpenAI, August 13, 2024. https://openai.com/index/introducing-swe-bench-verified/. A human-validated 500-instance subset of SWE-bench for resolving real GitHub issues. Note: OpenAI stepped back from the benchmark as a frontier metric in February 2026 over test flaws and contamination (https://openai.com/index/why-we-no-longer-evaluate-swe-bench-verified/), so it is best read as the most-cited agentic-coding benchmark rather than a definitive one. 

  9. Terminal-Bench. Stanford and the Laude Institute. https://www.tbench.ai/. Terminal-Bench 2.0 is 89 human-verified command-line tasks spanning software engineering, ML, security, and data science; frontier agents score under roughly two thirds. 

Articles connexes

Context Compaction Is a Decision, Not a Threshold

Coding agents compact context when a counter trips, not at a safe stopping point. A 2026 paper shows model-decided compa…

10 min de lecture

Reward the Tool Before the Answer

AI agents fail when answers claim tool work that never happened. Four failure modes and the rule that catches them, with…

13 min de lecture

Your Agent Writes Faster Than You Can Read

Five research groups published about the same problem this week: AI agents produce code faster than developers can under…

17 min de lecture