Leanpub Header

Skip to main content

Retrieval-Augmented Generation

Where the pipeline actually fails — chunking, retrieval, attribution and the numbers that hide it

This book is 100% completeLast updated on 2026-08-17

Chunk at 500 with 50 of overlap. Retrieve five. Add a reranker. Use hybrid search.

Repeated everywhere, justified almost nowhere.

This book measures them instead, on corpora built so that the correct answer follows from

how they were made. Several results contradicted the advice: the hybrid combination lost in

every condition tested, neither reranker produced an effect above the noise floor, and

attribution collapsed to 0.170 while retrieval was still reporting 0.920.

Minimum price

$16.00

$16.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Retrieval-augmented generation is the most widely deployed pattern in this field and the one

with the least measurement behind its defaults. Chunk at 500 tokens with 50 of overlap.

Retrieve five passages. Add a reranker. Use hybrid search. Each of those is repeated

everywhere and justified almost nowhere.

The result is a system with several tuning points, no instrumentation, and a single accuracy

number that cannot say which of its two stages is failing. When it works, nobody knows which

choice mattered. When it does not, the fixes are attempted in the order they were heard

rather than the order the evidence supports.

The obstacle to measuring retrieval is knowing what the right answer was — and human

relevance judgements are expensive, subjective and unavailable at the scale a book needs.

This book sidesteps that entirely by building corpora whose correct answers are true by

construction. Queries are drawn as spans from a text, so the chunk containing that span is

correct by definition. Documents describe invented entities, so no retriever can succeed by

prior knowledge, and each fact appears exactly once, so attribution can be checked exactly.

Nothing here is scored against a judgement. Every number is scored against a fact about how

the corpus was built.

Four of the results contradict advice that circulates as settled:

- **Hybrid search made things worse.** Combining a strong lexical retriever with a weaker

dense one lost on every metric in every query condition — and a fusion-weight sweep fell

monotonically with no interior optimum, which turns out to be a reusable diagnostic: a peak

in the middle means fusion is buying something, a monotonic curve means drop the weaker

retriever rather than tune the fusion.

- **Neither reranker beat the retriever** — and, more importantly, none of the differences

exceeded the noise floor, which this book computes before reporting any of them. The same

table, reported without the floor, would support two opposite headlines.

- **Attribution collapses long before retrieval does.** With near-duplicates in the corpus,

the answer was still retrieved 92% of the time while the fraction of claims traceable to a

single source fell from 1.000 to 0.170. A citation feature becomes arbitrary while every

retrieval metric looks healthy.

- **Retrieval scores are a usable abstention signal** — unlike a model's own confidence. On

50 queries about entities absent from the corpus, the highest scored 1.62 against a

threshold at 2.94, with nothing in between.

And two that confirm the advice, with the numbers behind it:

- **Chunk overlap is the cheapest fix in the book.** Ten words of overlap took the rate of

answers cut by a boundary from 10.0% to 0.5%, for a 9% larger index — while chunk size,

the parameter people reach for, costs prompt tokens on every request instead.

- **Retrieval depth trades signal for cost brutally.** Going from one chunk to twenty

multiplied the chance the answer was present by 2.4 and the token cost by 20, ending with

95% of the context irrelevant.

One boundary is stated plainly rather than hidden. A RAG pipeline has two stages and this

book can measure only one directly: retrieval is deterministic and is measured exactly;

generation requires a capable language model the build environment does not have. So

generation is either simulated with a controllable accuracy — which is enough to derive the

arithmetic of a two-stage pipeline exactly — or cited and marked in a visually distinct box.

No claim about what a model does with retrieved context is presented as if measured here.

What you get:

- 8 chapters covering the two-stage pipeline, chunking, lexical and dense and hybrid

retrieval, retrieval depth and reranking, duplication, evaluating two stages separately,

citation and abstention, and a final chapter of limits.

- 42 original practice questions, tagged by certification and by difficulty, where every

option is explained — not just why the right answer is right, but why each wrong answer is

wrong, because on these exams the distractors are where the teaching is.

- A reverse map from each certification to the chapters that serve it, in reading order.

- All the code — every retriever, every experiment — so each table and figure can be

regenerated.

Written to the published objectives of Databricks Certified Generative AI Engineer, NVIDIA

NCP-GENL, NVIDIA NCA-GENL, NVIDIA NCP Agentic AI, and AWS Certified Generative AI Developer –

Professional. Objectives were checked in August 2026; confirm the current blueprint with the

certifying body before you sit.

You need to be able to read Python. Embeddings, lexical scoring, ranking and evaluation are

built here from the ground up, and nothing in this volume assumes you have read any other

book.

Every question is original, written from published exam objectives. Nothing is reproduced

from, or based on recollection of, any live examination.

This book was created through a process that combines careful human planning, content direction, and advanced AI technology, followed by thorough refinement and review to ensure a high-quality final work.

Author

About the Author

Hatem M.

Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.

His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.

Contents

Table of Contents

Contents
  • 1. The Two-Stage Machine
    • Why the pattern exists
    • The shape of the pipeline
    • Two stages, one number
    • How this book measures retrieval exactly
  • 2. Where to Cut the Documents
    • What chunking decides
    • Chunk size, measured
    • Overlap, which changes the picture entirely
    • Query length matters more than expected
  • 3. How the Retrieving Is Done
    • The two families
    • The measurement
    • The sweep that identifies the cause
    • Why the dense retriever was weak, stated plainly
    • What the query condition shows
  • 4. How Much to Retrieve
    • The trade, measured
    • Reranking: the standard remedy
    • The number that had to be computed first
  • 5. What Duplication Does
    • What it costs the context budget
    • What it costs attribution
    • What to do about it
  • 6. Evaluating Two Machines at Once
    • The decomposition
    • Conditioning matters: generation is measured on the hits
    • What each stage needs measuring with
    • The noise floor, again
    • Building the evaluation set
  • 7. Citing Sources and Knowing When to Stop
    • What attribution actually requires
    • Queries the corpus cannot answer
    • The measurement that contradicted the expectation
    • Designing for abstention
  • 8. Limits, and Where Each Exam Goes
    • The limits, and what causes each
    • What retrieval-augmented generation does reliably
    • What this book did not establish
    • The reverse map: your certification to these chapters

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub