Σ 0:00▸ 0:00

Module 3 · Agent Tools, Knowledge, Memory & Research Design · scripted

Retrieval Augmented Generation

30 minoutcomes: context-engineering, knowledge-systems

Overview

Everything the agent has fetched so far, it fetched with a tool call. Retrieval Augmented Generation does it without one: before the model runs, code searches a collection of documents and inserts the passages it judges possibly relevant into the prompt. The agent asks for nothing; the context is simply already there when it starts. That makes RAG someone else writing the opening of the conversation — and every failure in this lesson follows from that: the chunk boundary that cuts a rule from its exception, the setting that floods the prompt with near-misses, the search that always returns something even when the collection holds nothing useful.

Where this fits

RAG is context that arrives provided — the agent never asks for it, because code searched and inserted it before the model ran. That code is placing a bet on "what do I need" before the agent has a chance to ask the question itself. When the bet is right, the context is simply there; when it is wrong, the agent starts the task with someone else's guess.

Where this fits

kindprovided — code searches and inserts the context before the model runs
question it answers"what do I need" — answered by a guess made ahead of the task

Retrieval without a tool call

Up to now the agent has done its own fetching: read the manual, searched the document, asked the library. Every one of those was a tool call the agent chose to make.

RAG removes the choosing. A piece of code runs first, searches, and pastes what it found into the prompt:

Retrieval runs before the model

Step by step:

What actually happens

AHEAD OF TIME

step 1cut the documents into chunks
step 2compute each chunk's address in meaning-space
step 3store the chunks and their addresses

WHEN A TASK ARRIVES

step 4compute the task's address the same way
step 5find the chunks whose addresses are nearest to it
step 6paste those chunks' text into the prompt
step 7the model reads the task and the pasted text together, and answers

The agent never called a tool. Steps 4 through 6 happened before it ran.

Nothing was added to the model

The model did not learn the documents. It was not trained,
not extended, not connected to a database.

We changed the TEXT IT RESPONDS TO. Retrieval is prompt
construction — the one big prompt, built partly by machine.

So whoever writes the chunker, the search, and the insertion decides what the agent knows before it speaks. That is why the small choices below matter so much. Let's break it, one choice at a time.

"Use RAG" is not a method

Every arrow in the diagram hides a decision, and each decision can quietly change what the model believes. Watch four of them fail.

Example 1 · The chunk boundary cuts the truth in half
user

Can we schedule a delivery to Quay 3 for 23:30 Thursday?

····················································· [retrieved chunk 41]: "…§142: Deliveries outside ordinary working hours (23:00–06:00) are permitted only at Quay 3, subject to a maximum sound level of 55 dB…" ·····················································

assistant

Yes — night deliveries are permitted at Quay 3, provided you stay under 55 dB. I'll schedule it for 23:30 Thursday.

✎ noteConfident, cited, wrong. Chunk 42 — the next one, not retrieved — begins: "…except during the herring season (weeks 45–2), when the quiet period extends to all quays." The chunker cut a rule from its exception at a paragraph boundary, and the retrieval returned only the half that matched. The model answered correctly about the text it was shown. The pipeline decided what that text was.

This is the tearing principle from earlier, now with a sharper consequence: at ingestion, a bad tear costs a strip its aboutness; at retrieval, it delivers half a rule as if it were whole. Chunking is a decision about which sentences are allowed to be separated — and rules travel with their exceptions or they lie.

Example 2 · What text you search with
user

My postdoc keeps buying equipment without asking — what's the rule?

✎ noteVersion A searches with the user's words exactly as typed. Their nearest neighbors are passages about personnel disputes and supervision — because that sentence is mostly about a postdoc misbehaving. Inserted: the conflict-resolution policy. Unhelpful.Version B rewrites the search text first: "procurement authorization — who may purchase equipment, approval thresholds." Inserted: the procurement policy, §3 approval limits. The documents didn't change and the store didn't change. Only the text used for the search changed — and that text is something you design.

How many chunks to insert

k = 1insert only the nearest chunk. If the answer spans two passages — the rule here, its definition there — the agent reasons from a fragment.
k = 20insert twenty, most of them merely nearby-ish. The real rule is in the prompt, buried under nineteen almost-relevant pages. Too much context, again — and this time the retriever caused it, not the tool.

a distance cutoff: "insert only chunks closer than X" — but on a question the collection doesn't cover, some chunks are still nearest, and they are still inserted. Nearness is relative.

That last line deserves its own alarm: retrieval always returns something. Nearest is not the same as near. The most dangerous RAG answer is the one assembled from confidently-inserted chunks that were merely the least-far-away.

How the text is introduced

One more decision, and it may matter most of all: how the inserted text is labeled when it enters the prompt. Anything that enters the conversation can instruct, and these chunks enter in the user's message. Watch the same chunk under two labels:

Example 3a · "Use this information"
user

When was the harbor's east crane installed?

····················································· Use the following information to answer: [chunk] "…the west crane, installed 1987, remains the harbor's primary heavy-lift asset…" ·····················································

assistant

The east crane was installed in 1987.

✎ noteThe chunk is about the west crane — the collection never mentions the east crane at all. But the label said use this, so the model obeyed: it treated an irrelevant passage as mandatory evidence and bent it to fit the question. The search inserted the wrong passage, and the label said "answer from it anyway."
Example 3b · "Possibly relevant"
user

When was the harbor's east crane installed?

····················································· The following was retrieved automatically and is possibly relevant — ignore it if it does not answer the question: [chunk] "…the west crane, installed 1987, remains the harbor's primary heavy-lift asset…" ·····················································

assistant

The retrieved passage covers the west crane (1987); I don't have information about the east crane's installation. If you can point me to harbor infrastructure records, I can check there.

✎ noteSame chunk, same model, same question. The label changed the chunk's status — from orders to evidence — and the agent was free to notice the mismatch. Inserted text should be offered the way a careful colleague offers a source: "this may help," never "this is the answer."

The labeling rule

Inserted text arrives in the user's message, with the power
to instruct. Label it as EVIDENCE, not ORDERS:

  "possibly relevant · retrieved automatically · ignore freely"

And remember what else rides in on inserted text: anything
anyone ever wrote into the collection. A stranger's words,
delivered to the seat that steers.

Inserted for the agent, or fetched by it

So which is right — text inserted automatically before the model runs, or the agent fetching for itself with a tool call? Put them side by side:

Two ways context arrives

who decidesRAG — the code, before the model speaks · agent-directed — the agent, mid-conversation
costRAG — one search, no extra loop iterations · agent-directed — a tool call and a loop iteration per fetch
reacts toRAG — the task as first stated · agent-directed — everything learned so far; it can refine, follow up, search again
fails byRAG — inserting the wrong context, invisibly · agent-directed — not knowing the collection exists, or not bothering to look

Neither wins outright, and production systems almost always mix them: insert the plausible; let the agent fetch the rest. The prompt starts with the obvious passages, and search tools stay available for when what was inserted turns out to be wrong or thin.

The budget principle

Retrieval before the question is a BET.
Retrieval after the question is a PURCHASE.
Bets are cheap and sometimes wrong; purchases are exact and
cost iterations. Budget for both — and give the agent a way
to notice when the bet missed.