guides/rag-vs-fine-tuning 1.00

RAG vs fine-tuning for code

Should you retrieve from your codebase or train on it? What each approach actually does, where each fails — and why for code questions the answer is almost always RAG.

Two different questions

The confusion starts because both approaches get described as "teaching the model your codebase." They teach it different things:

One supplies facts on demand; the other shapes behavior everywhere. Most "should we fine-tune on our repo?" questions are really asking for the first thing, which is what RAG does.

The case for RAG on code

The case for fine-tuning

Fine-tuning is not useless — it's pointed at a different target. It's the right tool when you want to change how a model writes, not what it knows:

Notice none of those are "answer questions about our repository." The moment the task is knowledge about code that changes, fine-tuning is fighting the wrong battle.

Side by side

RAGFine-tuning
Knowledge freshnessRe-indexes on every saveFrozen at training time
VerifiabilityShows the exact source chunksWeights only — no citations
Hallucination on unknownsVisibly misses or admits gapsPlausible fabrication
Data privacyCan be fully localCorpus shipped to training
Setup costMinutes (oce index)Dataset + runs + eval loop
Best atQuestions about changing codeStyle, format, narrow tasks

The standard answer: both, with retrieval first

Mature teams compose them: retrieval supplies facts and current code; fine-tuning (when justified) supplies manner and format. If you only do one for codebase work, do retrieval — it's where the correctness lives. See the code RAG guide for what a good retrieval pipeline needs (AST-aware chunking, hybrid fusion, measurement), or go straight to running it:

npm install -g @dhaxor/open-context
oce index                    # local hybrid index, minutes
claude mcp add oce -- oce mcp  # ground Claude Code in the real source

The MCP server guide covers Cursor and HTTP clients too; the benchmarks page shows what the retrieval actually scores before you trust it.

Frequently asked questions

Is RAG better than fine-tuning for code?

For questions about a specific, changing codebase — yes. Retrieval always answers from current source with citations; a fine-tuned model goes stale after the first merge and can't show its work.

When does fine-tuning make sense?

When the goal is behavior, not knowledge: house style, proprietary formats and DSLs, or a narrow stable task where trimmed prompts save real cost.

Can I combine both?

Yes — fine-tune for style and format, retrieve for facts and citations. They solve different problems and compose cleanly.

What does local RAG cost?

An index build in minutes plus modest context per query. With in-process embeddings and SQLite it's free and offline — nothing leaves the machine.

How do I start?

npm install -g @dhaxor/open-context, oce index, then oce search — or serve it over MCP. Setup guides are in the docs.