Abstract
Identifying the precise locations in a codebase that require modification—known as code localization—is a fundamental challenge in automated software engineering. While Large Language Model (LLM) agents have shown promise, they often struggle with the scale and structural complexity of real-world repositories. This paper presents LocAgent, a framework that represents codebases as heterogeneous graphs to guide LLM agents through functional dependencies. By combining graph-based navigation with model distillation, LocAgent achieves state-of-the-art localization accuracy while significantly reducing operational costs compared to proprietary models.
Introduction
For developers, the process of fixing a bug or implementing a feature begins with a search: finding the specific lines of code that need to change. As codebases grow, this "localization" task becomes a massive time sink. Traditional Information Retrieval (IR) methods, such as BM25 or dense embeddings, often fail because natural language issue descriptions focus on symptoms (e.g., a crash log) rather than the underlying structural causes.
Recent attempts to use LLM agents for this task typically involve iterative directory exploration. However, these agents frequently lose their way in deep file hierarchies or fail to recognize dependencies between distant components. LocAgent addresses these issues by providing agents with a "map" of the codebase—a lightweight, heterogeneous graph that highlights functional relationships like imports and function calls.
Methodology
LocAgent bridges the gap between traditional retrieval and agentic reasoning through three core components: graph representation, specialized tools, and model distillation.
1. Graph-Based Code Representation
LocAgent parses the Abstract Syntax Tree (AST) of a repository to construct a directed heterogeneous graph. Unlike a standard file tree, this graph includes:
- Nodes: Representing directories, files, classes, and functions.
- Edges: Capturing semantic relationships such as
contain,import,invoke(function calls), andinherit.
To ensure the LLM can process this information efficiently, the framework uses Tree-based Graph Formatting. Instead of raw adjacency matrices, local subgraphs are presented to the agent as indented trees, which significantly improves the model's ability to reason about the code's topology.
2. Agentic Tools and Navigation
The agent is equipped with a specific toolkit to interact with the graph:
- SearchEntity: A hierarchical BM25 index used for initial keyword or entity discovery.
- TraverseGraph: A multi-hop tool that allows the agent to explore the graph along its edges (e.g., following a call chain from a failing function to its definition).
- RetrieveEntity: A tool to pull the actual source code once a candidate node is identified.
3. Model Distillation
To reduce reliance on expensive proprietary models, the authors generated successful localization trajectories using Claude-3.5. These trajectories were used to fine-tune open-source models—specifically Qwen-2.5-Coder (7B and 32B)—via Low-Rank Adaptation (LoRA). This process teaches smaller models the specific logic required to navigate complex code graphs effectively.
Evaluation
The framework was evaluated on two primary benchmarks:
- SWE-Bench-Lite: A standard set of 300 real-world GitHub issues.
- Loc-Bench: A new benchmark introduced by the authors containing 560 Python issues collected after October 2024. Loc-Bench was designed to prevent data leakage (as the issues post-date the training of most LLMs) and to provide a balanced mix of bug reports, feature requests, and security patches.
Key Results
LocAgent demonstrates that functional proximity is often more important than physical proximity in code navigation. Key findings include:
- High Precision: On SWE-Bench-Lite, LocAgent (using Claude-3.5) achieved 94.16% file-level accuracy (Acc@5) and 77.37% function-level accuracy (Acc@10), outperforming existing baselines like SWE-agent and MoatlessTools.
- Cost Efficiency: The fine-tuned Qwen2.5-32B model achieved 92.70% file-level accuracy—nearly matching Claude-3.5—while reducing the cost per issue from $0.66 to $0.09, an 86% reduction.
- Downstream Performance: Improved localization directly impacts the success of automated repair. Integrating LocAgent with a standard repair agent (Agentless) increased the issue resolution rate (Pass@10) from 33.58% to 37.59%.
- The Necessity of Multi-hop Reasoning: Ablation studies confirmed that restricting the agent to 1-hop traversals significantly degraded performance, proving that following deep dependency chains is essential for accurate localization.
Limitations and Future Work
Currently, LocAgent is optimized for Python codebases. Future development will focus on expanding support to other programming languages and diversifying the training data beyond Claude-generated trajectories. Additionally, the authors plan to explore the framework's utility in broader software engineering tasks, such as large-scale refactoring and automated feature addition.
Conclusion
LocAgent demonstrates that by providing LLM agents with a structured, graph-based view of code dependencies, we can achieve high-precision code localization at a fraction of the cost of current proprietary solutions. The introduction of Loc-Bench further provides a robust, leakage-free environment for evaluating the next generation of software engineering agents.
Summary updated