Chapter 4. RAG and Agents
In Chapter 3, we looked at prompt engineering as one way to equip LLMs with the information they need. In this chapter, we introduce retrieval-augmented generation (RAG) and show how it evolves into agentic RAG and, ultimately, tool-using agents.
The shortcoming with prompt engineering is that in many real systems, better prompts are not enough. The model may need facts that were never in its training data, details that change frequently, or private information that cannot be included in pretraining. RAG offers a practical and cost-effective alternative; instead of forcing the model to “know” everything, the system retrieves relevant information at runtime and injects it into the model’s context before generation. Figure 4-1 shows the comparison between RAG and prompt engineering.
Figure 4-1. Comparison of prompt engineering and RAG. Both are common ways to adapt an LLM system.
RAG changed how LLM applications are built. Rather than treating the model as a self-contained knowledge base, modern systems increasingly treat it as a reasoning and synthesis engine that operates over external data. We’ll build up to that perspective in three steps:
-
First, we cover vanilla RAG (also known as classical RAG or basic RAG). In its simplest form, a RAG system retrieves a set of top-k documents relevant to each user query, augments the user query with them, and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access