RAG in plain English
RAG is answering with your own documents open in front of the model. It stands for retrieval-augmented generation. The model does not guess from memory. It looks up the answer in your files first, then writes the answer from what it found.
Think of it like an exam. A closed-book exam means you answer from memory, and you guess when you do not know. An open-book exam means you look the answer up in the book, then write it down. RAG is the open-book exam. The model still writes the answer, but it has the book open.
Here is a worked example. I have a company handbook as a PDF. I ask, "how many days of leave do I get?" Without RAG, the model guesses. It might say 20 days, or 25, or 15, all in a confident tone. With RAG, the model searches the handbook first, finds the paragraph that says "employees get 24 days of annual leave", and answers 24 days, quoting the paragraph. Same question, same model, different result, because one had the book open.
RAG has four steps.
| Step | What happens | Example |
|---|---|---|
| Chunk | Split the document into small pieces | The handbook becomes 200 paragraphs |
| Embed | Turn each piece into numbers | Each paragraph becomes a list of numbers |
| Retrieve | Find the pieces that match the question | The leave paragraph scores highest |
| Answer | The model writes from those pieces | "You get 24 days of annual leave" |
Chunking matters because a model cannot read a whole handbook at once. You split it into pieces small enough to fit. Embedding matters because numbers let the computer compare meaning, not just words. A question about "holiday" will match a paragraph about "annual leave" even though the words are different. Retrieval is the search. Answer is the model writing from what it found, not from memory.
The catch is that RAG is only as good as your documents. If the handbook is out of date, the answer is out of date. If the document is missing, the model falls back to guessing. RAG does not fix bad documents. It just makes the model read the ones you have.
python# RAG in miniature. No model needed to see the shape of it. # Chunk a document, then retrieve the chunk that best matches the question. doc = "Employees get 24 days of annual leave. Sick leave is 10 days. The office closes on public holidays." chunks = [c.strip() for c in doc.split(".") if c.strip()] question = "how many days of leave do I get" words = set(question.lower().split()) best = max(chunks, key=lambda c: len(words & set(c.lower().split()))) print("retrieved:", best) print("answer: you get 24 days of annual leave")
Try this
bashpython3 -c " doc = 'Employees get 24 days of annual leave. Sick leave is 10 days.' chunks = [c.strip() for c in doc.split('.') if c.strip()] q = set('how many days of leave do I get'.split()) print('retrieved:', max(chunks, key=lambda c: len(q & set(c.lower().split())))) "
RAG or fine-tuning? Topic 16 decides. It shows when to retrieve facts and when to train behaviour, and what each one costs.
Sources
- RAG: Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks", 2020, arXiv:2005.11401.
- Chunk, embed, retrieve, answer: standard RAG pipeline, LangChain and LlamaIndex docs.
