Retrieval-Augmented Generation (RAG)

Retrieval-Augmented Generation grounds an LLM in your data. Instead of relying on what the model memorized during training (which is stale, generic, and prone to hallucination), RAG retrieves the relevant passages from your knowledge base at query time and passes them to the model as context. The model then answers from that context — accurately, citably, and up to date.

It's the most common way to build LLM applications over private or current information: support bots over your docs, assistants over a company wiki, search over a codebase. The pattern is simple to state and full of nuance to get right: chunk → embed → store → retrieve → generate.

TL;DR

Quick Example

The generation step: retrieve, then constrain the model to the context:

How RAG Works

  1. Chunk — split source documents into passages (e.g. a few hundred tokens, with overlap).
  2. Embed — convert each chunk into a vector with an embedding model.
  3. Store — index the vectors in a vector database.
  4. Retrieve — embed the user's query and find the most similar chunks (semantic search).
  5. Generate — pass the retrieved chunks to the LLM as context and have it answer.

Chunking

Chunking is where many RAG systems live or die. Chunks must be small enough to be specific but large enough to be self-contained:

Retrieval Quality

Best Practices

Common Mistakes

Letting the model answer beyond the context

Poor chunking

FAQ

RAG or fine-tuning — which should I use?

Use RAG to give a model access to knowledge — facts, documents, current or private data — especially when that data changes or must be cited. Use fine-tuning to change behavior, style, or format, or to specialize a smaller/cheaper model for a narrow task. They're complementary: RAG injects up-to-date context at query time without retraining, while fine-tuning bakes in patterns. For "answer questions over my docs," RAG is almost always the right starting point.

Why not just paste all my documents into the prompt?

For small corpora you can — large context windows make "stuff everything in" viable for modest data. But it breaks down as data grows: you hit context limits, pay for tokens on every request, and dilute the model's attention with mostly-irrelevant text, which hurts accuracy. RAG retrieves just the relevant passages, keeping prompts focused, cheaper, and more accurate, and scales to millions of documents.

How do I reduce hallucination in a RAG system?

Constrain the model to the retrieved context ("answer only from the context; say 'I don't know' if it's absent"), require citations so claims are traceable, and improve retrieval so the right passages actually reach the model (good chunking, hybrid search, re-ranking). Most RAG hallucinations trace back to retrieval failures — the answer wasn't in the context — so fixing retrieval quality usually matters more than prompt tweaks.

What's the hardest part of building a good RAG system?

Retrieval quality — specifically chunking and search. The generation prompt is straightforward; getting the right passages in front of the model is not. Real systems iterate heavily on chunk size/overlap, embedding choice, top-k, hybrid (semantic + keyword) search, re-ranking, and metadata filtering. Evaluate retrieval directly (did the relevant chunk get retrieved?) separately from answer quality, so you know which half to fix.

Related Topics

References