Blog

RAG vs fine-tuning: Why knowledge and behaviour differ

The choice between RAG vs fine-tuning depends entirely on whether your model lacks facts or formatting. Here is how to measure what you actually need.

Caffinix·September 15, 2026·7 min read
ragfine-tuningarchitecture

RAG and fine-tuning solve different failures. RAG changes what the model knows at inference time by handing it context. Fine-tuning changes how the model behaves by altering its internal probability distribution. You cannot fix a knowledge gap with a behaviour change, and you cannot reliably fix a formatting error by passing more text in the prompt.

When an agent hallucinates in production, the right fix depends entirely on which mechanism is failing. Most teams get this wrong, and the cost of getting it wrong is high.

Two distinct failure modes

When a language model produces bad output it is failing for one of two reasons: it lacks the factual grounding to answer, or it has the facts but will not structure them correctly.

The knowledge failure. A conversational-commerce agent needs to know a specific buyer's current cart, the seller's live inventory, and the shipping cost to their district. A base model was trained months ago on public data. Fine-tuning on historical orders up to yesterday still will not surface the order placed this morning. No system prompt, no few-shot examples, no amount of prompting ingenuity makes the model correctly guess a private database record. This is a pure knowledge failure. The only fix is retrieval.

The behaviour failure. The same agent knows the order number and the refund amount. But it keeps outputting conversational prose instead of the structured JSON payload the downstream API requires. Or it outputs ISO 8601 timestamps when the payment service demands Unix epochs, rejecting the request silently. The model has the facts. It refuses to consistently apply the structural constraints. This is a behaviour failure. The fix is training on examples of the correct output shape.

Teams conflate these constantly. They see a model inventing a response about internal policy and immediately assume it needs to be retrained on the company handbook. Retraining to teach facts is using neural weights as a database. It is slow, inaccurate, and impossible to update in real time.

RAG: facts at inference time

Retrieval-augmented generation treats the model strictly as a reasoning engine and treats an external system — a vector database, a search index, an API — as the memory layer. When a user sends a query, the system searches the external store, retrieves the relevant text chunks, and pastes them into the context window alongside the question.

The model is no longer acting as an encyclopaedia. It is acting as a reading comprehension engine. You are asking it to read three supplied paragraphs and answer based only on what is written there.

This architectural shift moves the hallucination problem from an opaque model weight issue to an inspectable search problem. If the retrieved chunks do not contain the correct answer, the failure is in your search index, chunking strategy, or embedding model — not the LLM. Search failures are deterministic and fixable. Prompt a model wrong and it guesses. Retrieve the wrong chunk and the trace tells you exactly which one was wrong.

This is also why data mutability matters. When a policy changes, you update the database record. The next user query retrieves the updated text with no retraining step. Access control applies at the search layer — the model only receives documents the requesting user is authorised to see, which means it cannot accidentally surface restricted data.

We use Spring Boot + pgvector for the retrieval layer across our AI builds. Iris, the conversational-commerce bot we built on our Nous agent engine for Bangladesh and SEA sellers, is a direct example: it reads the seller's product catalogue, stock and order state from the backend at query time. Fine-tuning on historical catalogue data would have been the wrong approach — the inventory changes continuously, and a fine-tuned model would have been stale within days of training.

Separately, Heimdall — our self-hosted LLM gateway — exposes the embedding and generation APIs to internal services without those services holding provider credentials directly. Because the knowledge lives in retrieval rather than in model weights, changing the model behind that gateway doesn't mean retraining anything.

Fine-tuning: behaviour baked into weights

Fine-tuning takes a pre-trained model and continues training on a curated dataset of specific examples. Backpropagation adjusts the weights so that the model naturally favours the format, tone, or structure you need — without requiring that instruction in every prompt.

There are four situations where this is genuinely necessary:

Structural adherence at scale. If the model must output deeply nested JSON or a custom API tool call, and any deviation breaks a downstream pipeline, prompting fails at high volumes. The model eventually appends a markdown block or adds an explanation nobody asked for. Fine-tuning drops that formatting error rate from a persistent 3–5% to near zero. That 3–5% matters if you are processing millions of documents.

Domain-specific tone. A medical reasoning engine or legal contract analyser needs a specific professional register consistently. Prompting a base model into that tone works on short outputs but slips during long generations. Fine-tuning bakes the register into the weights permanently.

Latency and cost at scale. A prompt with 12 few-shot structural examples might consume 3,000 input tokens per call. Fine-tuning removes those examples from the prompt — the instructions are in the weights. At millions of calls a day, the token savings are real money.

Tool-calling reliability. If an agent has access to 20 internal APIs, fine-tuning on thousands of examples of correct tool selection and argument formatting substantially increases routing accuracy.

The critical caveat. Fine-tuning does not reliably teach new retrievable facts. If you fine-tune on a company handbook, the model memorises fragments. When the handbook changes next month, the model still confidently recites the old version. Worse, a model fine-tuned on domain documents learns the vocabulary and tone of that domain — and will use that exact tone to hallucinate convincing lies about facts that changed. A model fine-tuned on financial reports invents financial numbers that look entirely plausible. If your problem is missing facts, fine-tuning makes your hallucinations harder to detect, not easier.

The operational cost of maintaining fine-tuned weights

Teams consistently underestimate the ongoing burden of a fine-tuned model.

Data curation. You need thousands of correct input-output pairs. Errors in training data are learned exactly. When your API schema changes, every training example referencing the old schema is now actively harmful — you must identify them, rewrite them, and run a new training job. This is a living engineering dependency.

Model deprecation. When a provider deprecates the base model you tuned against, you cannot copy your weights to the new model. You must re-run the entire fine-tuning process. Your training dataset must be maintained indefinitely to support mandatory retraining events.

Provider lock-in. A retrieval pipeline can be pointed at a different LLM endpoint in an afternoon — change the API URL, adjust the system prompt. A fine-tuned model ties you to the provider hosting the weights. If their pricing changes or their latency degrades, you are stuck until you can curate data and retrain elsewhere.

How to sequence them in production

The answer to "RAG or fine-tuning?" is almost always: RAG first, fine-tuning only when retrieval is verified and a structural failure remains.

Start with the retrieval pipeline. Build the search index, the context injection logic, the parsing pipeline. Run this in production until knowledge retrieval is verifiably correct. Only then inspect what the remaining failures actually are.

If those remaining failures are formatting mistakes, tone drift, latency issues, or tool-calling inaccuracies — failures that retrieval cannot address — fine-tune specifically to fix those. Keep the retrieval pipeline intact. The fine-tuned model still receives retrieved context via the prompt. It just processes that context more reliably and outputs the result in a strictly constrained format.

Hybrid production architecture
Routing    → API gateway directs the request
Retrieval  → Vector search finds factual context
Generation → Fine-tuned model formats output strictly
Execution  → Downstream system parses structured response

Fine-tuning handles the shape of the response. RAG handles the factual substance.

Measure before you commit to either

Before you build a vector database, and absolutely before you curate a training dataset, run a baseline eval against the raw model.

Write 50–100 representative questions covering both facts the model needs and formats it needs to produce. Define a passing grade for each. Run the base model. Categorise the failures: how many were knowledge failures (the model lacked the fact), how many were structural failures (the model had the fact but formatted it wrong)?

Add a basic retrieval pipeline. Run the eval again. Knowledge failures should drop substantially. If formatting failures remain unacceptably high after retrieval is in place, you have a clear, measured case for fine-tuning — and a baseline to prove whether the training job worked.

This is the only way to avoid paying for the wrong fix. We apply this sequence on every AI agents and RAG engagement, and every build ships with an eval set from the client's own questions and a stated error rate at handover.

Related service

AI Agents & RAG Chatbots

Production RAG agents grounded in your real data, with tool-calling and a measured hallucination rate.

See how we run it →