- The paper introduces ContextSniper, which efficiently reduces token usage and API cost while retaining critical patch evidence.
- It employs a dual-family memory hierarchy and hybrid top-k retrieval for precise evidence selection across multi-file codebases.
- Experimental results show significant token and cost reduction with no negative impact on patch resolution quality.
ContextSniper: Token-Efficient Memory and Evidence Selection for Repository-Level Program Repair
Motivation and Problem Setting
Repository-level program repair tasks require LLM agents to identify, localize, and repair faults in large, multi-file codebases by leveraging outputs from code and search tools, test execution, and runtime artifacts. However, standard agent workflows for these tasks exhibit a severe context inefficiency: file reads, broad searches, and verbose command outputs accumulate a substantial quantity of contextually irrelevant or redundant information in the model input, which impacts both token cost and inference quality. This context bloat leads to increased latency, higher API expenses, andโdue to information dilutionโmakes it less likely for the agent to utilize the most relevant signals for effective patch generation and validation.
Naive context reduction (e.g., truncation or generic summarization) is not viable because it risks deleting highly localized signals (such as code line numbers, symbol definitions, or runtime error traces) that are pivotal for generating correct repository-level patches. Consequently, there is a strong requirement for a system that can retain task-relevant, provenance-rich evidence, while systematically minimizing irrelevant or duplicated context tokens.
ContextSniper System Overview and Architecture
ContextSniper is introduced as a middleware memory and retrieval module within the broader AntTrail agentic memory framework for repository-level program repair. It is architected to operate between the host agentโs reasoning loop and the raw repository/tool interface, gating and transforming evidence before it enters the agentโs context window.
The system comprises several tightly coupled components:
- Dual-family Memory Hierarchy: Code memory and action memory are backed by AGFS (Agent File System) and indexed at three levelsโL0 (compact abstract/sniped view), L1 (structured routing metadata and overview), and L2 (full, source-grounded content and tool output).
- Memory-Repository Synchronization: Evidence in memory is kept strictly aligned with the current state of the repository and updated contemporaneously as the agent edits or reads files, executes shell commands, or receives new tool outputs.
- Hybrid, Adaptive Top-k Retrieval: For any evidence request (natural language or keyword query), the system fuses BM25 lexical, semantic embedding, symbol, and code-graph scores (via weighted reciprocal rank fusion) to select a minimal set of high-relevance source snippets.
- Intention-Aware Context Gate: Raw tool outputs and file reads are filtered via a task-intentโsensitive gate that selects local definitions, relevant lines, error traces, and validation signals while omitting boilerplate, unrelated regions, or superfluous logs.
- Host-Agent Boundary Adapters: ContextSniper exposes a narrow plugin boundary, leaving all high-level agent planning and patch reasoning untouched; its only constraint is to mediate contextual evidence exposure.
This insertion-layer workflow fundamentally changes the evidence supply chain for the agent. Instead of accumulating full files and raw logs, the system delivers a succinct, high-signal evidence packet for each prompt, with all original sources recoverable for backtracking or expansion.
Figure 1: ContextSniperโs insertion layer replaces broad, noisy context accumulation with memory-backed, hybrid-retrieval and evidence sniping, delivering minimal context packets to the agent.
Experimental Results
ContextSniper is evaluated on SWE-bench Lite, using two host agents: OpenClaw (open-source) and Claude Code (production-focused). The evaluation protocol matches 50 tasks per host-agent baseline and ContextSniper configuration, measuring total token usage, API usage cost, tool/action frequency, and submitted patch validation rates.
Key empirical findings:
- Token reduction: ContextSniper achieved a 51.5% reduction in total tokens and 36.4% cost reduction for OpenClaw, and a 38.9% and 27.3% reduction respectively for Claude Code, with nearly identical patch resolution success rates in both agent settings.
- Action efficiency: The system reduced the number of context-acquisition actions (file reads/searches/outputs) required per task, further contributing to end-to-end cost and latency improvements.
- Competitive with advanced memory layers: In a controlled five-task subset, ContextSniper outperformed existing memory- and RAG-based agent memory toolkits on token efficiency, with no degradation in patch quality.
These results validate the central claim that precision evidence selection and context sniping yield major efficiency gains without quality loss, contradicting the intuition that longer or more complete contexts unconditionally aid agent reasoning.
Architecture Decomposition
The effectiveness of ContextSniper is attributable to several design elements:
- Layered, auditable memory views: All filtered evidence retains precise file paths, line numbers, and source-origin links, permitting agent auditability and lossless snapshot recovery.
- Dynamic adaptation to task context: The adaptive retrieval pipeline adjusts ranking weights, candidate breadth, and evidence type (symbolic, textual, graph) according to query intent, improving localization and diagnostic signal surfacing.
- Synchronization guarantees: Repository-edit events trigger immediate updates to in-memory evidence, eliminating context drift between agent prompts and file system reality.
- Recovery-aware context gating: When critical content risk exists, or views are aggressively shortened, the agent is provided with recovery hints and backtracking capability to broaden the context on demand.
Practical and Theoretical Implications
ContextSniper demonstrates that integrating memory-mediation and context gating at the evidence boundary can shift the efficiencyโquality Pareto front for LLM agents in code reasoning and repair. It enables the following:
- Substantial reduction in inference cost (and thus economic viability for large-scale or production deployment) by minimizing context explosion during exploration, editing, and validation.
- Greater determinism and transparency in patch generation workflows, with reduced susceptibility to irrelevant context distraction or prompt pollution.
- Host-agnostic extensibility: Because it does not interact with or constrain agent planning, it can be retrofitted to a diversity of modern code agents, supporting both open-source and commercial workflows.
- Foundation for advanced, cross-task memory: The current focus is on task-local, up-to-date evidence, but the framework architecture can accommodate integration of historical or experience-based expertise from broader project or cross-repository memory systems.
Theoretically, this paradigm clarifies the benefit of explicit separation between code memory and prompt construction, emphasizing selective, provenance-rich evidence flow rather than indiscriminate context aggregation. These findings challenge the view that increasing model context window size is a direct or sufficient remedy to the bottlenecks of agentic repository repair.
Future Directions
- Improved hybrid retrieval and evidence fusion: Enhanced selection via richer program analysis, graph-augmented retrieval, and embedding models may further minimize search misses and enable subtask-specific evidence delivery.
- Adaptive or learning-based sniping policies: Automated adjustment of sniping thresholds based on ongoing task progress, uncertainty signals, or explicit agent requests.
- Integration with cross-task and experience-based memory: Broader unification with lifelong learning and experience replay systems for software repair agents.
- Human-in-the-loop debuggability: Interfaces and logs for human practitioners to audit and adjust retrieved and filtered context in post-mortem or iterative repair loops.
Conclusion
ContextSniper establishes a robust, efficient code memory and context-layer for repository-level program repair agents, with demonstrated, agent-agnostic reductions in token and inference cost and no measurable negative impact on repair quality. Its architecture and results motivate an infrastructural focus on evidence-directed memory management as a critical, orthogonal axis to LLM and agent development for long-horizon software engineering tasks (2607.01916).