Source-linked AI summary
SWE-Pruner: Self-Adaptive Context Pruning for Coding Agents
Yuhang Wang, Yuling Shi, Mo Yang, Rongrui Zhang, Shilin He, Heng Lian, Yuting Chen, Siyu Ye, Kai Cai, Xiaodong Gu
TL;DR
Coding agents face costly, noisy long contexts, while existing compression methods are poorly aligned with code structure and evolving task goals. SWE-Pruner uses goal-conditioned, line-level neural skimming to select relevant context, achieving substantial compression across agent and single-turn benchmarks while maintaining or improving performance. Its scope is currently centered on Python repositories, with multilingual support left for future work and marginal latency overhead remaining.
Problem
Long coding-agent contexts incur high costs and noise, while existing compression methods can damage code structure and use static, task-agnostic criteria.
Method
SWE-Pruner uses explicit natural-language goal hints and a lightweight 0.6B neural skimmer for adaptive, line-level selection of relevant context.
Results
Across agent and single-turn benchmarks, SWE-Pruner reduces tokens by 23–54% and reaches up to 14.84× compression while maintaining or improving task performance.
Takeaways & Limitations
Line-level, context-aware pruning addresses context-window constraints across agent workflows and general code-understanding tasks.
Takeaways & Limitations
The implementation focuses on Python repositories, multilingual support remains future work, and the skimmer adds marginal latency overhead.
Abstract
from arXiv · showhide
LLM agents have demonstrated remarkable capabilities in software development, but their performance is hampered by long interaction contexts, which incur high API costs and latency. While various context compression approaches such as LongLLMLingua have emerged to tackle this challenge, they typically rely on fixed metrics such as PPL, ignoring the task-specific nature of code understanding. As a result, they frequently disrupt syntactic and logical structure and fail to retain critical implementation details. In this paper, we propose SWE-Pruner, a self-adaptive context pruning framework tailored for coding agents. Drawing inspiration from how human programmers "selectively skim" source code during development and debugging, SWE-Pruner performs task-aware adaptive pruning for long contexts. Given the current task, the agent formulates an explicit goal (e.g., "focus on error handling") as a hint to guide the pruning targets. A lightweight neural skimmer (0.6B parameters) is trained to dynamically select relevant lines from the surrounding context given the goal. Evaluations across four benchmarks and multiple models validate SWE-Pruner's effectiveness in various scenarios, achieving 23-54% token reduction on agent tasks like SWE-Bench Verified while even improving success rates, and up to 14.84x compression on single-turn tasks like LongCodeQA with minimal performance impact.
1 Introduction
SWE-Pruner addresses context-window costs and code-specific compression failures with task-aware, line-level pruning guided by explicit goals. Across agent and single-turn benchmarks, it reduces context substantially while maintaining or improving performance.
- Long repository contexts create prohibitive inference costs, attention dilution, and hallucinations for coding agents.
- Existing compression methods can compromise code syntax, discard debugging-critical character information, and apply static, task-agnostic criteria.
- SWE-Pruner uses explicit natural-language goals and a 0.6B model to select relevant lines while preserving syntactic and structural integrity.
- Across evaluated benchmarks, SWE-Pruner delivers substantial efficiency gains while maintaining or improving task performance, including a 39% reduction on SWE-Bench Verified with Claude Sonnet 4.5.
- The framework performs task-aware, line-level pruning to alleviate the context wall problem.
- 23–54% token reduction is achieved on agent tasks, while single-turn tasks reach up to 14.8× compression with minimal performance impact.
2 Motivation
Coding agents spend much of their token budget repeatedly reading and accumulating repository context. This motivates context-aware pruning that removes redundant material while retaining information relevant to the agent’s current goal.
- 76.1% of total tokens are consumed by read operations, exceeding execute and edit operations combined.
- Repeated exploratory file operations stream coarse-grained context into multi-round interactions, where earlier retrieved code accumulates.
- The resulting optimization opportunity requires filtering that identifies relevant information according to the agent’s current goal.
3 Approach
SWE-Pruner is a task-aware middleware framework that uses agent-generated goal hints and a lightweight neural skimmer to retain relevant code lines while preserving structure. It integrates adaptive line-level pruning into coding agents with minimal workflow changes.
- Overview: SWE-Pruner intercepts raw file-operation context and delivers a goal-guided pruned context between the coding agent and its environment.The pruning pipeline scores context at line level and adaptively selects content before returning it to the agent.
- Goal Hint Generation: Agents provide optional self-contained goal hints that describe their current information need and guide query-relevant filtering.The wrapper supports a context_focus_question parameter; omitting it bypasses pruning and preserves backward compatibility.
- Lightweight Neural Skimmer: The neural skimmer formulates pruning as reranking, scoring each token using the goal and full code context before aggregating scores to lines.Line scores average constituent token scores so relevance is evaluated across the whole line rather than being dominated by a few tokens.
- Lightweight Neural Skimmer: A 0.6B-parameter Qwen3-Reranker backbone retains lines whose aggregated scores exceed threshold τ and processes retrieved chunks in parallel.The lightweight architecture is intended to keep pruning overhead negligible relative to downstream token savings.
- Training: Training combines CRF-based sequential retention decisions with document-level reranking through a weighted objective.The training corpus uses teacher-generated task-oriented queries, line masks, and relevance scores, with quality filtering producing 61,184 examples.
- Agent Integration: The deployed framework dynamically regenerates goal hints across multi-turn tasks, while single-turn tasks use their inherent descriptions as initial hints.This lets the pruning focus shift as the agent’s reasoning evolves.
4 Experiments
SWE-Pruner is evaluated across four benchmarks covering single-turn code understanding and multi-turn coding-agent scenarios. The experiments compare it with multiple compression and retrieval baselines using task-performance and efficiency metrics.
- Benchmarks: Four benchmarks cover Long Code Completion, Long Code QA, SWE-Bench Verified, and SWE-QA across single-turn and multi-turn settings.The evaluation includes 500-example completion data, Long Code QA contexts up to 1M tokens, 500 real-world SWE-Bench issues, and repository-specific SWE-QA.
- Evaluation Settings: Single-turn tasks are evaluated under 4x and 8x compression constraints, while multi-turn tasks use Mini SWE Agent or OpenHands with Claude Sonnet 4.5 and GLM-4.6.The agent configurations differ by benchmark: Mini SWE Agent is used for SWE-Bench Verified and OpenHands for SWE-QA.
- Baselines: Baselines include LLMLingua-2, Selective-Context, embedding-based RAG with UniXCoder, LongCodeZip, Full Context, and No Context.The compression methods represent token-level, self-information, retrieval-based, and program-structure-based approaches.
- Metrics: Task performance is measured with ES and EM for completion, Accuracy for question answering, Resolve Rate for SWE-Bench Verified, and LLM-as-a-Judge scores for SWE-QA.Efficiency is measured by compression ratio, token consumption, interaction rounds, and API cost.
5 Results
Across multi-turn and single-turn benchmarks, SWE-Pruner reduces context usage while preserving or improving task performance, and its lightweight skimmer adds low latency.
- Multi-Turn Tasks: 23–38% token reduction on SWE-Bench Verified accompanies 1.2–1.4 percentage-point success-rate improvements across models.Interaction rounds also decrease by 18–26%.
- Multi-Turn Tasks: 29–54% token reduction on SWE-QA is achieved across Streamlink, Reflex, and Conan with minimal impact on task performance.GLM-4.6 uses 29–41% more rounds after pruning, while overall token consumption remains substantially lower.
- Baseline Comparison: 64% success on a 50-sample SWE-Bench subset exceeds the 62% vanilla-agent baseline while using the lowest token usage among compared strategies.LLM Summarize reaches 56% and incurs additional latency.
- Single-Turn Tasks: SWE-Pruner generalizes its query-aware, line-level pruning mechanism from coding agents to single-turn long-context tasks.The evaluation covers Long Code Completion and Long Code QA under 4x and 8x compression constraints.
- Single-Turn Tasks: 10.92x effective compression under the 8x constraint on Long Code Completion preserves 57.58 Edit Similarity and 31.0 Exact Match.At 4x, SWE-Pruner achieves 5.56x compression with 58.63 Edit Similarity and 31.5 Exact Match.
- Efficiency Impact: Below 100 ms first-token latency is maintained across sequence lengths, including 8K tokens, using a 0.6B encoder skimmer.Qwen3-32B exceeds 1200 ms under the comparison described.
6 Related Works
Related work addresses prompt compression and agent context management through token pruning, retrieval, summarization, reinforcement learning, and hierarchical or proactive history policies.
- Prompt Compression: Prompt compression methods include token-level pruning, embedding or retrieval-based approaches, and repo-level retrieval for code completion.Examples include LLMLingua, Selective Context, AttentionRAG, RAG, XRAG, and repository-level retrieval.
- Agent Context Management: Modern coding agents still experience context-window insufficiency and documented performance degradation on long contexts despite windows of 128k tokens or more.This motivates dedicated agent context-management methods.
- Agent Context Management: Long-horizon context management methods learn to manage interaction histories or introduce hierarchical oversight and proactive folding policies.SWE-Pruner instead operates as lightweight middleware at the agent–environment boundary.
7 Conclusion
SWE-Pruner combines structure-preserving, task-aware pruning with adaptive filtering to reduce token usage across agentic and general code-understanding tasks while maintaining or improving efficiency and performance.
- Conclusion: 23–38% token reduction on SWE-Bench accompanies 1.2–1.4 percentage-point success-rate improvements.The framework also achieves 29–54% reduction on SWE-QA and up to 14.84x compression on single-turn benchmarks.
- Conclusion: Line-level, context-aware pruning addresses context-window constraints across agentic workflows and general code-understanding tasks.The approach is implemented through lightweight binary classifiers and query-conditioned thresholding.
Limitations
The authors identify Python-focused implementation, ongoing data-leakage evaluation, and marginal skimmer latency as limitations and areas for future work.
- Scope: The implementation focuses on Python repositories, while comprehensive multilingual support remains future work.The authors state that the approach does not rely on Python-specific features and generalizes across different codebases.
- Evaluation: Continuous evaluation on newly released repositories remains important despite selecting SWE-QA repositories collected after the training data.This repository-selection strategy is described as a mitigation for data leakage.
- Efficiency: The lightweight neural skimmer introduces marginal latency overhead that could be further reduced through distillation or early-exit mechanisms.
A EMPIRICAL RESULTS ON GLM MODEL
GLM-4.6 shows that codebase exploration dominates agent token consumption, supporting the model-agnostic need for context pruning.
- Token consumption: 67.5% of GLM-4.6’s total tokens are consumed by read-type operations, totaling 2.89M tokens.Edit and execute operations consume 18.5% and 14.0%, respectively.
- Cross-model comparison: GLM-4.6 exhibits token-consumption patterns remarkably similar to Claude Sonnet 4.5.
- Implication: The dominance of codebase exploration remains consistent across different model architectures, training methodologies, and parameter scales.The paper presents this consistency as evidence that context-pruning needs are model-agnostic.
B MODEL ARCHITECTURE AND INFERENCE DETAILS B.1.0.0
SWE-Pruner combines a lightweight reranker-based skimmer with structured line-level pruning, task-specific training data, and thresholded inference.
- Architecture: SWE-Pruner uses a Qwen3-Reranker-0.6B backbone with multi-layer feature fusion and specialized pruning and reranking heads.The pruning head uses a CRF for line-level filtering, while the reranking head produces document-level relevance scores.
- Architecture: The CRF pruning head models retain/prune decisions as a structured sequence-labeling problem to encourage coherent pruning patterns.Its emissions represent local confidence and transitions capture dependencies between adjacent decisions.
- Inference: During inference, token scores are averaged by line and lines are retained when their average score exceeds τ = 0.5.Viterbi decoding selects the optimal label sequence before threshold-based retention produces the kept lines.
- Training data: The task taxonomy covers diverse coding scenarios including summarization, refactoring, optimization, explanation, debugging, feature addition, and completion.The taxonomy is designed to represent common information needs in software-development workflows.
- Training data: The training corpus contains 61,184 verified samples generated from 200,000 code snippets across 195,370 files and 5,945 repositories.Queries and line-level retention masks are synthesized with a teacher LLM and filtered for annotation quality.
E Agent Rounds and Token Consumption Analysis
SWE-Pruner reduces both token consumption and interaction rounds for Claude Sonnet 4.5 and GLM-4.6, with larger reductions for GLM-4.6.
- Claude Sonnet 4.5: 23.1% fewer tokens and 18.2% fewer rounds are achieved for Claude Sonnet 4.5.Average tokens decrease from 0.911M to 0.701M, while rounds decrease from 51.0 to 41.7.
- GLM 4.6: 38.3% fewer tokens and 25.7% fewer rounds are achieved for GLM 4.6.Average tokens decrease from 0.791M to 0.488M, while rounds decrease from 49.3 to 36.6.
- Token breakdown: 44.2% and 44.0% reductions occur in GLM 4.6 prompt and completion tokens, compared with 38.7% and 40.8% for Claude Sonnet 4.5.The distribution shifts are reported in Figure 7.
- Interaction rounds: 34.6% fewer agent rounds occur with GLM 4.6, compared with 18.3% fewer rounds with Claude Sonnet 4.5.The paper links the GLM reduction to a shift from 49.3 to 36.6 rounds.
F Detailed Efficiency Analysis
SWE-Pruner provides low-latency pruning and generalizes across models, achieving substantial compression while retaining strong performance on long-code tasks.
- Latency: 102.00ms TTFT at 8192 tokens is achieved by SWE-Pruner, versus 1188.67ms for Qwen3-32B and 529.45ms for Qwen3-14B.This corresponds to 7.5× and 5.2× speedups, respectively.
- Latency: 2.1× latency growth from 2048 to 8192 tokens is observed for SWE-Pruner, compared with 14.1× for Qwen3-32B.The table reports stable, low latency across sequence lengths.
- Deployment efficiency: 23–54% token reductions combine with 18.3–25.7% fewer interaction rounds to improve end-to-end efficiency across agent trajectories.The paper describes these gains as compounding despite the upfront pruning cost.
- Single-turn tasks: 14.68x compression and 55.75% accuracy are achieved on Long Code QA under the 8x constraint with Seed-Coder-8B-Instruct.This exceeds the reported RAG and LongCodeZip results on that benchmark and constraint.
- Single-turn tasks: 8.13x compression, 56.73 ES, and 28.5 EM are achieved on Long Code Completion under the 8x constraint.The reported result exceeds Selective-Context and LongCodeZip on the listed metrics.
H Syntactic Structure Preservation Analysis
SWE-Pruner’s line-level pruning preserves code structure better than token-level compression while reducing context and improving agent efficiency in software-engineering tasks.
- Syntactic structure preservation: Line-level pruning retains semantically relevant lines with minimal syntactic context, avoiding the arbitrary token deletions that disrupt AST structure.
- Syntactic structure preservation: 87.3% AST correctness on Function RAG demonstrates that SWE-Pruner preserves syntactic validity better than token-level compression methods.LLMLingua2 and Selective Context achieve 0.29% and 12.4% AST correctness, respectively.
- Agent efficiency: 83.3% token reduction on django__django-10554 coincides with successful completion by Pruner after the Baseline exhausts resource limits.The Pruner completes the task in 56 steps with 1.17 million tokens, versus more than 7 million tokens and 164 steps for the Baseline.
- Agent efficiency: 30.2% reduction in peak prompt length on django__django-11740 shows that pruning improves efficiency even when both agents succeed.The Pruner uses 6.0% fewer tokens despite taking 48 steps versus 42 for the Baseline.
- Agent behavior: Focused context changes exploration behavior: the Pruner reads targeted files once and avoids redundant exploratory reads and temporary validation artifacts.The Baseline accumulates historical noise through segmented reads and auxiliary scripts, while the Pruner directly edits the relevant section.