Giving a model an open book: RAG from scratch, and how to tell if it helped
For three weeks I built the parts of an AI stack in isolation: a model that reads text (the forward
pass), one that reads it fast (the KV-cache), and an index that finds similar vectors (HNSW). This
week they met. Retrieval-augmented generation — RAG — is just those parts wired together: embed a
question, pull the most relevant passages from a corpus, paste them into the prompt, and let the model
answer from them instead of from memory. The building was quick. The interesting half was the question
underneath it: how do you actually know retrieval helped?
What I built
A RAG pipeline over a small labelled corpus, plus an evaluation harness that scores it. The vivid
moment — ask GPT-2 small “Who wrote Romeo and Juliet?”:
no context -> "The author of Romeo and Juliet, who wrote the book, and who wrote the book's title"
with context -> "The author of Romeo and Juliet, William Shakespeare, was a great poet and a great writer"
Same model, same question; the one retrieved sentence (“William Shakespeare wrote the tragedy Romeo and
Juliet”) is the difference between a shrug and a name. Across the whole question set, retrieval scores
recall@3 = 0.58, MRR = 0.61 — against a random baseline of ~0.19. Decisively better than chance,
and decisively short of perfect. Both of those facts turned out to matter.
The concepts that made it work
- RAG is an open-book exam. A model only knows what’s in its weights; retrieval lets it look things
up. Embed the query, find the nearest passages in vector space, and hand them over as context. - The embedder is the lever. My retrieval is only 58% recall@3 because I’m embedding with
mean-pooled GPT-2 hidden states — and GPT-2 was never trained to make sentence vectors. The entire
pipeline stays the same if you swap in a model built for embeddings; recall jumps. Almost all of RAG
quality lives in that one component. - “Did it help?” is two questions. You cannot judge a retriever by reading generated answers,
because a weak reader will produce weak answers even on perfectly retrieved context. So you measure
the retriever (recall@k, MRR) and the reader separately. Componentised evaluation is the whole game.
The thing that bit me
The first time I ran it end to end, I judged retrieval the obvious way — I read the answers. They were
rough even when the correct passage had clearly been pulled, and I nearly concluded my retrieval was
broken. It wasn’t. The weak GPT-2 generator was masking a retriever that was doing its job. I’d
conflated two independent failure modes into one vibe. The fix was to stop reading answers and start
measuring the retriever on its own, against labels — and suddenly “it’s broken” became “recall@3 is
0.58, and here’s exactly which questions miss and why.” That’s the difference between an opinion and a
number, and it’s the single most useful habit RAG forced on me.
(Smaller trap: I also spent half the week unit-testing the metrics themselves — recall_at_k,
reciprocal_rank — on hand-computed cases before trusting a single aggregate. A measurement is only as
honest as the ruler.)
Tradeoffs & the road not taken
- Exact retrieval here, not the HNSW I just built. 16 passages — exact cosine is correct and
isolates embedding quality. Week 3’s index is the drop-in the moment the corpus is large; the
interface doesn’t change. - No end-to-end answer metric. I measured retrieval, not “does the final answer contain the gold
fact.” With a stronger reader that becomes worth building; with GPT-2 small it would mostly measure
the reader’s weakness. - Still the toy model, on purpose. The point was the wiring and the measurement, not SOTA answers.
Where this sits in the system
This week united the sprint: embeddings + an index + the model, into something that answers questions
about data it was never trained on. Weeks 1–3 built the organs; Week 4 connected them. Next the model
stops merely answering and starts acting — an agent loop with tools — and later, distributed
inference. (I’ve also added a “run real models locally” extension to the roadmap — quantization, then
llama.cpp / Ollama / MLX — because so far I understand the machine deeply but still can’t run a capable
one on my laptop.) Same spine as always: one system, revealed a layer at a time.
Part of a series building a modern AI stack from scratch, told visually. (Last time: a vector index from scratch.)