Why RAG Exists
An LLM’s knowledge freezes at training time and lives inside its weights. Retrieval-augmented generation (RAG) solves both problems at once: before answering, the system retrieves the relevant passages from your documents and feeds them into the prompt as context. The model answers from that context, not from memory.
| Concern | Without RAG | With RAG |
|---|---|---|
| Fresh information | Model doesn’t know it | Retrieved live |
| Private data | Never in training | Indexed and retrieved |
| Hallucination | Model guesses | Grounded in citations |
| Updates | Retraining | Re-index the docs |
The Pipeline
- Ingest — split source documents into small, self-contained chunks.
- Embed — convert each chunk into a dense vector.
- Index — store vectors in a vector database for fast search.
- Retrieve — embed the user’s question and fetch the top-k nearest chunks.
- Rank — optionally re-score the candidates with a precise cross-encoder.
- Generate — prompt the LLM with the retrieved context and demand citations.
The RAG visualizer on this page steps through this exact flow.
Chunking Trade-offs
| Chunk size | Pros | Cons |
|---|---|---|
| Small (~200–400 tokens) | Precise retrieval | Loses surrounding context |
| Large (~800–1000 tokens) | Self-contained context | Diluted relevance |
Overlap neighboring chunks so a passage split across a boundary isn’t lost. Chunk boundaries should respect section structure where possible — splitting mid-sentence hurts retrieval.
Retrieval and Ranking
Similarity is measured with cosine similarity on normalized vectors: nearest neighbors in embedding space, not keyword matches. “Can I return this?” finds the refund policy even though the words differ.
A cross-encoder re-ranker scores the small top-k set against the exact query and is far more accurate than the embedding score — at the cost of speed. Use it when precision matters more than latency.
Grounded Generation
The prompt tells the model: “Answer using only the provided context, cite the chunk ids you used, and say you don’t know if the context doesn’t contain the answer.” This converts hallucination from a probability into a detectable, auditable event — and the citations give users a path to verify.
RAG vs Fine-tuning
| Approach | Best for | Cost |
|---|---|---|
| RAG | Facts, freshness, private knowledge | Indexing + retrieval infra |
| Fine-tuning | Style, tone, format, domain fluency | Training + eval + serving |
They’re complementary, not either/or. RAG changes what the model knows; fine-tuning changes how it behaves. Most production systems use both: fine-tune the behavior, RAG the facts.
Practice Trajectory
- Design chunking for a 40-page product manual and justify the size.
- Explain why a user’s “return” question can match a “refund” chunk.
- List two failure modes of retrieval and how re-ranking fixes them.
- Write a system prompt that forces cited, grounded answers.
- Decide whether to fine-tune or RAG for a support chatbot over an internal wiki.