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.
翻译:大语言模型智能体在软件开发中展现出卓越能力,但其性能受限于冗长的交互上下文,这会导致高昂的API成本与响应延迟。尽管已有多种上下文压缩方法(如LongLLMLingua)应对此挑战,这些方法通常依赖固定指标(如PPL),忽略了代码理解的任务特异性,从而频繁破坏代码的语法逻辑结构并丢失关键实现细节。本文提出SWE-Pruner——一种专为编码智能体设计的自适应上下文剪枝框架。该框架借鉴人类程序员在开发调试过程中“选择性浏览”源代码的认知机制,对长上下文执行任务感知的自适应剪枝。针对当前任务,智能体将明确目标(例如“聚焦错误处理”)转化为提示信息以指导剪枝方向。我们训练了一个轻量级神经浏览模型(0.6B参数),使其能够根据目标动态筛选上下文中的相关代码行。在四个基准测试和多种模型上的实验表明,SWE-Pruner在各类场景中均表现优异:在SWE-Bench Verified等智能体任务上实现23-54%的令牌缩减且成功率有所提升;在LongCodeQA等单轮任务中达到最高14.84倍的压缩率,同时保持性能损失最小。