Chapter 2
RAG
Look up first, then think.
Not a definition lecture — start with why AI needs external memory
Teaching order
Do NOT start with...
Students get lost if you open with infrastructure. Start with three real problems.
Problem 1
Why does ChatGPT "make things up"?
It predicts plausible text — not verified facts.
Hallucination: confident answers with no ground truth in the prompt.
Problem 2
Why doesn't GPT know your company docs?
Your internal PDFs, policies, and databases were never in pretraining.
Enterprise data cannot (and should not) all be baked into model weights.
Problem 3
Why does GPT knowledge go stale?
Training cutoff — e.g. knowledge frozen at Oct 2023.
Model weights are not a real-time database. Parameters ≠ live storage.
The answer
RAG = plug external knowledge into the LLM
Look up first, then think.
(Say this in class — not "retrieval-augmented generation")
Key diagram
RAG: Before vs After
Why AI must have an external memory system
Upgrade
RAG is not just "searching documents"
It is the foundation of an Agent Memory System.
| Memory type | What it is | Implementation |
| Short-term | Current conversation | Context window |
| Long-term | Retrievable knowledge | Vector DB (RAG index) |
| Episodic | Past tasks & sessions | Session / task logs |
| Semantic | Stable domain facts | Curated knowledge base |
Key diagram
Agent + RAG Architecture
LLM + Memory + RAG + Agent — finally connected
One line each
Pretraining teaches knowledge.
RAG teaches retrieval.
Agents teach action.
Now: mechanics
Two phases (after students understand WHY)
| Phase | When | Plain English |
| Offline | Before launch | Build the library (index docs once) |
| Online | Every question | Look up, then LLM thinks |
Phase 1
Build the knowledge index
- Load documents (PDF, wiki, DB export)
- Clean & chunk into passages
- Turn text into searchable vectors (embedding)
- Store in vector database
This is long-term / semantic memory — not magic, just organized storage.
Phase 2
Answer a question
- User asks a question
- Look up — find relevant passages in the index
- Think — LLM reads evidence + question, generates answer
- Return answer with citations
System: Answer ONLY from context below.
Context: [doc_42] EU refunds within 14 days...
User: What is our EU refund policy?
Technical (last)
Under the hood (optional depth)
$$\text{sim}(q,d)=\frac{\mathbf{v}_q\cdot\mathbf{v}_d}{\|\mathbf{v}_q\|\,\|\mathbf{v}_d\|}$$
Embedding maps text → vectors. Similar meaning → nearby vectors → retrieval works.
| Metric | Offline | Online |
| Quality | Recall@k | Faithfulness, citation hit rate |