What is RAG?
Read a little, play a little. No scary maths, and no rush.
What the letters mean
RAG stands for retrieval-augmented generation. Break it into three words:
- Retrieval — before the model answers, go and fetch a few pieces of your own documents that look relevant to the question.
- Augmented — paste those fetched pieces into the prompt, so the answer is augmented with your knowledge instead of only what the model memorized.
- Generation — the model writes a normal fluent answer, but grounded in the text you just handed it.
That is the whole idea. A model with a training cutoff and no access to your company wiki becomes useful once you give it the right pages at the moment it is asked.
The freshness problem
Models don’t know your catalog, yesterday’s policy, or private tickets. Training has a cutoff. Your docs change daily.
Three fixes:
- Fine-tune — bake knowledge into weights. Good for style, bad for changing facts (needs retraining).
- RAG — keep docs outside, retrieve at query time. Update index, next query uses it.
- Long context — paste everything in prompt. Simple for tiny docs, expensive for large ones.
For private/changing data, start with RAG.
How RAG works
Offline: load docs → chunk → embed → store in vector DB.
Online:
- Embed question (same model)
- Search vectors (cosine similarity)
- Take top-K chunks
- Prompt = question + context + “answer only from context”
- Generate grounded answer
Why it won
- Separation: update knowledge without touching model
- Cheap updates: re-embed changed chunks
- Bounded cost: send 5×200 tokens, not whole wiki
- Transparency: show sources, cite, audit
When NOT to use basic RAG
- Need behavior change (tone/format) → fine-tune
- Tiny static docs → long context is simpler
- Near-duplicate docs where one word matters → add filters, rerank, keyword search
- Live state (price, inventory) → call source of truth directly
One question, end to end
An employee asks the HR assistant: "How many vacation days can I carry over?"
- The question is embedded and compared against the indexed handbook.
- The top four chunks come back: the carry-over rule, the general vacation policy, the sick-leave policy, and a section on public holidays. Two are relevant, two are near misses, which is normal.
- The prompt numbers the chunks [1] to [4] and says: answer only from these, cite the chunk you used, and say NOT FOUND if the answer is missing.
- The model replies: "You can carry over up to 5 unused days into the next year [1]."
The employee can click [1] and read the actual policy text. If they ask something the handbook does not cover, such as the office's parking rules, the honest reply is NOT FOUND rather than a confident invention.
Where RAG answers go wrong
When an answer is wrong, the cause is almost always in one of four places. Check them in this order:
- Retrieval missed. The right chunk was not in the top results. Look at what was retrieved before blaming the model.
- Bad chunks. The right passage exists but was split mid-thought, or lost its heading, so it never ranks well.
- Weak grounding. The right chunk arrived, but the prompt did not require using it, so the model answered from memory.
- Stale source. Everything worked, and the document itself is out of date.
Logging the retrieved chunks next to every answer turns this checklist from guesswork into a two-minute inspection.
Minimal RAG (conceptual)
# 1. index once
db.add(docs, embeddings=embed(docs))
# 2. per query
q_vec = embed(question)
ctx = db.search(q_vec, k=4)
answer = llm(f"Q:{question}\nContext:{ctx}\nAnswer from context only, else say 'not found'.")
Production adds: parsing, smart chunking, metadata, query rewrite, evals. That’s the next chapter.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Related chapters
Spotted a mistake or want a topic covered? Report an issue