acceptodds
Under review as a conference paper at ICLR 2027

AutoCompact: Learning When to Compact Context in Long-Horizon Coding Agents

Abstract

Coding agents solve repository-level software engineering tasks through long trajectories of code inspection, search, editing, and testing. As a task progresses, earlier exploration becomes stale, so managing context is more than avoiding overflow: an agent must decide when to compact, what working state to preserve, and how to continue from it. We introduce AutoCompact, which trains a coding agent to make these decisions as part of its policy. To collect training data, we run the base agent on coding tasks and use a judge to review its compaction decisions, summaries, and actions after compaction. Flawed outputs are replaced with corrected ones before being executed in the environment, so each trajectory continues from the corrected decisions. We use these trajectories for supervised fine-tuning, then jointly optimize coding and compaction through reinforcement learning with task-success rewards. Experiments on SWE-bench Verified and SWE-PolyBench Verified show that AutoCompact improves pass rates over the base model by an absolute 9.2% and 5.0%, respectively. The improvements hold across all evaluated inference budgets, with a 256K context window that never overflows and with a 16K window whose overflow triggers fallback compaction.

Primary area:
foundation or frontier models, including LLMs
Keywords:
Large Language Model, Coding Agents, Context Compaction, Context Management, Reinforcement Learning

Reviews

Among:
Shape:
1234this paper, Bottom 27%
Figure 1. Mean review rating of the reviewed papers at ICLR 2027. This paper averages 2.00 over 3 reviews, above 27% of the others.
Reviewer_gwWbOpenReview ↗
Rating
2
Confidence
4
Informativeness
2
Presentation
3
Soundness
2
Read review
Paper summary

This paper introduces AutoCompact, a method that trains coding agents to proactively manage long interaction histories by learning when to compact the context, what information to retain, and how to continue after compaction. The method first uses judge-corrected trajectories for supervised fine-tuning and then applies outcome-based RL to jointly optimize task solving and context management. Experiments on SWE-bench Verified and SWE-PolyBench Verified show that AutoCompact outperforms both the base model and existing length-triggered or proactive compaction baselines.

Critical strengths
  1. A clear move beyond passive, threshold-based compaction.
    AutoCompact allows the agent to decide when to compact based on task progress, rather than waiting until the context window is close to full. This makes context management part of the learned policy instead of a fixed heuristic.

  2. The method considers the full compaction process rather than only summary generation.
    AutoCompact jointly learns when to compact, what information to preserve, and how to continue after compaction. In particular, explicitly modeling post-compaction behavior makes the context-management process more complete than methods that focus mainly on generating compressed summaries.

Critical weaknesses
  1. Mismatch between the RL training horizon and the evaluation context length, with insufficient analysis of the learned compaction timing policy.
    RL rollouts are capped at 32K tokens, while the main long-context evaluation uses a 256K context window. It is unclear how a compaction policy learned from substantially shorter trajectories generalizes to this much longer regime. More importantly, the paper does not characterize when the learned policy actually invokes compact() under the 256K setting. For example, it would be useful to know at what context length or interaction step the first and subsequent compactions typically occur, and how many compactions are performed per task. The reported 58.5% proactive-compaction rate is only a task-level binary statistic and does not sufficiently characterize the learned “when to compact” policy.

  2. Limited analysis of the RL training horizon and multi-segment credit assignment.
    The RL setup fixes rollouts to a 32K-token cap, but the paper does not study whether different training horizons, such as 64K, would lead to different performance or compaction behavior. In addition, when a trajectory is split into multiple segments by compaction, all segments share the same trajectory-level advantage. The paper does not investigate alternative credit-assignment schemes, such as segment-dependent weighting or temporal decay. Given that compaction fundamentally changes the trajectory structure, it would be useful to understand how sensitive the method is to these design choices.

  3. The efficiency analysis relies heavily on dollar-based inference budgets without reporting the underlying resource usage.
    The reported inference cost depends on a particular pricing scheme and prefix-caching assumption, making it difficult to understand where the efficiency gains of AutoCompact actually come from. It would be important to additionally report direct statistics such as the average total token usage per task and the average number of environment steps or interaction rounds per task, comparing AutoCompact with the corresponding no-compaction variant. These statistics would clarify whether compaction improves efficiency mainly by reducing token consumption, enabling more interaction steps under the same budget, or allowing the agent to solve tasks in fewer steps.

  4. The contribution of compaction at the SFT stage is not fully disentangled from the effect of SFT itself.
    The paper reports the performance of the base model and AutoCompact-SFT, but AutoCompact-SFT is evaluated with the learned compaction mechanism enabled. Therefore, this comparison does not reveal how much of the improvement comes from SFT improving the underlying agent and how much comes from executing compaction during inference. A useful additional control would be to evaluate the same AutoCompact-SFT checkpoint with compaction disabled, analogous to the no-compaction ablation performed for the final RL checkpoint.

Questions for authors

Could the authors provide some end-to-end trajectory-level comparisons between AutoCompact and the corresponding no-compaction variant on the same tasks? For each case, it would be helpful to show when compact() is invoked, the context length at that point, what information is retained in the summary, how the subsequent actions differ from the no-compaction trajectory, the final task outcome, and the total token usage and number of interaction steps. In particular, cases where AutoCompact succeeds while the no-compaction variant fails, or achieves the same outcome with substantially fewer tokens or steps, would make the practical benefit of proactive compaction much clearer.

3 comments
AuthorsOpenReview ↗

Rebuttal by the Authors (1/2)

Thank you for your valuable feedback and questions. Below, we respond to the comments in Weaknesses (W) and Questions (Q).


W1. Training–evaluation horizon mismatch and compaction timing.

We agree that the task-level rate does not fully characterize when the policy compacts. We have added the following statistics on the 256K evaluation trajectories:

StatisticMean
Interaction round at first compaction62
Context length at first compaction26.2K tokens
Interaction round at second compaction94
Context length at second compaction9.3K tokens
Compactions per task1.44

On the 32K training–evaluation mismatch, existing results cover both larger and smaller context-window settings:

  • 256K (much longer). No trajectory reaches the forced-compaction threshold, so every compaction is the model's own decision. AutoCompact still outperforms AutoCompact-SFT at all budgets (Fig. 3b).
  • 16K (shorter). AutoCompact outperforms Base at all budgets (Fig. 3c). With the same checkpoint, proactive compaction with a 16K fallback reaches 38.0%, versus 36.0% when compacting only at the 16K threshold.

This is consistent with our design: the model can compact at task transitions before reaching the context limit. Such transitions also occur within 32K rollouts, and these results suggest that the learned behavior remains effective under different context-window settings.


W2. Training horizon and multi-segment credit assignment.

Training horizon. In preliminary experiments, increasing the RL training context from 32K to 128K did not improve the pass rate. Instead, the policy learned longer interactions: model calls and generated tokens roughly doubled, and it hit the context limit more often. Together with the 16K and 256K results above, this indicates that 32K is sufficient to learn effective compaction at a substantially lower rollout cost.

Credit assignment. We deliberately use standard outcome-reward GRPO/RLOO, in which all model-generated tokens of a trajectory share its advantage. Segmenting is only an implementation detail required because compact() rewrites the context prefix. Each token's log-probability is computed under the context in which it was generated, so the gradient is exactly standard GRPO over the full trajectory.

This standard setting is already sufficient: without any compaction-specific credit assignment or reward shaping, RL improves the pass rate by 7.4 points over SFT. It also learns better compaction, with proactive use rising from 44.3% to 58.5% of tasks and key-state omissions falling from 3.1% to 0.2%.

Designing new credit-assignment schemes is a separate research problem and not the contribution of this paper, so we did not explore it. Our contribution is learning when to compact, what to preserve, and how to continue. Improved credit assignment is orthogonal and could be combined with AutoCompact in future work.


W3. The efficiency analysis relies heavily on dollar-based inference budgets without reporting the underlying resource usage.

Thank you for the suggestion. To separate resource efficiency from token pricing and prefix-caching assumptions, we additionally report raw token usage and interaction rounds at approximately matched performance. At comparable pass rates (38.0% for AutoCompact and 37.7% for no compaction), AutoCompact uses 0.795M raw tokens and 53.7 interaction rounds per task, compared with 1.845M tokens and 79.4 rounds for no compaction. AutoCompact therefore requires 56.9% fewer raw tokens and 32.4% fewer interaction rounds to reach approximately the same success rate. Raw token usage sums the complete input and output of every model call, including summary generation, without applying any prefix-caching discount. These results show that AutoCompact improves the pass-rate–resource trade-off independently of the pricing scheme.

AuthorsOpenReview ↗

Rebuttal by the Authors (2/2)

W4. The contribution of compaction at the SFT stage is not fully disentangled from the effect of SFT itself.

Thank you for the suggestion. We have extended the same-checkpoint ablation in Section 3.3 to AutoCompact-SFT. The table below reports SWE-bench Verified pass rates at an inference budget of $4.00 per task.

CheckpointCompaction enabledCompaction disabled
AutoCompact-SFT32.2%30.4%
AutoCompact (SFT + RL)39.6%37.7%

Executing compaction improves the pass rate by 1.8% after SFT and 1.9 % after RL. These results support an additional contribution from compaction execution at both training stages.


Q1. Can the authors provide end-to-end trajectory comparisons with and without compaction?

Thank you for the suggestion. We have added two paired trajectory comparisons using the same checkpoint, tasks, verifier, execution limits, and decoding settings.

Case 1 (scikit-learn__scikit-learn-14053). AutoCompact first compacts after step 13, with 16,183 context tokens. The summary preserves the _tree.TREE_UNDEFINED sentinel, the target function, and the next edit. The agent then applies and verifies the fix. Without compaction, it repeatedly attempts ineffective replacements, leaves the source unchanged, and incorrectly declares completion; the target test fails.

Case 2 (psf__requests-1142). AutoCompact compacts after step 13, with 16,091 context tokens, preserving the root cause in prepare_body() and the condition excluding GET and HEAD. It finishes after four focused checks. Both variants succeed, but the no-compaction trajectory contains repeated edits and overlapping checks. AutoCompact reduces total tokens by 90.13% and environment steps by 77.03%.

CaseVariantOutcomeTotal tokensSteps
1AutoCompactSolved292,21430
1No compactionFailed1,981,42581
2AutoCompactSolved171,70717
2No compactionSolved1,740,20774

Token totals include cumulative input and output tokens, including summary generation.

Thank you for the additional results. With the same checkpoint, allowing proactive compaction alongside the 16K fallback raises the pass rate from 36.0% to 38.0%. This is a modest gain. The token savings reported in the rebuttal come from the comparison against no compaction, so the actual inference cost of the two policies in the 16K comparison remains unclear.

I appreciate the direct correction of the agent’s actions after compaction. Given prior work on learned context management [1, 2] and the size of the reported gains, I see this as an incremental contribution. I stand by my original rating of 2.

References

[1] Gao, S., et al. (2026). SWE-MeM: Learning Adaptive Memory Management for Long-Horizon Coding Agents. arXiv:2606.28434.

[2] Sun, W., et al. (2025). Scaling Long-Horizon LLM Agent via Context-Folding. arXiv:2510.11967.

Reviewer_VyVYOpenReview ↗
Rating
2
Confidence
3
Informativeness
2
Presentation
2
Soundness
2
Read review
Paper summary

I. Research Content This article studies the context management issues faced by long-term code agent when solving warehouse-level complex tasks, and proposes an active context compression method named AutoCompact based on supervised fine-tuning and reinforcement learning to address problems such as historical information lag and unreasonable compression timing that occur after the agent's context grows. The "context compression" in this paper is modeled as an active decision-making action for the agent. Three main aspects are used to enhance the model's capabilities.

  1. Determine whether the current task has reached a interim result and is suitable for compression.
  2. Generate "Working-state summary", retaining core findings, code status, and remaining actions.
  3. Based on the compressed summary, derive the correct execution action for the next step. II. Main Contributions This paper breaks away from the previous passive truncation logic based on context length threshold and introduces an active compact action, allowing the agent to incorporate context management as part of its task execution strategy. The judgment model corrects the agent's behavior trajectory from three dimensions: triggering timing, quality of the working-state summary, and the coherence of subsequent actions, generating high-quality SFT training data. Then, GRPO is adopted to optimize code writing and context compression in an end-to-end manner under the same reward signal. III. Verification Method The base model uses Qwen3-Coder-30B-A3B-Instruct, and the dataset uses SWE-bench Verified and SWE-PolyBench Verified. The baseline is compared with: full historical record,fixed compression, and existing active compression methods. Through the through rate curve drawn under different API inference budgets, it is proved that this method can achieve the highest benefit in both low and high budgets. Three ablation studies are set: SFT and RL; proving that SFT establishes the basic capability, while RL further improves the model's cost-effectiveness and upper limit. Normal execution and ignoring summaries: verifying that "executing the compression action itself" is more effective than "directly discarding history and continuing to run" in terms of task improvement. Different context mechanisms: tested under two settings of 256K and 16K, proving that this method is effective in both scenarios.
Critical strengths

The context management has been upgraded from an external engineering fix to an internal learnable strategy within the intelligent agent.

Critical weaknesses

The current experiments in the paper are not sufficient to fully distinguish the contributions of the active compression strategy and the general improvement in coding ability to the final performance. The existing ablation studies have not sufficiently separated the individual effects of compression, additional encoding supervision, and RL, and cannot fully and directly prove that the model has indeed learned the appropriate compression timing.

Questions for authors

Can an experimental comparison be made to distinguish the contributions of the active compression strategy and the general improvement in Coding ability (resulting from SFT and GRPO optimization)? Can a direct assessment of the compression timing be provided to prove that the model's decision is superior to simple rules and that the model has indeed learned the appropriate compression timing?

2 comments
AuthorsOpenReview ↗

Rebuttal by Authors

Thank you for your valuable feedback and questions. Below, we respond to the comments in Weaknesses (W) and Questions (Q).


W1(1)&Q1(1). Separating compaction gains from additional coding training.

Thank you for the suggestion. Our ablation in Section 3.3 measures the benefit of executing compaction with a fixed checkpoint. Following your suggestion, we additionally trained a full-history baseline using the same base model, scaffold, and task data, with comparable judge assistance and matched SFT and RL compute budgets. This baseline retains the full interaction history during both training and evaluation.

The table below reports SWE-bench Verified pass rates in the original 256K setting at $4.00 per task.

MethodSFTSFT + RL
Full-history baseline30.1%36.9%
AutoCompact32.2%39.6%

These results suggest that learned compaction provides gains beyond those from additional coding training.


W1(2)&Q1(2). Assessing compaction timing.

Building on our 16K evaluation in Section 3.3, we additionally evaluated a fixed-threshold variant using the same final AutoCompact checkpoint. This variant disables proactive compaction and compacts only when the context reaches 16K tokens. We compare it with normal AutoCompact execution, which allows proactive compaction and retains the same 16K fallback. Both conditions use the same summary-generation procedure, scaffold, and inference budget.

The following results are on SWE-bench Verified at $4.00 per task.

Trigger strategyPass rate
Fixed threshold (16K)36.0%
AutoCompact with 16K fallback38.0%

These results suggest that allowing the model to choose earlier compaction points is more effective than relying solely on the 16K threshold.

I think the first question has largely been answered. With matched training budgets, AutoCompact outperforms the full-history baseline after both SFT and SFT + RL. Together with the original comparison where the same model either executes or skips compaction, these results convince me that compaction itself accounts for some of the gains.

My remaining concern is about timing. With the same model and inference budget, the pass rate is 38.0% when proactive compaction is allowed and 36.0% when compaction is triggered only at 16K tokens. This shows that proactive compaction works better in this setting. What remains unclear to me is whether the model uses task progress to choose good moments to compact, or whether the gain simply comes from compacting earlier and possibly more often. These two results alone do not distinguish between those explanations.

I am convinced that compaction helps, but I remain unsure whether the model has learned to choose when to compact based on how the task is progressing. I therefore maintain my original rating.

Reviewer_8BvVOpenReview ↗
Rating
2
Confidence
3
Informativeness
2
Presentation
3
Soundness
2
Read review
Paper summary

This paper presents AutoCompact, which trains coding agents to decide when to compact context, what information to preserve, and how to continue afterwards. It uses judge-corrected trajectories for supervised fine-tuning, followed by reinforcement learning. The results show that the proposed AutoCompact significantly improves sucesss rate on two benchmarks.

Critical strengths

The paper addresses an important aspect of context management

Very good results: AutoCompact improves pass rates by 9.2 and 5.0 percentage points on two benchmarks

Critical weaknesses

It is unclear how much of the improvement comes from learning to compact context and how much comes from additional coding training. The trained model achieves 37.7% on SWE-bench Verified even with compaction disabled, compared with 39.6% when enabled. To separate these effects, the authors should compare AutoCompact with an agent that receives comparable supervision and training compute but retains the full interaction history.

Questions for authors

Does AutoCompact outperform a full-history coding agent trained with the same task data, comparable judge assistance, and the same SFT/RL compute budget?

1 comment
AuthorsOpenReview ↗

Rebuttal by Authors

Thank you for your valuable feedback and questions. Below, we respond to the comments in Weaknesses (W) and Questions (Q).


W1&Q1. A matched full-history training baseline is missing.

Thank you for the suggestion. Our ablation in Section 3.3 measures the benefit of executing compaction with a fixed checkpoint. Following your suggestion, we additionally trained a full-history baseline using the same base model, scaffold, and task data, with comparable judge assistance and matched SFT and RL compute budgets. Unlike the inference-time ablation above, this baseline retains the full interaction history during both training and evaluation.

The table below reports SWE-bench Verified pass rates at the highest evaluated inference budget ($4.00 per task), after SFT and after SFT + RL.

MethodSFTSFT + RL
Full-history baseline30.1%36.9%
AutoCompact32.2%39.6%

These results suggest that learned compaction provides gains beyond those from additional coding training.

From OpenReview, CC BY 4.0. Authors as signed there; shown unedited.

Related papers

Loading the map…

Discussion (0)

Sign in to comment.