Retrieval-Augmented Generation (RAG)
RAG gives a language model an open-book exam: retrieve the most relevant passages from your knowledge base, paste them into the prompt, and instruct the model to answer from them. The result is a general chatbot turned into an expert on *your* documents, with citations to prove it.
Learning Objectives
- Explain RAG: retrieve relevant knowledge, then generate a grounded answer.
- Trace one question through the full RAG pipeline.
- Explain why RAG beats both raw LLMs and fine-tuning for knowledge tasks.
- Identify the failure points: retrieval misses, bad chunks, ignored context.
- Know the quality levers production teams actually pull.
Why This Matters
RAG is the most deployed LLM architecture in industry, full stop. Every "chat with your docs" product, every enterprise assistant that knows company policy, every support bot with current product info β RAG. It solves the three fatal flaws of a raw LLM at once: knowledge cutoff (AI-007), no access to private data, and hallucination (AI-060). This lesson assembles everything from Level 2 β tokens, prompting, embeddings, vector search β into the architecture you will actually build.
Everyday Analogy
Two exams. Closed-book: the student answers from memory β fluent, but shaky on details, and prone to confident inventions when memory fails. That is a raw LLM. Open-book: the student first looks up the relevant pages, then answers with the book open, citing page numbers. Same student, dramatically more reliable. RAG is the open-book exam, and the citations mean you can check their work.
The Pipeline, End to End
Offline (once per document): chunk your knowledge base β embed each chunk β index in a vector database. (This is exactly AI-015's pipeline.)
Online (per question) β user asks: "What is our parental leave policy for part-time employees?"
- Embed the question with the same model used for chunks (AI-014).
- Retrieve the top-k nearest chunks (say k=5) from the vector DB: the HR policy passages most similar in meaning (AI-015).
- Assemble the prompt (AI-010's skeleton): system instructions + the 5 retrieved passages + the question + guards: "Answer only from the provided context. Cite the source of each claim. If the context doesn't contain the answer, say so."
- Generate. The LLM (AI-009) writes the answer from the supplied passages, within its context window (AI-061).
- Show sources. The UI renders the answer with clickable citations to the original documents.
Total cost: one embedding call + one vector lookup + one LLM call: a few cents, a few seconds.
RAG vs Fine-tuning vs Long Context
Three ways to give a model your knowledge β teams confuse them constantly:
| RAG | Fine-tuning (AI-051) | Stuff the context (AI-061) | |
|---|---|---|---|
| Knowledge updates | Re-index a doc β minutes | Retrain β days | Re-paste every call |
| Cost at scale | Low per query | High upfront | Very high per query |
| Citations | Natural | None | Possible |
| Best for | Facts, documents, freshness | Style, format, behavior | Small, one-off corpora |
The standard guidance: RAG for knowledge, fine-tuning for behavior. They combine well β a fine-tuned tone with RAG-supplied facts.
Where RAG Breaks (and What to Pull)
RAG quality problems come from specific, diagnosable stages:
- Retrieval misses. The right passage exists but wasn't in the top-k. Levers: better chunking (respect paragraph/section boundaries), hybrid search for exact terms (AI-015), a reranker (a second model reordering the top-50 into a sharper top-5), query rewriting ("it" β the actual topic). See AI-077 for a practitioner-depth walkthrough of chunking strategies, hybrid search, and reranking.
- Bad chunks. A passage cut mid-sentence carries broken meaning. Lever: chunk on structure, overlap edges, keep titles attached.
- Ignored context. The model answers from its weights instead of your passages. Levers: stricter prompt guards, citation requirements, "lost in the middle" placement β put the best chunk first or last (AI-061).
- Stale index. The doc changed; vectors didn't. Lever: re-index on document change, not on a quarterly calendar (AI-006's freshness).
Measure each stage separately β retrieval recall and answer groundedness are different metrics (AI-025) β or you will fix the wrong stage.
Real-World Showcase
- Perplexity built a search company on web-scale RAG: retrieve live results, generate with citations (AI-060's showcase, now with its architecture named).
- Every enterprise copilot, from Microsoft 365 Copilot to Glean and Notion AI Q&A, is RAG over your emails, docs, and wikis with permission-aware retrieval.
- Klarna's assistant (AI-009's showcase) answers policy questions via RAG. The LLM never memorized refund rules; it reads them per query, so a policy edit updates the bot in minutes.
Try It Yourself
- Run manual RAG: paste a policy document into a chatbot with "Answer only from this document, cite the section, say 'not found' if absent" β then ask one answerable and one unanswerable question. You just executed steps 3β5 by hand, and watched the guards work (AI-060's grounding experiment, now recognizable as RAG).
- Design retrieval failure: ask about a topic the document mentions with a synonym only ("annual leave" when the doc says "vacation days"). If quality drops, you've seen a retrieval-side miss, and you know the lever (embeddings handle it; keyword search wouldn't).
- Sketch your own RAG: pick a corpus you know (team wiki, course notes). Write down: chunk size, metadata fields, k, and the guard lines of your prompt. That half-page is a genuine v1 design.
Common Mistakes
- Blaming the LLM for retrieval failures. Most bad RAG answers never had the right passage in the prompt; debug retrieval first.
- Skipping evaluation β without a golden set of questionβexpected-passage pairs, tuning is guesswork (AI-022, AI-025).
- One-size chunks β legal contracts and Slack threads need different chunking; inspect what your chunks actually look like.
- Believing long context killed RAG β million-token windows still lose the middle and bill per token; retrieval remains cheaper, faster, and more accurate (AI-061).
- No citations β grounding without visible sources wastes RAG's biggest trust dividend (AI-060).
Key Takeaways
- RAG = open-book exam: retrieve β assemble prompt β generate grounded, cited answers.
- It fixes cutoff, private data, and hallucination in one architecture, hence its dominance.
- RAG for knowledge, fine-tuning for behavior; they compose.
- Debug by stage: retrieval recall vs generation groundedness are separate dials.
- Everything in Level 2 (tokens, prompts, embeddings, vector DBs, context) just became one system.
Glossary
- Retrieval-Augmented Generation (RAG)
- The open-book exam for LLMs: retrieve the most relevant passages from your knowledge base, paste them into the prompt, and instruct the model to answer from them with citations. It fixes knowledge cutoff, private-data access, and hallucination in one architecture, making it the most deployed LLM pattern in industry.
- Retrieval
- The "R" stage: embed the question, pull the top-k nearest chunks from the vector database. Most bad RAG answers are retrieval misses: the right passage never reached the prompt, so debug this stage first. (see AI-015)
- Grounding
- Anchoring the model's answer to supplied passages rather than its weights, enforced with prompt guards like "Answer only from the provided context; if it doesn't contain the answer, say so." (see AI-060)
- Reranker
- A second model that reorders the retrieved top-50 into a sharper top-5, one of the highest-leverage fixes for retrieval misses.
- Chunking
- Splitting the knowledge base into passages that respect paragraph and section boundaries, with overlapping edges and titles attached. A passage cut mid-sentence carries broken meaning. Bad chunks are a diagnosable RAG failure stage.
- Query Rewriting
- Transforming a vague user question ("what about part-timers?") into an explicit, retrievable one before embedding it β a standard lever when conversational follow-ups miss.
- Fine-tuning
- The alternative knowledge path that retrains model weights β days of work versus minutes to re-index a document. The standard guidance: RAG for knowledge and freshness, fine-tuning for style and behavior; they compose. (see AI-051)
- Groundedness
- The evaluation metric for the generation stage: did the answer actually come from the retrieved passages? Distinct from retrieval recall; measure both separately or you will fix the wrong stage. (see AI-025)
References
Diagram
Knowledge Check
7 questions