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.

GPT-5.6 Sol / Codex use fixed-threshold compaction
User prompt
Reasoning
Tool call
Tool result
Reasoning
Tool call
Tool result
Reasoning
Compressed
summary
Reasoning
Tool call
Tool result
Reasoning
Compressed
summary
Reasoning
Final answer
Compaction limit
AutoCompact lets the model decide when to compact
User prompt
Reasoning
Tool call
Tool result
Reasoning
Model calls compact()
AutoCompact
summary
Reasoning
Tool call
Tool result
Reasoning
Model calls compact()
AutoCompact
summary
Reasoning
Final answer
Fallback compaction limit

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.

Interactive context-compaction demo From long history to a clean context
Long historyExploration keeps accumulating
10k / 16k
Initial taskImplement the requested changeKEEP
Tool · 4.2k tokensLong file outputMost lines are unrelated to the fixDROP
AgentRepeated inspect / grepRevisits paths already ruled outDROP
Agent + toolFailed edit and reverted diffBased on a stale hypothesisDROP
Useful conclusionBug localized; edit plan identifiedSUMMARIZE
Recent turnsLatest command and resultKEEP
Model-generated action
compact() Localization is complete. Earlier exploration is no longer needed.
Clean contextUseful information retainedAgent is acting from compacted state
02.0k2.5k / 16k
Waiting for the model to trigger compaction
Kept verbatimInitial task
Generated working state# Auto Context SummaryObjective, conclusion, modified files, verification status, and next action
Kept verbatimRecent turns
Next agent turnApply the localized fixEdit the target file, then run focused tests
Keep verbatim Summarize Drop as outdated noise
InteractiveClick any step below to jump to that state
Step 1: exploration keeps accumulating; the context approaches capacity and becomes increasingly noisy.

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.

Interactive annotation demo What the model proposed—and what the environment executed
Base-model proposal Actions before Judge review Candidate action at each step
Action 1 · Agent proposalInspect candidate filesLocalization is still in progress
Action 2 · Agent proposalGrep related symbolsUseful exploration within the current phase of the task
Action 3 · Agent proposalInspect another fileLocalization is already complete
Action 4 · Summary# Auto Context SummaryTarget file is missing
Action 5 · Agent proposalRepeat repository searchFails to use the compacted working state
Judge review Choose the action Keep, replace, or repair
Action 1 decisionAppropriate explorationLeave the proposed action unchangedKEEP
Action 2 decisionRelevant evidence gatheringLeave the proposed action unchangedKEEP
Action 3 decisionCompaction is appropriateReplace continued exploration with compactionREPLACE
Action 4 decisionRequired state is missingAdd the target fileREPAIR
Action 5 decisionContinuation is off trackUse the summary and begin the localized editREPAIR
Environment execution Actions after Judge review These are the actions actually executed
Action 1 · CodingInspect candidate filesFile contents returnedExecuted
Action 2 · CodingGrep related symbolsSymbol matches returnedExecuted
Action 3 · Compaction actioncompact()Context compactedExecuted
Action 4 · Corrected# Auto Context SummaryState, target file, and next action
Action 5 · CodingEdit the localized fileLocalized edit appliedExecuted
EXECUTED · environment actionNOT EXECUTED · replaced proposalREPAIRED · corrected output
Saved supervisionCorrected trajectory enters SFTExecuted actions and corrected outputs
24% trigger53% summary23% continuation
After SFTThe trained agent acts without the judgeIt learns when to trigger compact(), what to preserve, and how to continue.
InteractiveClick any step below to inspect that annotation state
Step 1: actions 1 and 2 pass through Judge review and execute unchanged.

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.

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.

Long context · 256k · no forced compaction AutoCompact solves more tasks under the same inference-cost budget
Qwen3 30B CoderAutoCompact-Qwen3 30B Coder (SFT)SFT additional solved tasks
Baseline versus AutoCompact SFT on SWE-bench Verified by inference-cost budget in USD per task with a 256k context window and no forced compaction AutoCompact SFT outperforms the baseline across all shown budgets in the 256k long-context regime.

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.

RL · 256k · no forced compaction RL improves AutoCompact’s task performance at every inference-cost budget
AutoCompact-Qwen3 30B Coder (SFT)AutoCompact-Qwen3 30B Coder (SFT+RL)RL additional solved tasks
AutoCompact SFT versus AutoCompact SFT plus RL by inference-cost budget in USD per task RL substantially improves over the SFT model across all shown budgets.

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.

Limited context · 16k forced-compaction threshold Adaptive compaction outperforms fixed-threshold compaction
Qwen3 30B CoderAutoCompact-Qwen3 30B Coder (SFT+RL)Final-model additional solved tasks
Baseline versus final AutoCompact model by inference-cost budget in USD per task on SWE-bench Verified when both use a 16k forced-compaction threshold The final AutoCompact model remains stronger than the baseline when the context limit is reached more often.

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/}
}