TL;DR
Not all context is useful
AutoCompact starts from a simple but important observation: if we separate out the key steps that directly contribute to completing the task, much of the remaining work is exploratory. During localization, for example, the agent may inspect several files, compare plausible locations, and test different hypotheses before identifying where the change should be made. These steps are often necessary in the current phase of the task, even if they do not directly appear in the final solution.
But once the agent moves to the next phase of the task, those details may no longer help. What matters is the conclusion it produced, not every intermediate step that led there. Keeping the full exploration in context adds noise that can interfere with the model's subsequent reasoning; for example, it may cause the model to revisit hypotheses it has already ruled out.
The agent must recognize when prior history is no longer needed and compact it without losing the working state, and all relevant information necessary to successfully complete current and future tasks. This is not just condensing old content. The model needs to keep the working state needed for the next step, for example, which paths have been ruled out, which files have been changed, and what needs to be verified next.
AutoCompact teaches the model to replace the full trajectory with a clean, compact context, allowing it to continue into the next phase of the task with the information it needs.
AutoCompact: Adaptive context compaction
Existing context-compaction methods typically use a fixed threshold: once the context length exceeds a fixed threshold, earlier interactions are replaced with a summary. For example, OpenAI uses this approach to train its models and deploy them in ChatGPT and Codex. AutoCompact retains the same summarize-and-continue structure, but lets the model adaptively call compact() to compact the context when the task state—not context length—indicates that compaction would be useful.
summary
summary
summary
summary
Figure 1. Fixed-threshold compaction summarizes only at a context limit. AutoCompact keeps the same summarize-and-continue structure, but lets the model proactively choose earlier, task-aware compaction points.
The figure above (Figure 1) shows the difference between AutoCompact and fixed-threshold compaction. The figure below (Figure 2) then provides a more detailed, interactive walkthrough of how AutoCompact compacts the context.
compact()
Localization is complete. Earlier exploration is no longer needed.
Figure 2. When the model decides that the current context should be cleaned up, it calls compact() to trigger compaction. After that, the model only sees the initial task, the most recent turns, and the compacted # Auto Context Summary, instead of the full history.
Most of the time, the model still works like a normal agent: it reads files, edits code, and runs verification. At an appropriate transition between phases of the task, such as after localizing the issue and before beginning the edit, the model can proactively invoke compact(). When compact() is invoked, the model compacts the current context into a # Auto Context Summary. The summary preserves the working state needed for the next phase of the task, while intermediate exploration details are removed from the active context. The model then continues with "original task + recent turns + summary."
Learning AutoCompact from judge-guided on-policy rollouts
AutoCompact requires the model to learn three capabilities: when to invoke compact(), what information to preserve in the summary, and how to continue from the compacted context. Existing models do not reliably exhibit these behaviors. Even when explicitly instructed how and when to use compact(), they rarely invoke it proactively, possibly because such behavior is uncommon in their training data. As shown in Figure 3, we therefore introduce a judge during data collection. Given only the history visible at the current step and explicit knowledge of compact(), the judge evaluates and, when necessary, revises the base model’s action, summary, or continuation. This turns autonomous management of the full compaction process into step-level evaluation and correction under explicit criteria, which can be performed more reliably by human annotators or a well-prompted LLM.
More specifically, if the proposed action is already appropriate, the judge leaves it unchanged and advances to the next step. When the judge determines that compaction is appropriate, it can instead replace the model’s proposed action with compact(). If the resulting # Auto Context Summary omits or misrepresents state needed for the next phase of the task, the judge revises the summary. It can also correct the continuation if the model fails to use the compacted context, such as by repeating exploration that has already been resolved or losing track of conclusions preserved in the summary.
The judge does not rewrite the trajectory arbitrarily. It follows an annotation protocol that specifies valid compaction points, the state each summary should preserve, and the corrections that may be applied. SFT is then performed on the resulting corrected trajectories. At inference time, the judge is removed, and the trained model decides when to invoke compact() and continues from the compacted context on its own. Figure 3 shows how we construct the supervision signals needed to teach the model to use AutoCompact.
compact()Context compactedExecutedcompact(), what to preserve, and how to continue.Figure 3. The left column shows the base model’s proposed actions. The Judge keeps valid proposals or replaces invalid ones before execution. Actions 3 and 5 on the left are not executed; the environment instead executes the corrected actions on the right, including compact() and the localized edit.
Using 379 SWE-rebench tasks and GPT-5.5-Codex as the judge, we collect 1,052 cold-start SFT examples after filtering malformed requests, summary loops, and off-track continuations.
compact()Long context is not enough
We first evaluate AutoCompact-Qwen3 30B Coder (SFT) against the baseline model Qwen/Qwen3-Coder-30B-A3B-Instruct on SWE-bench Verified with a 256k context window. Every task finishes before reaching this threshold, so threshold-based compaction is never triggered. All models use the same scaffold and runtime settings. The baseline keeps the full trajectory, while AutoCompact may compact only when the model decides it is useful. This directly tests whether long context alone is sufficient, or whether proactively removing stale history still improves reasoning.
Figure 4. On SWE-bench Verified, AutoCompact-SFT solves more tasks than the baseline under every inference-cost budget with a 256k context window and no forced compaction. Long context alone is therefore not enough; adaptive compaction still helps.
We evaluate runs at fixed inference-cost budgets, reported in USD per task.AutoCompact-SFT consistently outperforms the baseline across every inference-cost budget. Because neither model is forced to compact in this setting, compaction can help even when the context limit is never reached. This means that a trajectory may fit within a 256k context window and still contain enough stale exploration to impair subsequent reasoning.
Jointly optimizing coding and compaction with RL
The SFT stage teaches the model to imitate judge-approved compaction behavior, but it does not directly optimize whether those decisions ultimately help solve the task.
Starting from the SFT checkpoint, we develop a fully asynchronous RL system and train the agent with GRPO on SWE-Gym. Each rollout receives a 0/1 outcome reward based on whether its final patch passes the test suite. GRPO converts this outcome into a trajectory-level advantage and applies the same advantage to every model-generated token in that trajectory—including regular coding actions, the decision to call compact(), the generated context summary, and the continuation after compaction.
Compaction is therefore not trained as a separate auxiliary task. The agent jointly learns to solve the software-engineering problem and manage its context whenever doing so improves the final task outcome. Useful compaction is reinforced when it contributes to a successful trajectory, while unnecessary or lossy compaction is discouraged when it harms task completion. We compare the SFT model with the RL model under the same 256k evaluation setting.
Figure 5. On SWE-bench Verified, under the same 256k context window with no forced compaction, RL improves over the SFT model at every inference-cost budget.
The RL model substantially outperforms the SFT model across every inference-cost budget, with the largest gains at lower inference-cost budgets. SFT establishes the compaction behavior, while RL further improves task performance.
RL not only improves task performance but also makes context management more active:
- More active compaction. After SFT, the model proactively invokes
compact()in 44.3% of tasks; after RL, this rises to 58.5%. - High-quality summaries. 99.8% retain relevant task and workspace state; 97.8% specify a next action.
- Trigger timing can still improve. Many summaries are generated only after the agent has begun testing or validating its code changes. This suggests that the model is more reliable at what to preserve than when to compact.
Fixed-threshold compaction is not enough
Finally, we evaluate on SWE-bench Verified with a much smaller 16k context window, where AutoCompact and the baseline are forcibly compacted at the same threshold. However, a fixed token threshold may not align with the task trajectory: by the time the threshold is reached, the accumulated context may be harder to summarize, or compaction may interrupt an ongoing phase of the task, such as reasoning, debugging, or editing. This comparison therefore tests whether fixed-threshold compaction is sufficient, or whether AutoCompact still benefits from choosing when to compact before the threshold. As shown in Figure 6, AutoCompact consistently outperforms the fixed-threshold baseline, supporting the intuition that compaction timing should depend on the current task state rather than context length alone.
Figure 6. On SWE-bench Verified, both models use forced compaction at the 16k threshold, but AutoCompact still performs better under equal inference-cost budgets. A fixed-threshold trigger is therefore not enough; deciding adaptively when to compact adds value.
Conclusion
AutoCompact turns context compaction from a fixed threshold-based rule into a learned agent behavior. With judge-guided SFT and simple RL, coding agents learn when to compact, what to preserve, and how to continue from the compacted state. On SWE-bench Verified, AutoCompact solves more tasks with lower inference cost than both full-history and fixed-threshold baselines.
Citation
@misc{zhang2026autocompact,
title = {AutoCompact: Learning When to Compact Context
in Long-Horizon Coding Agents},
author = {Zhang, Xuan and Zheng, Longtao and Du, Cunxiao
and An, Bo and Dong, Xin},
year = {2026},
url = {https://autocompact.github.io/}
}