Chapter 7: Retrieval and RAG
Lecture
Chapter principle
Key takeaways
- Retrieval generates candidate evidence; it does not generate the final answer.
- RAG couples retrieval, context construction, and generation.
- Similarity does not establish relevance, authority, currency, or truth.
- Reranking cannot recover a relevant item absent from the candidate set.
- Measure retrieval failures and generation failures separately.
- Citations should support claims, not merely accompany them.
Experiments
Every experiment in the book is small, runnable and printed with its real output. In the Lab you can replay a recorded real run of each one, with no programming, and watch what the model and the agent do.
Experiment 13: Word counts vs embeddings
How does an agent find the document that answers a question?
Three short documents and four customer questions. Each question goes to two retrievers. The first counts the words a question shares with each document. The second uses a real embedding model, which turns each text into a long list of numbers so that texts with similar meaning end up close together. Only the first question shares a word with any document. This is a recorded run of a real embedding model. Its scores can vary slightly in a new run.
Experiment 14: Answering from documents
When the retriever finds two conflicting policies, which one does the answer use?
Two documents: an old policy (refund window 30 days, from 2025) and the current one (14 days, from 2026). They differ only in the number, so the retriever scores them the same. A real model then answers “What is the current refund window?” from four versions of the evidence: the top candidate alone, both policies in each order, and both with their effective years. Each version is asked three times. This is a recorded run of a real model. A new run may differ.
Experiment 15: Reranking
Can a second, smarter ranking fix what the retriever found?
The question is “What is the current refund window?”. A fast first stage ranks three documents by the words they share with the question. A slower second stage, the reranker, asks a real model to rate each candidate from 0 to 10 for how well it answers the question. Then the first stage is cut down to return only its top candidate. This is a recorded run of a real model. Its ratings can vary a little in a new run.
All the code for this chapter: github.com/agentic-engineering-book/code
Download the bookBrowse all chapters