Every coding agent needs to understand your codebase. The question is how to give AI agents codebase context that actually works. The two dominant approaches -generic RAG (Retrieval-Augmented Generation) and structured codebase indexing -produce very different results, and most teams are using the wrong one.
RAG was designed for documents. Code is not a document. That mismatch explains why the AI agent context window is too small for useful retrieval, and why AI coding agent memory feels broken -the agent keeps forgetting your patterns, conventions, and architecture.
How generic RAG handles coding agent context
Standard RAG pipelines split text into chunks, embed those chunks as vectors, and retrieve the top-K most similar chunks for a given query. This works well for documentation, knowledge bases, and support tickets. For code, it breaks down in specific, predictable ways.
The chunking problem
RAG systems typically chunk by character count or token count -split every 500 tokens, with 50 tokens of overlap. This works for prose because a paragraph is a self-contained unit of meaning. Code has no such property.
A 500-token chunk might cut a function in half, separate a class from its methods, or split an import block from the code that uses those imports. The agent receives a fragment that compiles in isolation but lacks the context to understand what it does or how it fits into the larger system.
Consider a TypeScript file with a class definition, three methods, and two helper functions. A naive chunker produces five fragments. The chunk containing the second method has no access to the class properties it references, the types it uses, or the helper it calls. The agent sees a method signature and a body, but not the class it belongs to or the types it operates on.
The embedding problem
Text embeddings capture semantic similarity. "How to handle user authentication" and "implementing auth for users" are semantically close, so RAG retrieves relevant documents.
Code similarity doesn't work this way. A function called processPayment in your payments service is semantically similar to processRefund in your refunds service. But when an agent asks "how do we process payments?", it needs processPayment, the PaymentGateway interface, the Transaction type, and the error handling pattern -not processRefund. Semantic similarity retrieves related concepts. Code tasks need structurally connected components.
The ranking problem
RAG returns results ranked by embedding similarity. For code, the most similar chunk is often not the most useful one. When an agent needs to add a new API endpoint, the most useful context is: the route registration pattern, the middleware chain, the response envelope type, and one existing endpoint as a template. These four things have low semantic similarity to each other but are all structurally necessary.
RAG might return four endpoint handlers instead -semantically similar, but redundant. The agent gets four examples of what exists but misses the structural context it needs to write correct new code.
How codebase indexing gives AI agents better context
Structured indexing treats code as a graph, not a bag of text. It parses the codebase into its structural components -files, classes, functions, types, imports, exports, call sites, inheritance chains -and builds a queryable map of how they connect.
When an agent needs context, it doesn't search by semantic similarity. It traverses the graph. "Show me how to add a new API endpoint" triggers a structural query: find the route registration file, identify the pattern, follow the type chain to the response envelope, locate the middleware stack, and pull one complete endpoint as a template. The result is a coherent set of files that together provide everything the agent needs.
Structure-aware chunking
Instead of splitting by token count, a code index respects syntactic boundaries. A class is one unit. A function is one unit. An import block is one unit. If a function is too large for a single context window, it's split at logical boundaries (try/catch blocks, conditional branches) with metadata linking the parts.
This means the agent never receives a half-function. It gets complete, compilable units with their type dependencies resolved.
Dependency-aware retrieval
When the agent requests the UserService class, the index also retrieves the types it uses (User, UserCreateInput), the repository it depends on (UserRepository), and the error types it throws (UserNotFoundError). These aren't retrieved because they're semantically similar -they're retrieved because the dependency graph connects them.
The agent gets a complete picture: the class, its dependencies, and its contract. It can write code that uses UserService correctly because it has the full type signature, not just a semantically similar chunk.
Pattern extraction
A code index can identify patterns across the codebase -capturing the tribal knowledge that lives in your team's heads. If 90% of your service classes follow the same constructor injection pattern, the index captures that as a convention. Codebase knowledge management moves from informal "ask the senior dev" to structured, queryable data. When the agent creates a new service, it receives the pattern as a constraint, not just an example.
RAG can't do this because it retrieves individual chunks, not cross-codebase patterns. It might retrieve three different constructor patterns from three different files, leaving the agent to guess which one is canonical.
Where coding agent context quality matters
Token efficiency
RAG retrieves by similarity, which often means retrieving redundant content. Five similar endpoint handlers contain overlapping patterns. A code index retrieves one endpoint handler plus the unique structural context -route registration, types, middleware. This typically uses 40-60% fewer tokens for equivalent or better results.
For a team of 50 engineers making 15 agent requests per day, the token savings compound to thousands of dollars per month.
Code correctness
When agents write code based on structurally complete context, they produce fewer type errors, fewer import mistakes, and fewer pattern violations. The code compiles on the first attempt more often because the agent had access to the actual types and interfaces, not semantically similar but structurally incomplete fragments.
In our measurements, agents with indexed context produce code that passes type checking on the first attempt 78% of the time, versus 52% with RAG-based retrieval. That 26-point gap translates directly into fewer retry loops and less wasted computation.
Architectural compliance
When the index knows your architecture -which layers exist, which modules can import from which -it can provide context that steers the agent toward compliant code. RAG has no concept of architectural layers. It retrieves whatever is most similar, even if that similar code lives in a layer the agent shouldn't be accessing.
When RAG still makes sense
RAG isn't wrong for everything in the development workflow:
- Documentation search. Finding relevant docs, README files, and API guides. This is text retrieval, and RAG handles it well.
- Issue and ticket context. Pulling relevant Jira tickets, Slack threads, or commit messages. Semantic similarity works for natural language content.
- Unfamiliar codebases. When an agent first encounters a codebase and needs a rough orientation before the index is built, RAG provides a fast (if imprecise) starting point.
The mistake is using RAG as the primary code retrieval mechanism. For code understanding, dependency resolution, and pattern following -the core tasks of a coding agent -structured indexing outperforms RAG on every metric that matters: correctness, token efficiency, and architectural compliance.
So which one wins?
Code has structure. It has types, dependencies, layers, and patterns. Retrieval systems that ignore this structure and treat code as searchable text will always produce inferior results. They'll retrieve fragments instead of complete units, similar code instead of connected code, and examples instead of patterns. Tribal knowledge in your codebase -the unwritten rules, the architectural decisions, the "why we do it this way" context -gets lost.
If your agents are producing code that compiles but doesn't fit your architecture, or if your token bills are climbing faster than your output, the retrieval layer is likely the bottleneck. Structured indexing isn't a marginal improvement -it's a better fit for how code actually works.