RAG vs long context for code: how AI tools decide what code to read
Retrieval, agentic search and whole-repository context are three ways to give a model code. How each works, where each fails, and which suits which questions.
A language model can only reason about code it can see. Every AI tool for code has to answer the same question first: which code goes into the model's context for this request? There are three main strategies, and they explain most of the differences in how these tools behave.
Strategy 1: retrieval (RAG)
The tool splits the codebase into chunks ahead of time, often by function or file, and indexes them, usually with embeddings, sometimes combined with keyword search and a code graph. For each question, it retrieves the chunks that look most relevant and sends only those to the model.
Strengths: scales to codebases of any size; cheap per question.
Weaknesses: if retrieval misses a relevant chunk, the model never sees it, and answers confidently from what it has. Questions about how distant parts connect are hardest, because the connecting code may not look similar to the question.
Strategy 2: agentic search
The model is given tools such as search, read file and list directory, and decides step by step what to look at. Coding agents like Claude Code, Amp and IDE agent modes work this way.
Strengths: adapts to the question; can follow a trail across files; scales to large repositories; can run code to check.
Weaknesses: slower and more expensive per question; results depend on the path the agent took, and it may stop before finding everything.
Strategy 3: whole-repository context
The tool puts the entire codebase (or a filtered version) into the model's context. Modern models accept hundreds of thousands to a million tokens, enough for many real repositories.
Strengths: nothing relevant is missed for lack of retrieval; cross-file questions work well because every connection is visible; answers can cite any line.
Weaknesses: limited by context size; large prompts cost more (prompt caching reduces the cost of follow-ups); attention over very long contexts isn't perfect, so precise citations help.
Comparing them
| Retrieval | Agentic search | Whole repository | |
|---|---|---|---|
| Repository size | Any | Any | Up to the context limit |
| Risk of missing code | Retrieval misses | Agent stops early | Low, within the limit |
| Cost per question | Low | High | Medium, lower with caching |
| Cross-file questions | Weakest | Good | Strongest |
| Speed | Fast | Slow | Medium |
What RepoGrasp does
RepoGrasp uses whole-repository context. For a public GitHub repository up to about 200,000 tokens of source, it sends every kept file with line numbers, so the model can trace paths across the whole codebase and cite exact lines. For larger repositories, you link a subfolder, which keeps the guarantee within that scope. See how RepoGrasp works.
Which should you use?
- Huge codebases, quick lookups: retrieval-based tools.
- Investigations that need running code: agents.
- Understanding a mid-sized repository or one package of a monorepo: whole-repository context.
Whatever the strategy, ask for citations and check them. The strategy determines what the model could see; citations show what it actually used.