CLIFFCOMPACTION: Cost-Efficient Compaction for Long-Horizon Coding Agents
Summary (Overview)
- CLIFFCOMPACTION is a rule-based, training-free autocompaction technique for coding agents that reduces inference cost by up to 50% while maintaining or improving performance on coding benchmarks (SWE-bench Verified, Terminal-Bench) and achieving state-of-the-art results on KernelBench for continual learning.
- The method's key design principle is high compaction precision over recall: it truncates or drops content verbatim (never rephrasing or summarizing), and each compaction discards previous compacted history entirely, preventing context drift from accumulating across compaction events.
- CLIFFCOMPACTION makes test-time scaling economically viable: three compacted Kimi K2.6 rollouts on Terminal-Bench 2.0 match Opus 4.7 and exceed Opus 4.6 and GPT-5.3 Codex at lower cost, gaining +10.5 points at only 1.9× the cost of a single uncompacted run.
- For continual learning on KernelBench Level 3, CLIFFCOMPACTION achieves CUDA kernel speedups of 2.23× after 200 steps and 3.58× after 400 steps, surpassing specialized search algorithms and trained agents despite being a general-purpose technique.
- The authors open-source a scaffold-agnostic API-proxy implementation usable with Claude Code, Codex, and other harnesses.
Introduction and Theoretical Foundation
Background and Motivation
Coding agents solve complex tasks requiring contexts spanning millions of tokens, creating two fundamental challenges:
- Context management: How to handle information once an agent session exceeds its context window
- Efficiency: How to maximize performance while reducing the computational cost of long contexts
The authors identify that the KV-cache is the primary bottleneck for long-context sessions, being both memory-intensive and bandwidth-bound. While backend solutions like FlashAttention reduce KV-cache memory directly, frontend token-based algorithms can reduce costs indirectly by manipulating context content.
Key Theoretical Concepts
Compaction Precision vs. Recall: The paper introduces a crucial trade-off:
- Compaction precision: How faithfully preserved information matches the original (avoiding omission/distortion)
- Compaction recall: How much information remains retrievable across many compactions
LLM-based summarization suffers from low precision because summaries of summaries compound losses, leading to subtle context drift. CLIFFCOMPACTION optimizes precision at the cost of recall, based on the finding that "removing stale context can sometimes improve agent performance rather than simply reducing memory pressure."
Residual Propagation: Though CLIFFCOMPACTION discards old context, information propagates forward implicitly:
Each live session is generated with compacted history in view, so the agent's actions carry implicit influence of discarded context.
Methodology
The CLIFFCOMPACTION Algorithm
The core algorithm operates as follows. After the -th compaction, the context consists of compacted history followed by live session :
Critical design choices:
- Token threshold triggering: Context grows naturally until exceeding a preset budget, then compacts
- Verbatim retention: Keep exact excerpts, never summaries
- Flat compaction: Each new compaction discards previous compactions entirely, operating only on the live session since the last event
Compaction Rules
- Tool results: Drop those exceeding 500 characters; retain shorter ones (grep matches, exit codes, concise output)
- Tool calls: Reduce to compact signatures (tool name, target file/path, essential arguments); remove long embedded content
- Thoughts: Truncate to 300 characters
- System prompt and task description: Keep in full
- Recent turns: Keep the most recent turn pairs unchanged
Algorithm Pseudocode
Algorithm 1 CLIFFCOMPACTION
1: function CLIFFCOMPACTION(turns)
2: old, recent ← turns[:-2K], turns[-2K:] ▷ K turn pairs
3: parts ← []
4: for m ∈ old
5: if m is a prior compaction
6: skip ▷ discard, keep flat
7: else if m is ASSISTANT
8: parts += [TRUNCATE(m.thinking, 300), SIGNATURE(m.toolcall, 150)]
9: else if m is TOOLRESULT and |m| ≤ 500
10: parts += [m]
11: end if
12: end for
13: return JOIN(parts), recent
14: end function
KV-Cache Efficiency
CLIFFCOMPACTION preserves cache efficiency by never modifying context between compactions. The cache remains valid across each growth segment and is invalidated only at compaction points. Since uncached input tokens cost 5–6× more than cached ones, this design is crucial for cost efficiency.
Experimental Setup
- Benchmarks: SWE-bench Verified, Terminal-Bench 2.0/2.1, KernelBench Level 3
- Scaffolds: mini-swe-agent, OpenHands, Terminus-2, Claude Code (via API proxy)
- Models: Kimi (K2.5, K2.6, K2.7), GLM (5, 5.1, 5 Turbo, 5.3 Flash, 4.7 Flash), GPT-5-mini
- Compaction thresholds:
Empirical Validation / Results
SWE-bench Verified Performance
CLIFFCOMPACTION preserves most full-context performance at moderate thresholds:
| Model | Full Context | 32K | 16K | 8K |
|---|---|---|---|---|
| Kimi K2.6 | 73.87% | 73.27% | 71.87% | 67.60% |
| GLM 5.1 | 71.40% | 68.80% | 69.33% | 65.13% |
| GLM 5 Turbo | 69.80% | 70.53% | 67.53% | 63.27% |
Terminal-Bench Results
On Terminal-Bench 2.0, CLIFFCOMPACTION improves over full-context baselines:
- Kimi K2.6: 61.42% at both 32K and 16K thresholds vs. 59.16% full-context baseline (+2.26 points)
- At 16K, replacing Terminus-2's native LLM summarization with CLIFFCOMPACTION increases success from 55.45% to 61.42%
- GLM 5.1: Improves from 49.83% (full) to 53.20% (32K) and 54.33% (16K)
On Terminal-Bench 2.1 with Claude Code, CLIFFCOMPACTION reaches 76.69% at matched ~45K context, exceeding both Claude Code's autocompaction (70.97%) and default 200K configuration (73.03%).
Test-Time Scaling Results
CLIFFCOMPACTION makes multi-rollout scaling cost-effective:
| Configuration | k | Cost (USD) | Oracle | Practical | Gain @ Cost |
|---|---|---|---|---|---|
| Kimi K2.6 + CLIFF (16K) + SGV | 3 | $58.01 | 74.2 | 69.7 | +10.5 @ 1.9× |
| Kimi K2.6 + CLIFF (16K) + SGV | 2 | $38.67 | 70.4 | 65.9 | +6.7 @ 1.3× |
| Kimi K2.6 + SGV (no compaction) | 3 | $91.65 | 70.8 | 64.0 | +4.8 @ 3.0× |
| GPT 5.3 Codex | 1 | $64.63 | — | 64.7 | — |
| Opus 4.7 | 1 | — | — | 69.4 | — |
The SOFT GROUP VERIFICATION (SGV) selector uses a LightGBM classifier with features including line-overlap agreement features across rollouts, selecting .
Continual Learning on KernelBench Level 3
CLIFFCOMPACTION enables sustained improvement beyond context limits:
| Method | Model | Speedup | Acc. | >2× | Steps |
|---|---|---|---|---|---|
| AdaExplore (specialized) | GPT-5-mini | 1.78× | 100% | 22% | 200 |
| CUDA-Agent (specialized) | Seed 1.6 (RL) | 1.80× | 94% | — | 200 |
| No compaction (256K) | Kimi K2.7 | 1.30× | 86% | 50% | 200 |
| CLIFFCOMPACTION (128K) | Kimi K2.7 | 2.23× | 94% | 54% | 200 |
| CLIFFCOMPACTION (128K) | Kimi K2.7 | 3.58× | 96% | 86% | 400 |
Without compaction, 98% of runs terminate early (median 99 steps) due to context exhaustion, capping speedup at 1.30×. CLIFFCOMPACTION outperforms specialized kernel-optimization systems by 25% despite being general-purpose.
Comparison with Existing Compaction Strategies
| Method | SWE-bench (Kimi) | Cost Δ | KernelBench Speedup | KernelBench Cost |
|---|---|---|---|---|
| Sliding window | 72.80% | +9% | 2.86× | $21.52 |
| Summarization | 70.27% | -23% | 3.47× | $8.10 |
| Summarization + Microcompaction | 71.00% | -17% | 3.33× | $12.84 |
| CLIFFCOMPACTION | 71.33% | -21% | 3.58× | $8.32 |
CLIFFCOMPACTION is the only method that stays competitive on quality while remaining cheap on every benchmark.
Theoretical and Practical Implications
Theoretical Contributions
-
Precision-recall framework for compaction: The paper formalizes the trade-off between compaction precision (faithfulness) and recall (information retention), arguing that precision is more critical for agent performance than previously recognized.
-
Residual knowledge propagation: Demonstrates that discarding context entirely doesn't lose all information—the agent's subsequent behavior carries implicit influence of prior context, enabling effective continual learning over millions of tokens.
-
Cache-aware algorithm design: Highlights the importance of minimizing re-prefill events, showing that infrequent but substantial compactions are more cost-effective than frequent small ones.
Practical Implications
- Cost reduction: Up to 50% cost reduction on Terminal-Bench and 90% reduction in cache-read costs
- Democratized test-time scaling: Makes multi-rollout scaling affordable, allowing smaller models to compete with frontier models at lower cost
- Scaffold-agnostic deployment: The API-proxy implementation works with closed-source agents like Claude Code without modification
- Continual learning without specialized training: A general-purpose compaction technique outperforms task-specific kernel optimization systems
Conclusion
CLIFFCOMPACTION demonstrates that a simple, rule-based compaction strategy can outperform sophisticated summarization and specialized systems across coding benchmarks. By prioritizing compaction precision (verbatim retention) over recall, and by discarding previous compactions to prevent context drift, the method maintains performance while dramatically reducing costs.
The authors identify several limitations: benefits depend on scaffold complexity (fixed components consume budget), and the method is most valuable for medium-to-long-horizon tasks. Future work could explore comparisons with trained context-management approaches and external memory systems.
Key takeaway: CLIFFCOMPACTION shows that "less context can be more"—removing stale information not only saves money but can improve agent performance, making it a practical and effective technique for long-horizon coding agents.
Related papers
- SWE-Review: Closing the Loop on Issue Resolution with Agentic Code Review
SWE-Review closes the loop on AI-generated pull requests with agentic code review, boosting resolve rates from 27.5% to 56.9% and enabling efficient test-time scaling.
- VibeMemBench: Evaluating Memory Systems for Coding Agents on Real Repository Coding Tasks
Existing memory systems fail to improve coding agent performance because raw transcript volume, not instruction semantics, degrades retrieved fix patterns in 69.3% of failures.
- Agent Approval Laundering: Transitive Effects Beyond the Approved Invocation
Approval laundering occurs when durable approval records omit effects from transitive workflows, and no record-only policy can guarantee correct decisions when identical visible fields require different effect-specific actions.