Large codebases present a unique challenge for coding agents. Despite advances in AI, agents often fail to deliver meaningful results when working on complex software systems. The root of this failure is not a deficiency in the model’s reasoning capabilities, but rather a failure in the infrastructure that connects the agent to the codebase.
Why coding agents fail in large codebases
A recent analysis of 1,281 agent runs across 40+ open source repositories reveals five consistent failure patterns. These are not signs of inadequate intelligence, but symptoms of flawed infrastructure. The study focuses on real-world tasks, such as tracing how a resource allocation system works in Kubernetes. The agent must analyze 1.4 million lines of Go code spread across 22,000 files.
It begins by grepping for a key component. Hundreds of results appear. The agent reads files, follows imports, and navigates deeper. But it never converges. After 6,000 seconds—over an hour and a half—it produces no output.
When the same task is given with advanced code search tools—semantic search, keyword search, and find-references—the agent completes the analysis in just 89 seconds. It scores -than 0.90 out of 1.0. This dramatic improvement underscores a critical insight: the difference between complete failure and near-perfect completion is not in the agent’s intelligence, but in the efficiency with which it accesses context.
Where the data comes from
The findings are based on CodeScaleBench, a benchmark evaluating software engineering tasks across large open source repositories. It spans nine programming languages and includes 1,281 scored agent runs. This volume of data helps distinguish between random noise and systematic failure modes. The results show clear patterns tied to specific infrastructure gaps.
CodeScaleBench was designed to simulate real-world software engineering scenarios. It evaluates agents on tasks such as refactoring, debugging, and system tracing. Each task is grounded in actual codebases, with inputs and outputs derived from real code structures. The benchmark includes both single-repository and multi-repository tasks, allowing researchers to assess how agent performance degrades when code spans organizational boundaries.
The data collection process involved recording every tool call, file access, and reasoning step. This scoring system ensures that performance is not just about generating output, but about producing accurate, actionable results that maintain the integrity of the codebase.
The five failure patterns
Lost in the codebase
This is the most common failure. The agent explores endlessly, following file imports, but never finds a path to a solution. Each file references three more, creating an exponential search tree. In a codebase with 22,000 files, this strategy leads to unbounded exploration.
CodeScaleBench shows a clear correlation with codebase size. Agents using only local tools—such as grep and file reading—begin to struggle systematically when codebases exceed 400,000 lines of code. This threshold is not arbitrary. It aligns with the point at which the number of potential file references begins to outpace the agent’s ability to prune irrelevant paths.
When code search tools are added, the reward delta increases from −0.080 to +0.259 between 400K and 2M lines of code. This improvement is not due to better reasoning—it’s due to better search. The agent can now skip irrelevant files and directly access relevant packages through indexed search. This shift allows the agent to avoid combinatorial explosion and focus on high-impact paths.
Wrong file, wrong symbol
Agents find code matching search terms but select the wrong one. In Kubernetes, a search for ‘allocate’ returns 47 matches. Many of these are in test files, deprecated code, or utility functions. The actual allocation logic is buried among them.

Lexical search is the most used tool—7,993 calls in the dataset. But it lacks structural awareness. It cannot distinguish a symbol’s definition from its call sites. For example, a function named ‘allocate’ may appear in dozens of files, each with a different role. Without structural context, the agent has no way to determine which instance is the one being referenced.
Research in software engineering shows that common naming conventions—such as ‘handler.go’ or ‘__init__.py’—create a high density of superficial matches. These files often contain placeholder or utility code that does not represent the actual implementation. The agent’s inability to differentiate between these elements leads to incorrect navigation and faulty conclusions.
The fix lies in structural navigation tools: go-to-definition, find-references, and type hierarchy resolution. These tools leverage the compiler’s understanding of code structure to identify which code is a definition, which is a call site, and which belongs to a specific type or interface. This structural awareness allows the agent to prioritize relevant code and avoid false positives.
Partial completion
The agent finds some relevant files but misses others. In a refactoring task in the Strata finance library, the baseline agent modified only 2 of 7 affected files. It scored 0.32. The agent with Sourcegraph MCP found all files and completed the refactoring, scoring 0.80.
Partial refactoring can leave code in an inconsistent state. The API contract at changed sites may no longer match unchanged call sites. This is especially dangerous in financial systems where data integrity and transaction consistency are paramount.
The effect is worse in multi-repository tasks. The F1 score improvement is +0.209, compared to +0.085 in single-repo tasks. This shows that as codebases grow in complexity and span multiple repositories, the probability of missing relevant files drops significantly. Without specialized retrieval systems, agents are likely to miss critical dependencies or configuration files that span organizational boundaries.
One example from the dataset shows a refactoring task where the agent missed a configuration file that was shared across three repositories. The change in one module required updates in a shared service, but the agent failed to detect this dependency. This kind of oversight can lead to runtime errors or security vulnerabilities in production systems.
Tool thrashing
The agent makes repeated, inefficient tool calls. In one task, it made 96 tool calls over 84 minutes—six complete reversals of approach. It scored 0.32. The agent with structured search tools used only five targeted calls in 4.4 minutes and scored 0.68.
Tool thrashing is not a sign of poor reasoning. Each search is reasonable, but the tools lack structural relevance detection. Without a way to distinguish between relevant and irrelevant code, the agent cycles through multiple approaches, wasting tokens and time.
Agents with structured search tools are 30% cheaper ($0.51 per task vs. $0.73 baseline) and 38% faster on average. This cost and time reduction comes almost entirely from eliminating redundant tool calls. The savings are not just financial—they represent a significant improvement in operational efficiency and developer productivity.
Each backtrack leaves irrelevant content in the conversation history. This wastes context and reduces the agent’s ability to produce accurate output. In long-running tasks, this residue can accumulate, making it harder for the agent to maintain focus or recall prior decisions.
Context overflow
The agent reads entire files, even when only a few lines are relevant. This dilutes the signal in its context. On some tasks, agents with code search tools took longer than those with only local tools. They used search to find more code, then read it all.
Long-context language models have been shown to struggle with accessing information in the middle of long inputs. When irrelevant code is included, the most important information may be out of reach. This is a well-documented cognitive limitation in large language models.
The rest is boilerplate or unrelated functionality. Without a mechanism to limit context, the model must process hundreds of lines to find the relevant portion, which degrades performance.
The solution is not a larger context window. It is smarter selection—using tooling to choose only what is needed. This requires task-type-aware retrieval systems that understand the nature of the task and dynamically adjust the amount and type of code retrieved.
These are not intelligence problems
A common assumption is that future models will solve these issues. But the data shows otherwise.
For example, in ‘lost in the codebase,’ the agent’s reasoning is correct. It reads files and follows imports. The problem is not in the model’s logic—it’s in the search space. Even a smarter model would face the same combinatorial explosion. The solution lies in indexing and retrieval, not in model capability.
Similarly, in tool thrashing, each search is reasonable. But without structural relevance, the agent keeps trying different paths. A smarter model would still be limited by the tools available. This distinction matters for how you invest engineering effort. If you believe the problem is model capability, you wait for the next model release. If you understand it as infrastructure, you incorporate code search systems, structured indexes, and retrieval pipelines. The latter approach produces compounding returns: the infrastructure you build for today’s agents makes tomorrow’s agents better, too.
What to build
Each failure mode points to a specific infrastructure solution.
- Lost in the codebase and wrong file, wrong symbol: Invest in code search and indexing. Structural navigation tools are essential. These tools provide the compiler-level understanding needed to distinguish definitions from call sites and prioritize relevant code.
- Partial completion: Build retrieval coverage systems. Hybrid retrieval pipelines that combine keyword, semantic, and structural search maximize the probability of finding all affected files, not just the obvious ones. These systems should be adaptive, adjusting based on task type and repository structure.
- Tool thrashing and context overflow: Implement context management. Task-type-aware retrieval that matches strategy to task structure, with empirical thresholds for when more context helps versus hurts. This includes mechanisms to stop reading files once a relevant section is found and to prune irrelevant content from the context stream.
The key insight is this: reliable coding agents are not built by choosing the right model. They are built by solving engineering problems between the model and the codebase. Code search, structured indexes, retrieval pipelines, and proper evaluation infrastructure are what turn a reasoning-capable model into a reliable agent.
For organizations, this means a shift in priorities. Instead of waiting for next-gen models, they must invest in tooling that enables effective code understanding. This includes not just access to code, but the ability to navigate, analyze, and reason about it efficiently and accurately.
Sourcegraph offers a platform designed for enterprise code understanding. It supports the infrastructure needed to build robust coding agents, including real-time code search, structural navigation, and task-aware retrieval systems.
Sources & further reading
Featured image: Why coding agents fail in large codebases (and what to do about it). Via original publisher. Image source

