Pillar 2 - Retrieval
How outside facts get onto the desk: the difference between handing the model everything up front and letting it grab what it needs, just in time
The Question This Pillar Answers
A model only knows two things: what it learned during training, and what you put in its context window. For anything specific to your world - your files, your data, today's ticket - someone has to fetch it and put it on the desk. That fetching is retrieval, and doing it well is the single biggest source of correct answers (and doing it badly is the single biggest source of made-up ones).
Two Styles: Up Front vs. Just in Time
Before the model starts, you search a library of documents, grab the most relevant chunks, and load them into context. (RAG = "retrieval-augmented generation.") Fast, because the data's already there - but you have to guess what's relevant before the model has even thought about the problem.
Instead of pre-loading everything, you give the model lightweight pointers - file paths, search commands, links - and let it pull the actual content when it decides it needs it. Slower per step, but the model fetches based on what it's actually discovering.
UP-FRONT (RAG) search docs โ grab top chunks โ stuff into context โ start good when: you know what's relevant ahead of time JUST-IN-TIME give the model: file paths, a search tool, a database query tool model decides: "I need to see auth/middleware.ts" โ reads it now good when: the relevant info depends on what the model finds
Why Just-in-Time Mirrors How You Work
You don't memorize your whole filing cabinet before starting a task. You keep an index - folders, file names, bookmarks - and pull the specific thing when you need it. Just-in-time retrieval gives the model the same superpower.
test_utils.py in a /tests folder tells the model something before it even opens the file. Folder names, file sizes, and timestamps all help the model decide what's worth grabbing. It discovers the right context layer by layer, keeping its desk clear.The Hybrid That Usually Wins
In practice the best agents mix both. Anthropic's own Claude Code is the textbook example:
Claude Code's hybrid retrieval:
UP FRONT drop CLAUDE.md into context at the start
(the standing project rules - always relevant)
JUST IN TIME give it grep + glob + file-read tools
(let it hunt down the exact files it needs,
fresh, instead of relying on a stale pre-built index)The always-relevant stuff (project conventions) goes in up front. The depends-on-the-task stuff (which files?) is fetched just in time. Best of both: no stale index, no bloated desk.
Bad Retrieval Is the #1 Source of Wrong Answers
When an agent confidently states something false, retrieval is usually the culprit. Three classic ways it goes wrong:
- Too much: a search returns 4,000 hits, you dump them all in, and the real answer drowns (context overload).
- Stale: your pre-built index is months old, so the model reads a deprecated function and confidently calls it. Freshness matters.
- Off-target: the search grabbed plausible-looking but irrelevant chunks, and the model reasons over the wrong facts.
For Coding Agents: Structure Beats Guessing
A quick real-world note (you don't need to build this, just understand it). For code, plain text search is weak - searching for a function name returns every file that mentionsit. "Code-intelligence" tools instead understand structure: they can return the one place a function is defined plus its exact call sites. In published benchmarks, giving an agent structure-aware retrieval turned tasks that timed out after two hoursinto ones that finished in under two minutes. That gap is entirely context engineering - same model, better retrieval.
Hands-On: Up Front vs. Just in Time
Fetching outside facts (files, data, search results) into the context window so the model can use them
Search a document library and load the top relevant chunks before the model starts
Give the model pointers (paths, queries) and let it pull the actual content only when it needs it
File names, folders, sizes, timestamps act as clues so the model discovers the right context layer by layer
Load always-relevant info up front (CLAUDE.md), fetch task-specific info just in time (grep/file reads)
Too much, stale, or off-target retrieval is the #1 source of wrong/hallucinated answers