Agentic Engineering from First Principles All chaptersLecturesLabDownload the book
Part II · Giving Agents Abilities

Chapter 7: Retrieval and RAG

Central questionHow do we find useful evidence in a large knowledge source, and turn it into a grounded answer?

Lecture

Chapter principle

Retrieval is not truth. And two inequalities to remember. Retrieval quality is not answer quality: a good retriever can feed a poor generator, and a strong generator cannot reliably use evidence it never receives. And similarity is not relevance: semantic closeness is evidence for candidate selection, not proof that a document is the right authority for the task.

Key takeaways

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

This is Chapter 7 of Agentic Engineering from First Principles: thirty chapters, a case study and fifty-nine executable experiments, from a single model call to controlled, self-improving agents.
Download the bookBrowse all chapters
← Chapter 6: Context EngineeringChapter 8: Memory →