Retrieval-augmented generation (RAG)
Retrieval-augmented generation means searching your own documents for passages relevant to a question, then putting those passages into the model’s prompt so it answers from them rather than from memory. The model is not changed; only what it is shown changes.
How the pipeline works
Two halves. Indexing, done ahead of time: split documents into chunks, turn each chunk into an embedding vector with an embedding model, and store the vectors with their text — commonly in Postgres with pgvector, or a dedicated vector database. Query time: embed the question, find the nearest chunks, paste the top few into the prompt with an instruction to answer only from them, and ask the model to cite which chunk it used.
That last part is not decoration. Citations are what make the answer auditable, and without them you cannot tell grounding from guessing.
Retrieval quality is where the work is. Pure vector search misses exact terms — product codes, error numbers, names — so most serious systems run hybrid search, combining vector similarity with keyword matching, then rerank the candidates with a cross-encoder before selecting. Chunking matters more than people expect: chunks that cut a table in half or separate a heading from its content produce retrieval that looks fine and answers that are subtly wrong.
Why it exists
It answers three problems at once: knowledge after the training cutoff, private data the model never saw, and hallucination on specifics. It also updates instantly — add a document and it is available on the next query, with no retraining.
What to watch out for
RAG does not teach the model anything, and it does not guarantee a grounded answer. Two separate misconceptions:
- It is not learning. Nothing persists between calls. Every question re-retrieves and re-sends. Compare with fine-tuning, which changes behaviour and style but is a poor way to install facts. Facts belong in retrieval; form belongs in fine-tuning.
- It is not a hallucination cure. If retrieval returns nothing relevant, most models will answer anyway from memory. You have to instruct and test for the "not in the provided documents" case explicitly, and check that it actually happens.
The failure mode nearly everyone hits: the answers are bad and the model gets blamed. In practice the retrieval step is at fault far more often. Before changing model, log what was retrieved for the failing questions. Usually the right chunk was never in the prompt at all.
Large context windows have not made this obsolete. Sending three relevant pages is cheaper, faster and more accurate than sending the whole manual and hoping.
Frequently asked questions
Should I use RAG or fine-tuning for my company documents?
RAG, almost always. Documents change, and retrieval reflects a change immediately while fine-tuning requires a new training run and still gives no citations. Fine-tuning is the right tool for a consistent format or voice, not for supplying facts. Many systems use retrieval for content and fine-tuning for style.
Build it yourself
NorthernGo turns a plain-text description into a working web app with a database, login and a live URL. Local AI generation runs on your own GPU, is unlimited, and is free on every plan.