guides/code-rag 1.00

Code RAG: retrieval-augmented generation for your codebase

How RAG over source code works, why the naive recipe fails on real repositories, and how to run it locally — with retrieval quality you can measure.

What is code RAG?

Code RAG is retrieval-augmented generation applied to source code: index a codebase into searchable units, retrieve the ones relevant to the current task, and put them into the model's context before it answers or edits. It exists because a real repository is orders of magnitude larger than any model's context window — so an AI assistant's grounding is only as good as its retrieval.

Every AI coding tool you use is doing code RAG under some other name. When Claude Code answers "where do we validate JWTs?", when Cursor suggests a fix in the file you haven't opened, when a PR-review bot flags a broken caller — a retriever chose what code the model saw. Weak retrievers produce the classic failures: hallucinated APIs, edits to the wrong file, "fixes" that break code the model never saw.

Why naive code RAG fails

The default recipe — split files every N lines, embed the chunks, cosine-search them — falls over on real codebases in three distinct ways:

Each failure mode rules out half the query space. That's why production code RAG needs both retrieval signals — and the structure code uniquely provides.

What good code RAG looks like

The pieces that separate measured retrieval from a demo:

Run code RAG locally in three commands

Open Context Engine is a code-RAG engine you can run over your own repository — local, private, and free (MIT core):

npm install -g @dhaxor/open-context
oce index                        # AST-aware chunks + hybrid index + code graph, in local SQLite
oce search "where do we validate JWTs?"

Embeddings run in-process (MiniLM by default — no API key), everything lives in local SQLite, and if the vector extension is unavailable the engine degrades to keyword-only BM25 instead of dying. To give an agentic workflow this grounding, serve it over MCP — the MCP server guide covers Claude Code and Cursor in one command each.

Measure before you trust

The uncomfortable question for any code-RAG tool: what's your recall@10? If a vendor can't answer with a methodology — gold sets, scoring metrics, before/after for ranking changes — its accuracy claims are marketing. OCE treats retrieval quality like tests: committed gold sets (this repo, Flask 3.1.2, gin v1.10.0), CI baselines, and a public benchmarks page generated from the eval harness's own reports. The harness ships with the product, so you can score it on your own codebase too.

Why not just fine-tune on the codebase?

A common alternative — training a model on your source — answers a different question. Fine-tuning shapes how a model writes; retrieval supplies what it knows, freshly, with citations. Code changes every commit, so frozen weights go stale where a re-indexed answer doesn't. The full comparison is in RAG vs fine-tuning for code.

Frequently asked questions

What is code RAG?

Retrieval-augmented generation over source code: index a repo into searchable units, retrieve what's relevant to the task, and put it in the model's context before it answers or edits. It's how AI assistants handle codebases bigger than the context window.

How is code RAG different from general RAG?

Code has machine-checkable structure: ASTs, imports, call graphs, symbol tables. Good code RAG chunks on AST boundaries, fuses semantic + keyword retrieval, expands along the code graph, and resolves symbol definitions — none of which general text RAG needs.

Why does chunking matter so much?

Because a chunk that halves a function embeds as neither half's meaning. AST-aware chunking keeps chunks semantically complete, which is the biggest single accuracy lever in the pipeline.

Can code RAG run fully offline?

Yes. With OCE, embeddings run in-process with no API key and the index is local SQLite; keyword-only mode covers environments where vector search is unavailable. Nothing is uploaded.

How do I evaluate a code RAG system?

Build or use committed gold sets and score recall@k, MRR, and nDCG — OCE's benchmarks page shows the method and the numbers, and oce eval reproduces the harness on your own repo.