What Is RAG, and Why Does It Make Language Models More Trustworthy?
Retrieval-augmented generation (RAG) lets a language model look things up in your own sources before it answers. Here are its parts, trade-offs and common failure modes.
Key takeaways
- RAG does not change what the model knows; it hands the model relevant text alongside the question.
- Retrieval quality (chunking, embeddings, reranking) usually matters more than the choice of model.
- Without separate evaluation of retrieval and generation, you cannot tell where errors come from.
Large language models know a great deal, but their knowledge stops at training time and they have never seen your internal documents. Retrieval-augmented generation, or RAG, is the standard remedy: before the model answers, the system finds the most relevant passages in a knowledge base and places them next to the question. Think of it as an open-book exam.
What RAG actually does
The flow is simple. A question arrives, a handful of relevant passages are retrieved, those passages are inserted into the prompt, and the model writes an answer based on them. The model's weights never change. Updating knowledge therefore means adding or removing documents in an index, not retraining. That is the key difference from fine-tuning, which mostly shapes a model's style and behavior rather than its facts.
Chunking: the first big decision
Documents must be split into smaller chunks, because the model's context window is finite and a large, padded chunk makes search blurry. Chunks that are too small, on the other hand, lose their context. A sensible starting point is a few hundred characters or a few dozen tokens with some overlap. Better still is splitting along the document's own structure: headings, paragraphs and tables.
def chunk(text, size=800, overlap=120):
step = size - overlap
return [text[i:i + size] for i in range(0, len(text), step)]
Embeddings and vector search
An embedding model turns each chunk into a vector of numbers so that texts with similar meaning land close together. The question is embedded too, and the nearest chunks, usually by cosine similarity, are returned. Large collections use approximate nearest-neighbor (ANN) indexes, which trade a little accuracy for a lot of speed. A practical caveat: vector search is weak on exact names, product codes and numbers, so combining it with keyword search such as BM25 often gives better results.
Reranking and building the context
First-pass retrieval is fast but coarse. A common pattern is to fetch twenty or fifty candidates, then let a reranker, a model that reads the question and passage together, pick the best few. Every extra chunk adds cost and noise, so more is not automatically better. Pass each chunk with a source identifier so the answer can cite it.
Grounding and citations
The goal of RAG is a grounded answer. Tell the model explicitly to use only the supplied passages, to cite a source for each claim, and to say it does not know when the passages do not contain the answer. Make citations clickable in the interface so users can verify them. This reduces hallucination considerably, but it does not eliminate it.
Evaluation and failure modes
Measure retrieval and generation separately. For retrieval, build a set of questions with known correct passages and check whether the right one appears in the top k results. For generation, check that the answer is faithful to the sources and relevant to the question. The usual culprits are these:
- Poor chunking that splits an answer across two pieces.
- Stale or duplicate documents that contradict newer ones.
- Multi-step questions that a single retrieval pass cannot answer.
- Tables and scanned PDFs that were extracted badly.
- Hidden instructions inside documents that steer the model; our article on AI security covers this.
This article is sample educational content; verify against official sources before making important decisions.