RAG from Scratch
A series on Retrieval-Augmented Generation, built from first principles with runnable code: chunking, embeddings, the query flow, evaluation, BM25, and advanced retrieval.
Every part implements one piece of a RAG pipeline twice: once from scratch with the Python standard library, and once with real libraries. Each worked example has an expected output, so you can check every claim by running the code in rag-scratch-to-speciality.
Start with Part 0 for setup, or jump to any part that interests you.
Part 0 · 15 Oct 2026
RAG from Scratch, Part 0: What This Series CoversA six-part series on Retrieval-Augmented Generation, built from first principles with runnable code: chunking, embeddings, the query flow, evaluation, BM25, and advanced retrieval.
Part 1 · 15 Oct 2026
RAG from Scratch, Part 1: ChunkingWhy a '50-character' chunk turned out to be 47, and how fixed-size and recursive chunking actually work, implemented from scratch in Python.
Part 2 · 20 Oct 2026
RAG from Scratch, Part 2: Embeddings, Tokens, and MetadataWhy your user's question can silently vanish before retrieval, why chunk sizes belong in tokens, and what metadata does that embeddings can't.
Part 3 · 20 Oct 2026
RAG from Scratch, Part 3: The Query-Time FlowWhat actually happens between a user's question and the LLM's answer, why vector search always returns something, and how to design for 'no match'.
Part 4 · 25 Oct 2026
RAG from Scratch, Part 4: Evaluating RetrievalPrecision, recall, MRR, and nDCG computed by hand and in code, the nDCG mistake that hides missed chunks, and whether 82% vs 78% accuracy is a real difference.
Part 5 · 25 Oct 2026
RAG from Scratch, Part 5: BM25Why vector search confuses error code E-4012 with E-4021, how BM25 scores exact matches, a worked example you can check by hand, and why BM25 scores have no fixed scale.
Part 6 · 25 Oct 2026
RAG from Scratch, Part 6: Advanced RetrievalHybrid search with Reciprocal Rank Fusion, rerankers, query transformation, small-to-big and contextual retrieval, GraphRAG, and agentic RAG, plus how to choose between them.