Table of Contents
Fetching ...

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

SWE-Pruner tackles the context wall in coding agents by introducing task-aware, line-level pruning that preserves syntactic structure. A lightweight 0.6B neural skimmer, guided by explicit Goal Hints, selectively retains relevant lines within code contexts, using a CRF-based pruning head and a reranking head for coherent filtering. Trained on 61,184 synthetic, task-diverse samples, SWE-Pruner achieves substantial token reductions (23–54% on multi-turn tasks; up to 14.84x on single-turn tasks) with minimal performance impact, while reducing interaction rounds and API costs. Integrated as middleware between agents and the environment, it generalizes across backbones and benchmarks, offering practical efficiency improvements for real-world coding workflows.

Abstract

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 and up to 14.84x compression on single-turn tasks like LongCodeQA with minimal performance impact.

SWE-Pruner: Self-Adaptive Context Pruning for Coding Agents

TL;DR

SWE-Pruner tackles the context wall in coding agents by introducing task-aware, line-level pruning that preserves syntactic structure. A lightweight 0.6B neural skimmer, guided by explicit Goal Hints, selectively retains relevant lines within code contexts, using a CRF-based pruning head and a reranking head for coherent filtering. Trained on 61,184 synthetic, task-diverse samples, SWE-Pruner achieves substantial token reductions (23–54% on multi-turn tasks; up to 14.84x on single-turn tasks) with minimal performance impact, while reducing interaction rounds and API costs. Integrated as middleware between agents and the environment, it generalizes across backbones and benchmarks, offering practical efficiency improvements for real-world coding workflows.

Abstract

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 and up to 14.84x compression on single-turn tasks like LongCodeQA with minimal performance impact.
Paper Structure (51 sections, 7 equations, 7 figures, 10 tables, 1 algorithm)

This paper contains 51 sections, 7 equations, 7 figures, 10 tables, 1 algorithm.

Figures (7)

  • Figure 1: Efficiency analysis on SWE-Bench Verified. SWE-Pruner (orange) achieves substantial reductions on Token Cost and Agent Rounds for the base Mini SWE Agent (gray) for both Claude Sonnet 4.5 and GLM 4.6.
  • Figure 2: Token cost distribution over different tool calls for Mini-SWE-Agent on SWE-Bench Verified with Claude Sonnet 4.5. Read operations dominate token consumption at 76.1%, motivating the need for context pruning mechanisms.
  • Figure 3: Overview of SWE-Pruner. Left: The Interaction Workflow demonstrates how SWE-Pruner functions as a middleware between the Coding Agent and the Environment. It intercepts the Raw Context from file operations and delivers a Pruned Context to the agent. Right: The Pruning Pipeline details the internal mechanism. Based on a specific goal hint from the coding agent, the neural skimmer processes the raw context through line-level scoring and adaptive selection to deliver the pruned context.
  • Figure 4: First token latency comparison across different sequence lengths. SWE-Pruner maintains consistently low latency below 100 ms.
  • Figure 5: Token cost distribution over different tool calls for Mini-SWE-Agent with GLM-4.6 on SWE-Bench Verified. Read operations dominate token consumption at 67.5%, further validating the necessity of context pruning mechanisms across different backbone models.
  • ...and 2 more figures