Agentic AI · L-RAG
Knowledge the model was not trained on: what retrieval repairs and what it breaks, the three stages of the pipeline, how the pipeline is measured, and the decision to let the agent retrieve for itself.
Prerequisite: the recap Linear Algebra.
Retrieval-augmented generation (RAG) supplies a language model with passages retrieved from an external document collection, so that an answer rests on knowledge that is not stored in the weights. Part I states which failures of a language model retrieval repairs and which failure it creates, Part II how documents are cut, embedded and indexed, Part III how a query is matched against a chunk, Part IV how $k$ retrieved passages become one answer, Part V how the pipeline is measured, and Part VI whether the agent itself decides when to retrieve. Ingestion, retrieval and synthesis are the three stages of the pipeline, in the order in which a document passes through them:
The question this part answers: why retrieve at all?
A language model's knowledge is parametric — it lives in the weights, and it is fixed at the moment training ends. Four consequences follow from that single fact. The model cannot hold what was private to an organisation. It cannot hold what happened after training stopped. It holds part of what it was taught in one direction only. And whatever it does hold, it holds without a source: no part of the weights records which document a fact came from.
Retrieval answers all four in the same way. The knowledge is moved out of the weights and into the context, where changing it costs an edit to a file rather than a training run, where the documents never leave the organisation that owns them, and where every sentence of the answer has a passage it can be attributed to. What that buys, what it does not buy, and what it breaks are the subject of this part.
The weights are a store of finite capacity, and the training objective fills it in proportion to how often a fact appears. A fact met once contributes one term to the loss and competes for the same parameters as everything else, so the rare, the recent and the private are exactly what is held weakly or not at all.
Which of a model's mistakes can a document fix? Hallucination is one word for several different failures, and the answer differs for each, so the word has to be split before the question can be asked properly. The literature splits it along two independent axes.
Axis 1 — what the output conflicts with (Huang et al., 2025).
| Factuality hallucination | Faithfulness hallucination | |
|---|---|---|
| Conflicts with | the world | the material supplied in the prompt |
| Example | asked who the sixteenth President of the United States was, the model answers Thomas Jefferson | given a report stating "the test was negative for diabetes", the model summarises it as "the patient was diagnosed with diabetes" |
| Cause | the knowledge in the weights is wrong, outdated or absent | the model departed from a source that was in front of it |
Axis 2 — whether the error is stable. Farquhar et al. (2024) reserve the term confabulation for a claim that is "both wrong and arbitrary — by which we mean that the answer is sensitive to irrelevant details such as random seed", and separate it from an error the model repeats.
| Confabulation (arbitrary) | Consistent error (stable) | |
|---|---|---|
| Re-run the same prompt | a different wrong answer each time | the same wrong answer every time |
| What it indicates | the model holds no belief about the question and is guessing | the wrong belief is in the weights |
The axes are independent — a factuality error may be arbitrary or stable — and it is the second that decides whether an index helps. Where the model is arbitrary it holds no belief, and the retrieved passage fills a vacuum. Where the model is consistent the passage must overrule what the weights already contain: a contest between what was trained and what was supplied in the prompt, and one whose winner is measured rather than assumed.
Which repair applies is decided by the pair of values, not by either axis alone.
| confabulation (arbitrary) | consistent error (stable) | |
|---|---|---|
| factuality hallucination — conflicts with the world | the weights hold no belief; the supplied passage fills a vacuum | the weights hold a wrong belief; the supplied passage must overrule it |
| faithfulness hallucination — conflicts with the material supplied in the prompt | the passage is in the prompt and is used unreliably: a different departure from it each run | the passage is in the prompt and is departed from the same way every run |
Supplying a document is a repair by itself only where a factuality error is arbitrary. Where it is consistent, the passage has to win a contest against the weights; and the faithfulness row has no entries at all until a passage has been supplied.
Retrieval repairs what the weights do not hold; it does not settle what they hold wrongly, and it creates one failure an ungrounded model cannot have.
| Failure | Repaired by retrieval? | Why |
|---|---|---|
| nothing in the weights — never learned, or never available to training | yes — the case the architecture was built for | swapping the index swaps what the system knows (Lewis et al., 2020) |
| stale fact — true when training stopped | yes, by the same mechanism | the edit is to a file, not to a model |
| absent in one direction — the reversal curse | yes, and no other repair here does | a fact learned one way is barely answerable the other (Berglund et al., 2024) |
| a systematically wrong belief | only if the passage overrules the weights | which side wins is measured, not assumed (Chen, J. et al., 2024) |
| context inconsistency — the answer contradicts the passage it cites | no — retrieval creates this category | support for each claim has to be asked for and graded (Asai et al., 2024) |
Knowledge and reasoning are separated, and only one of them is expensive to change. A new document is indexed in minutes; the same fact written into the weights costs a training run. The model is then kept for what it is good at — reading and reasoning over text placed in front of it — and the facts are kept where facts can be edited.
RAG is two steps: retrieve, then generate. A retriever selects passages from a corpus of your choosing, those passages are placed into the prompt, and the model answers from them instead of from its training data alone. Embeddings (→ L-Embed) make the first step possible: they place meaning in $\mathbb{R}^d$, so relevance becomes nearest neighbour by cosine.
RAG was named and formalized by Lewis et al. (2020) through a core framing that continues to define modern architecture: a system operating with two distinct memory types, only one of which is cheap and fast to update.
| Where it lives | How it is changed | Cost of a change | |
|---|---|---|---|
| parametric memory | the weights | a training run | GPU-months |
| non-parametric memory | an index over documents | editing a file | minutes |
"We can update RAG's world knowledge by simply replacing its non-parametric memory." > — Lewis et al. (2020), §4.5
What this lecture builds is the simpler, later form. Lewis et al. fine-tuned the retriever and the generator together; what is built today prepends the retrieved documents to the input and changes nothing about the model. Ram et al. (2023) name that form the in-context RALM — retrieval-augmented language model — and measure what it buys: with an off-the-shelf BM25 retriever a 345 M-parameter model outperforms a 762 M one, and with a trained re-ranker a 1.5 B one. Retrieval buys the equivalent of a two- to fourfold increase in parameters, and it costs no training at all.
Retrieval is not the only answer to "the model does not know this." Where the knowledge fits comfortably into the context window, it is placed there directly and no index is built. What decides the threshold is not the size of the corpus but the context budget, which has four terms:
A concrete anchor. A forty-page product handbook is roughly 25,000 tokens and fits in a 128,000-token window with room to spare, so it is placed in the context and no index is built. Fifty thousand support tickets are roughly 25 million tokens and will never fit. Between those two extremes lies the interesting case, and it is decided by the four terms above rather than by the word large.
The consequence is a spectrum rather than a switch:
A second boundary, independent of size. The four terms above decide whether the corpus fits. They say nothing about a class of question that top-$k$ retrieval cannot serve at any $k$: "what are the main themes in this collection?". Similarity retrieval assumes the answer is localised — contained in a few records, with the rest irrelevant. Here every record contributes, so a retrieved subset is not a shorter answer but a different one, and enlarging $k$ does not repair it. Edge et al. (2025) classify such a question as query-focused summarization rather than retrieval, and answer it by summarising the corpus ahead of the query instead of ranking passages after it. The failure when a pipeline does not notice the difference is worth naming: every sentence of the answer may be supported by a retrieved passage, and the answer still misrepresents the corpus.
Retrieval is justified; what remains is its shape. A retrieval pipeline is three stages, see e.g. (Gao et al., 2024).
Stages 1 and 2 together are the retriever, which maps a query to relevant chunks; the model in stage 3 is the generator. Retriever + generator = RAG (Lewis et al., 2020). Retrievers are dense (embeddings and approximate search), sparse (term matching) or hybrid, and are often refined by a re-ranker.
The two halves of the diagram are paid at different rates, and that governs every design choice in this lecture.
The third stage joins the retrieved text to the model through the prompt — the only one of several possible joining points that requires no access to the weights, which is why this course teaches it.
A production pipeline rarely retrieves by embedding alone. The same chunk is written into two indexes — a vector index over its embedding and a term index over its words — both are searched with the query, and the two rankings are combined into one.
The second retriever is lexical, also called sparse or term-based: it scores a passage by the words it shares with the query, weighting each word by how rare that word is in the corpus. TF-IDF is the classical weighting and BM25 its standard refinement; both are derived later in Lexical Retrieval: TF-IDF and BM25. Two properties matter for the diagram. The lexical side needs no model at all — an inverted index and a formula — and it fails on different queries than the dense side does: it matches a part number exactly and misses a paraphrase, where the embedding does the reverse.
The rest of the lecture takes this diagram apart: what a chunk is and where it is cut, what is embedded, what the two indexes are as data structures, how one ranking is made from two, and what the generator does with the passages once it has them.
The index decides what retrieval can possibly return, and every choice that fixes it is made before a single query arrives. The stage decomposes into seven steps:
Three of these seven steps involve permanent, unrecoverable decisions:
A mistake in any of these three stages creates a silent failure downstream: the correct answer simply disappears from the search space rather than returning an incorrect result, making it far harder to detect. Steps 6 and 7 (indexing and storage) shift to a classic data-structures problem, which brings this section to a close.
A chunk is a smaller, self-contained snippet of text extracted from a larger document. Instead of feeding a 100-page PDF into a system all at once, the system breaks the document down into bite-sized pieces—typically ranging from a few sentences to a couple of paragraphs.
A chunk is two things at once, and they want opposite sizes:
One example, both directions. A one-sentence chunk reading "It must be filed within that period." is a precise retrieval unit and a useless reading unit: retrieved on its own it tells the generator neither what must be filed nor which period. A whole-chapter chunk is the reverse — it certainly contains the answer, but its single vector is an average over thirty pages, so it sits near nothing in particular. Every named chunking policy is an answer to that tension, and most of them work by making the two units different.
| Policy | Retrieval unit (what is indexed and matched) | Reading unit (what the generator receives) | How it works, and what it is for | Primary source |
|---|---|---|---|---|
| Fixed size (with overlap) | Fixed token block | The same block | Baseline. Splits text by fixed token count with boundary overlap. Cheap and simple, but ignores document structure. | — |
| Structure-aware | Natural section / heading | The same section | Markup documents. Splits along syntax boundaries (Markdown, HTML, JSON) to keep functions or sections whole. | — |
| Similarity-based (semantic) | Topic segment | The same segment | Unstructured prose. Splits text where vector similarity between adjacent passages drops; often inaccurate in practice. | Lu et al. (2025) |
| Sentence-window | Single sentence / short span | Target span + surrounding sentences | Precise fact retrieval. Matches an exact, narrow sentence and passes neighboring context to the generator. | — |
| Parent–child (small-to-big) | Small child chunk | Enclosing parent section / page | Granular matching with wide context. Matches small text snippets while retrieving their larger parent section. | — |
| Summary tree (RAPTOR) | Any tree node (leaf or summary) | That specific node | Thematic & multi-doc QA. Recursively clusters and summarizes chunks into a multi-layer tree, searching across all levels simultaneously. | Sarthi et al. (2024) |
| Auto-merge | Leaf chunks in a hierarchy | An ancestor chosen per query | Variable-depth queries. Dynamically replaces retrieved sibling chunks with their parent section if enough child nodes match. | Lu et al. (2025) |
Citability. Only the last two rows have a reachable primary source. Fixed size, structure-aware, sentence-window and parent–child are described in the practitioner literature without a citation, an experiment or a dataset, and the survey that names them is a pointer source rather than a citable one.
In the common case a chunk is a few hundred tokens of contiguous text with a small overlap into its neighbours, so that a sentence cut by a boundary still appears whole in one of the two pieces.
This section nevertheless recommends no chunk size, and the reason is measurable. In the standard document-question-answering collections the evidence for a question occupies only 1.7 to 10.5 sentences of a very long document, so any chunker that does not cut through that one sentence returns it and the end-to-end score barely moves: 64.50 for fixed-size chunking against 65.53 for the best method the same authors could build (Lu et al., 2025). A benchmark can only measure a component whose failure changes the answer.
A chunk is stored together with the fields that say where it came from — title, author, date, source URL or file path, section heading, page number, and whatever the corpus itself carries: product, language, customer, access level. They are extracted once the document has been cut and before the chunk is embedded, and they are kept beside the vector rather than inside it. Two uses follow, and only one of them touches the score.
Filtering restricts the candidate set before anything is ranked. A question about the current price list is run only against the chunks whose validity range covers today; a question from a user without clearance is run only against the chunks that user may read. Passages that would mislead, or that must not be shown, are then not candidates at all — a guarantee no amount of ranking quality provides. Gao et al. (2024) count metadata among the pre-retrieval optimisations, beside index granularity and query rewriting.
Every policy above answers the question what is returned once a chunk matches? Chunk augmentation answers a different one: what is matched against? Before indexing, a model that has been shown the whole document writes fifty to a hundred tokens situating each chunk, and the augmented text — not the original — is what is embedded and what enters the term index (Contextual Retrieval = Contextual Embeddings + Contextual BM25; Ford, 2024). The indexed string is then no longer a substring of the document.
The model is called once per chunk and is shown the whole document each time, so the cost falls entirely in the offline half of the pipeline, where it is paid once per document instead of once per query. Both indexes are then built over the augmented text — which is why one name covers two methods, contextual embeddings and contextual BM25.
It is the member of the family that reaches the chunk that is unidentifiable rather than too small, and one example carries the whole idea:
| The string | |
|---|---|
| the chunk | The company's revenue grew by 3 % over the previous quarter. |
| what is indexed | This chunk is from an SEC filing on ACME Corp's performance in Q2 2023; the previous quarter's revenue was $314 million. The company's revenue grew by 3 % over the previous quarter. |
The italic half is what the model wrote; the rest is the chunk, unchanged. On its own the chunk names neither the company nor the quarter, so no term index matches a query about ACME in 2023 and no embedder places the chunk near such a query. Returning its parent would not help either: a parent is returned only if the child was retrieved, and it was not. The repair has to happen on the indexed string.
Reported on one corpus, the proportion of questions whose evidence is missing from the top twenty falls from 5.7 % to 2.9 % when both indexes are contextualised, and to 1.9 % with a re-ranker over 150 candidates. ⚠ Anthropic's own experiment (Ford, 2024), whose methodology is not published; the mechanism is established, the magnitudes are indicative.
How to implement Contextual Retrieval in practice, see e.g. in the Enhancing RAG with contextual retrieval cookbook from Antrophic.
The index can only rank what the embedder separated, so retrieval quality is decided one step before the index is built. A text embedder is trained contrastively — the matching document must out-score the others in the batch — so what the model calls similar is whatever its training pairs called similar, and none of that is visible from the vector store afterwards.
Contrastive training, concretely. A training example is a triple: an anchor, a positive that should land near it, and one or more negatives that should not.
The loss is InfoNCE (van den Oord et al., 2018): a softmax over the scores of all candidates, in which the positive is the class to be predicted. Minimising it raises the anchor's score against the positive relative to every negative — so similar means what the training pairs called similar, and nothing else. The negatives are usually the other examples of the same batch, which makes the batch size part of the objective rather than a property of the hardware.
What that means concretely. Two embedders may disagree about whether "How do I cancel my subscription?" is nearer to "Termination of contract"—the same intent, different words—or to "How do I cancel my newspaper delivery?"—the same words, a different subject. Neither answer is wrong in general; each reflects its training data, and only one will fit a given corpus.
The case in one table. On short texts, models perform similarly, but context length shifts relative model performance:
| Model | Size | MTEB (Short text, 512 tokens) | LoCo (Long text, 8192 tokens) |
|---|---|---|---|
text-embedding-ada-002 (OpenAI) |
Proprietary | 60.99 | 52.70 |
| Nomic Embed | 137 M | 62.39 | 85.53 |
On MTEB (truncated to 512 tokens), text-embedding-ada-002 scores 60.99—a respectable mid-field result. On LoCo (evaluated at 8192 tokens), its score drops to 52.7, while Nomic Embed—a 137M open model—outperforms Ada on MTEB at 62.4 and reaches 85.5 on LoCo (Nussbaum et al., 2025). Model rankings established on short-context benchmarks do not survive when documents get long.
One embedder places two kinds of text, and nearest does not mean the same thing for both. Asked which text is closest to "What is the capital of France?", a similarity task answers "What is the name of the capital city of France?" — a rephrasal — and a question-answering task answers "Paris is the capital of France." Both are right, and a single model trained on both kinds of pair therefore "receives conflicting reward signal" (Nussbaum et al., 2025, §4.2.2): one batch pushes questions toward questions, the next pushes questions toward answers.
The repair is to make the task an input. A short string is prepended to the text before it is embedded, telling the model which relation to encode:
| Prefix | Applied to | The relation it selects |
|---|---|---|
search_query: |
the query only | retrieval, question side |
search_document: |
the passage only | retrieval, passage side |
classification: · clustering: |
both sides | similarity between texts of the same kind |
The first two are asymmetric — the sides are structurally unlike, a short interrogative against a long declarative passage, and are tagged differently. The last two are symmetric. The strings themselves are one model's interface and not a standard: another embedder has other prefixes, a free-text instruction, or none.
Omitting a prefix is a silent failure. It is ordinary text, so nothing raises an error: a vector comes back, the search returns its nearest neighbours, and they are the neighbours of a point placed under the wrong relation. The loss shows up only as recall the corpus never had. And because the prefix is part of every stored string, changing it means re-embedding the corpus rather than re-running the query.
Removing the asymmetry at query time — hypothetical document embeddings (HyDE). A task prefix tells the embedder which relation to encode. Gao, L. et al. (2023) remove the need for a query-side representation instead. An instruction-following language model is asked to "write a passage to answer the question"; the same encoder $f$ that embedded the corpus embeds that hypothetical passage, and the mean over $N$ sampled passages $\hat d_1, \dots, \hat d_N$ becomes the query vector:
$$\hat{\mathbf{v}}_q = \frac{1}{N}\sum_{k=1}^{N} f(\hat d_k).$$
Retrieval has become passage-to-passage similarity — a symmetric task, which an encoder trained without relevance labels already performs.
The hypothetical passage may be factually wrong, and the design does not depend on it being right: it serves only as a search key and is discarded after the search. The fixed-size vector keeps the vocabulary and the shape of an answer and loses most of its details; the authors describe the encoder as "a lossy compressor" in exactly this role.
Measured on the TREC Deep Learning 2019 web-search queries with nDCG@10, a rank-weighted score over the top ten results: the unsupervised encoder alone reaches 44.5, with HyDE 61.3 — level with the same encoder fine-tuned on labelled pairs (62.1). The gain depends on the generator: 48.9 with an 11-billion-parameter model, 61.3 with InstructGPT. The cost is one generation call before every retrieval.
Exact search (FLAT) compares a query against all $N$ vectors ($O(Nd)$ cost), which only scales up to a few thousand items. To scale to millions of vectors, Approximate Nearest Neighbor (ANN) algorithms trade a small amount of recall for drastically reduced search latency.
Every ANN method works by searching only a target subset of the dataset:
nprobe: how many of those cells a single query actually searches).efSearch the size of the candidate list HNSW keeps while it searches ).Retrieval is the selection of a few passages from a large collection, given a query. Two components perform it.
Three practical principles follow from how scoring and ranking work in practice:
Semantic retrieval uses dense embeddings at query time to fetch contextually relevant passages. This dense half of the pipeline consists of three distinct operations, though only the first runs in real time once a user query arrives:
Notice what is absent at inference time: no passage computation happens on the fly. Passage embeddings are computed once during offline ingestion, imposing the core constraint of scalable semantic search: a dual-encoder architecture can only scale if the corpus is fully embedded before the query ever exists.
The bi-encoder, also called a dual encoder: query and passage pass through the same embedding model separately, a pooling step (by default the mean over the token vectors) reduces each to one vector in $\mathbb{R}^d$, and the two meet only in the cosine (Reimers & Gurevych, 2019). The passage branch is computed offline, once per chunk; only the query branch is computed when the query arrives.
No query token ever attends to a passage token: everything the model knows about the pair is contained in two vectors computed without knowledge of each other.
Where dense retrieval embeds text into continuous vector spaces, lexical (or sparse) retrieval scores passages purely by exact word overlap, requiring no neural models (Jurafsky & Martin, 2026, §11.1).
TF-IDF measures the importance of a term $t$ within a document $d$:
$$\text{tf-idf}(t,d) = \underbrace{\text{tf}(t,d)}_{\text{frequent in }d} \times \underbrace{\log\frac{N}{\text{df}(t)}}_{\text{rare across corpus (idf)}}$$
where $\text{df}(t)$ is the number of documents containing $t$, and $N$ is the total document count. A term scores high when it is frequent in a specific document but rare across the corpus. Both queries and documents are represented as high-dimensional, sparse vectors across the vocabulary and ranked by their dot product (Spärck Jones, 1972).
In a corpus of $N = 1{,}000{,}000$ documents using natural logarithms:
Ubiquitous words like "the" receive $\text{idf} \approx 0$, effectively filtering them out automatically without needing manual stop-word lists.
The High-Level Idea: While standard TF-IDF dampens term frequency logarithmically, its score can still grow indefinitely with high word counts. BM25 replaces unbounded growth with a strict mathematical ceiling: no matter how many times a keyword appears in a document, its term frequency score can never exceed a hard upper limit ($k_1 + 1$).
BM25 (Robertson & Zaragoza, 2009) refines standard TF-IDF through two critical adjustments:
It remains the standard lexical baseline in production search architectures.
The BM25 ranking function. For a query $q$ and a document $d$, BM25 adds one weight for every query term $t$ (Robertson & Zaragoza, 2009, Eq. (3.15), in the common variant with $(k_1+1)$ in the numerator, which multiplies every term alike and therefore leaves the ranking unchanged):
$$\mathrm{BM25}(q,d)\;=\;\sum_{t\,\in\,q}\ \mathrm{idf}(t)\cdot \frac{f(t,d)\,\bigl(k_1+1\bigr)}{f(t,d)\;+\;k_1\!\left(1-b+b\,\dfrac{|d|}{\overline{|d|}}\right)}$$
where $f(t,d)$ is the raw count of term $t$ in $d$, $|d|$ the length of $d$ in terms, $\overline{|d|}$ the average document length in the corpus, and $k_1>0$, $b\in[0,1]$ the two parameters (commonly $k_1\approx1.2$–$2.0$ and $b\approx0.75$ — conventions of the implementations, not constants of the model). The $\mathrm{idf}$ factor is the Robertson/Spärck Jones weight in the case of no relevance judgements, "a close approximation to classical idf":
$$\mathrm{idf}(t)\;=\;\log\frac{N-\mathrm{df}(t)+0.5}{\mathrm{df}(t)+0.5}$$
with $N$ and $\mathrm{df}(t)$ as in TF-IDF above.
Where the two refinements act. Saturation: with $b=0$ the fraction is $f(k_1+1)/(f+k_1)$, which increases with $f$ and tends to $k_1+1$ as $f\to\infty$ — the ceiling named on the slide. Length normalisation: the term $b\,|d|/\overline{|d|}$ enlarges the denominator for a document longer than average, so the same count $f$ earns a smaller weight; $b=0$ ignores length, $b=1$ normalises it fully. A query term absent from $d$ has $f(t,d)=0$ and contributes nothing, so the sum only needs the documents listed under the query's terms — the postings an inverted index stores.
Lexical retrievers tokenize text into words or stems (applying lowercasing, stop-word removal, and stemming/lemmatization), rather than using the generator's sub-word Byte-Pair Encoding (BPE) tokens. Meanwhile, dense retrievers rely on their own specific embedding model's vocabulary. Consequently, the term "token" carries entirely different structural definitions across different stages of the retrieval pipeline.
| Sparse (term-based: BM25 / TF-IDF) | Dense (embedding-based) | |
|---|---|---|
| Query cost | tokenize + inverted-index lookup — cheap, no model call | embed the query (a model forward pass) + ANN search |
| Index / memory | inverted index; small; exact postings | vector index (HNSW/IVF) + $d$-dimensional float vectors — more memory; ANN is approximate |
| Match type | exact lexical (keyword overlap) | semantic (meaning) |
| Strong on | rare terms, identifiers, code, exact phrases; interpretable | synonyms, paraphrase, cross-lingual |
| Weak on | vocabulary mismatch — a query for "car" misses a document saying "automobile" | exact and rare tokens — an error code ORA-01555 or a part number is mapped near every other code |
Measured over the same 21 M Wikipedia passages (Karpukhin et al., 2020): top-20 retrieval accuracy 78.4 % (dense) against 59.1 % (BM25) on Natural Questions — real, anonymised Google queries paired with a Wikipedia page, annotated with a long answer, a short answer, or null where the page answers neither (Kwiatkowski et al., 2019) — at 995 against 23.7 queries per second — but 8.5 h to build the vector index against about 30 min for the inverted one. Dense is the faster to query and by far the slower to build.
Sparse gives high precision where the overlap is lexical, dense higher recall on meaning, and hybrid retrieval — fusing the two rankings, optionally followed by a re-ranker — tends to beat either alone.
A hybrid retriever runs a lexical and a dense retriever over the same corpus and combines the two rankings. It combines the ranks, not the scores, because the two numbers are calibrated against different things: a BM25 score moves with the corpus and with the rarity of the query's terms, while a cosine similarity lies in $[-1,1]$ and, for a trained embedder, is concentrated in a narrow band. Adding them requires a normalisation, and every normalisation is a parameter that does not transfer between corpora, query types or embedder versions.
Reciprocal rank fusion (Cormack et al., 2009) needs none. For a document $d$ and a set $R$ of rankings, each assigning $d$ the rank $r(d)$:
$$\mathrm{RRF}(d) = \sum_{r \in R} \frac{1}{k + r(d)}, \qquad k = 60 .$$
Documents are then sorted by that score: no training, no calibration, no per-corpus tuning. The reciprocal shape keeps lower ranks from vanishing as an exponential would, and the constant $k$ "mitigates the impact of high rankings by outlier systems" (Cormack et al., 2009).
Two documents, worked out. Document $A$ is placed 3rd by the lexical retriever and 3rd by the dense one. Document $B$ is placed 1st by the dense retriever and is missing from the lexical ranking entirely:
$$\mathrm{RRF}(A) = \tfrac{1}{63} + \tfrac{1}{63} \approx 0.0317, \qquad \mathrm{RRF}(B) = \tfrac{1}{61} \approx 0.0164 .$$
$A$ wins, although no retriever ever ranked it first. That is the point of fusing at all: agreement between two different failure modes is evidence, and one confident system is not.
The returned unit — what is indexed and scored need not be what the generator reads:
| Variant | Indexed and scored | Returned to the generator |
|---|---|---|
| fixed chunk (the base case) | the chunk | the same chunk |
| sentence-window | single sentences | the matched sentence together with its neighbours |
| auto-merge | the leaves of a chunk tree | the leaves, except that retrieved siblings are replaced by their parent once enough of that parent has been matched (Lu et al., 2025) |
The ranking — which candidates are considered, and in which order:
| Variant | The step it changes |
|---|---|
| single ranking (the base case) | none: one retriever scores the whole index |
| metadata filter | the candidate set — only chunks whose metadata matches (a date range, a source, an access level) are ranked at all |
| hybrid | the ranking — a lexical and a dense ranking are fused by rank, as above |
| re-ranked | the order — a larger shortlist is re-scored by a more expensive model, below |
Once the returned units differ in size, top-$k$ stops being a comparable setting. The retrieval parameter is then a token budget, of which top-$k$ is the special case in which every chunk has the same length. It is also why merging pays only where there is more than one granularity to choose between: over a single-level hierarchy the merged unit is the whole section, and the system degrades.
A cross-encoder reads the query and the passage in one sequence, so attention runs across both and every passage token is represented in the presence of the query. It can therefore encode this passage answers this question rather than merely this passage is about this topic, which is why it scores better than any retriever above — and why nothing about the passage can be computed before the query is known. It is exactly the model the decomposability constraint excludes. So it is used wherever a small candidate set makes it affordable: as a re-ranker over the shortlist a fast retriever returned, and offline as the teacher from which fast retrievers are distilled.
The cross-encoder The cross-encoder processes the query and passage together as a single concatenated input sequence (Devlin et al., 2019). A classification head scores full cross-attention interaction directly. Because representations cannot be pre-computed offline, the model must run real-time inference across every candidate pair on the shortlist.
The contrast with the bi-encoder is the position at which query and passage meet: there in one cosine between two finished vectors, here in every attention layer.
A re-ranker cannot exceed the pool it is given, and that ceiling is measurable. Re-ranking the first thousand results of a lexical retriever, a far stronger neural retriever reaches recall@1000 of 81.4 — which is exactly the lexical retriever's own recall@1000, since re-ranking only re-orders that pool. Removing the lexical stage and searching the whole collection lifts the same model to 96.8, and improves the top of the ranking as well, because it reaches documents "missed entirely" by the first stage (Khattab & Zaharia, 2020). Two stages buy latency, and they cost recall.
The passages have been retrieved. They are not yet an answer.
Synthesis is the downstream half of RAG: taking the retrieved passages and transforming them into a single coherent, grounded response. Perfect retrieval only supplies raw materials; synthesis governs how context is ordered inside the prompt, how conflicting evidence is resolved, and how strict generation boundaries prevent hallucinations.
Where a passage sits in the prompt changes whether it is used. Holding content fixed and varying only its position, accuracy follows a U-shaped curve: performance peaks when key information is placed at the very beginning or end, but plummets in the middle—sometimes dropping below closed-book baselines where providing documents performs worse than providing none at all Liu et al., 2024. Context layout is therefore an explicit ordering decision, and top-ranked passages belong at the edges rather than in the center.
Grounding is not verification, and the two are easy to confuse.
Given documents whose answers had been deliberately falsified, and a system instruction warning that documents may contain errors, accuracy falls from 91 % without documents to 17 % with them, with the error detected in 1–8 % of cases (Chen, J. et al., 2024). The pipeline contains no step that compares the corpus against anything.
RAG models follow retrieved context blindly rather than fact-checking it.
Standard retrieval and generation do not automatically verify citations: Citation precision asks whether every claim in the answer is supported by the passage it cites. Retrieval-augmented instruction-tuned models left to themselves reach 2–6 % on long-form answers, where a system that explicitly judges support for each segment reaches 66.9–70.3 % (Asai et al., 2024). The judgement is a separate step; retrieval does not perform it.
To ensure reliable attribution, your RAG pipeline must treat attribution evaluation as an explicit, post-generation judging step.
Retrieval returns passages; the answer is built from them, and how it is built is a decision of its own with three standard forms. Write $k$ for the number of passages and $b$ for how many of them fit in one prompt.
| Strategy | Mechanism | Model calls | Also called | Where it wins |
|---|---|---|---|---|
| single pass | all $k$ passages in one prompt | 1 | compact |
fast and cheapest; the sensible default |
| iterative refinement | passage 1 → initial answer → passage 2 → refined answer → … → final answer | $k$, strictly sequential | refine |
the most nuanced answer, at $k$ latencies |
| hierarchical merging | summarise the passages in groups, then merge the summaries, recursively | $\lceil k/b \rceil$ per round over $O(\log_b k)$ rounds, parallelisable | tree_summarize |
long-form summarisation over many passages |
What each strategy carries forward is what separates them. The single pass carries nothing and is the only form in which attention can compare two passages directly. Refinement carries the answer: every passage after the first is shown the answer so far and rewrites it, so the result is order-dependent and the last passage has the most influence over the wording. Merging carries intermediate summaries: each group is compressed once and the compressions are combined, so no passage ever sees another passage's text.
And the arithmetic, for $k = 20$ passages and $b = 5$ per prompt. The single pass makes 1 call. Refinement makes 20, strictly one after another. Merging makes $\lceil 20/5 \rceil = 4$ calls in the first round and $\lceil 4/5 \rceil = 1$ in the second — 5 calls in 2 rounds, and the four calls of a round run in parallel. The total work of merging is therefore linear in $k$ while its depth is logarithmic, and only depth is unavoidably sequential.
The recursion may be paid once instead of once per query. RAPTOR (Sarthi et al., 2024) runs the same cluster-and-summarise recursion over the corpus at indexing time — the summary tree of the chunking table — so that the merged summaries are retrievable chunks rather than work done per query. Restricting retrieval to any single layer of that tree scores about 57.9 on one document and allowing three layers 73.68: the summaries answer thematic questions no leaf contains, and the leaves answer the detail the summaries have compressed away.
Three stages of design decisions have now been made, and none of them can be settled by argument.
Retrieval is measured separately from generation, and it is measured first: a better generator or an agentic loop placed around a retriever that never returned the evidence inherits the miss, and no amount of work on the second stage recovers it. Separate measurement is what tells the two apart — and it is also what decides the question of the final part.
Measure retrieval before improving it. The retrieval stage is measured on its own, against a set $R$ of gold passages marked relevant for a query by hand. Write $T_k$ for the top $k$ the retriever returned:
$$\text{recall@}k = \frac{|R \cap T_k|}{|R|}, \qquad \text{precision@}k = \frac{|R \cap T_k|}{k}.$$
Worked. Four passages in the corpus answer the query, and the retriever's top 10 contains three of them. Then $\text{recall@}10 = 3/4 = 0.75$ and $\text{precision@}10 = 3/10 = 0.30$.
The two answer different questions, and recall@$k$ is the one that bounds everything downstream: it asks whether the evidence reached the generator at all, and nothing later in the pipeline can recover a passage that was never returned. Precision@$k$ asks how much of the context window was spent on passages that were not needed.
Three further scores measure the pipeline end to end — the triad of Es et al. (2024), collected in Gao et al., 2024, §VI.C. A query produces three objects — the query itself, the retrieved context, the response — and the three scores are the three edges between them:
| Score | The question it asks | Compares | What it targets |
|---|---|---|---|
| Context relevance | is the retrieved context precise and specific for this query? | query → context | retrieval |
| Answer faithfulness | does the answer stay inside the retrieved context? | context → response | generation |
| Answer relevance | does the answer address the question that was asked? | response → query | generation |
Faithfulness and relevance are not the same score, and an example separates them. Asked "how long is the refund window?" over a context stating "14 days", the answer "30 days" is perfectly relevant and flatly unfaithful; the answer "our support team is available 24/7" is faithful to nothing in particular and not relevant. Only faithfulness is checkable against something outside the model, which is why citation precision — is every claim supported by the passage it cites? — is the form in which faithfulness is usually made concrete.
Faithfulness is the share of the answer's statements that the retrieved context supports. Es et al. (2024) compute it, like the two scores that follow, by prompting a model, and all three are reference-free: they use the question $q$, the retrieved context $c(q)$ and the answer $a_s(q)$, never a gold answer. Faithfulness takes two prompts:
$$F = \frac{|V|}{|S|} \in [0, 1].$$
Worked. The context states "Refunds are accepted within 14 days of delivery." The answer "Refunds are accepted within 14 days, and the customer pays the return shipping." decomposes into $s_1$ = refunds are accepted within 14 days and $s_2$ = the customer pays the return shipping. The context supports $s_1$ and says nothing about $s_2$, so $V = \{s_1\}$ and $F = 1/2$.
An unsupported statement lowers the score because it enters the denominator $|S|$ without entering the numerator $|V|$ — whether $s_2$ is true does not matter, only whether the context supports it. Since the decomposition is itself a model call, a different split of the same answer changes $|S|$, and $F$ with it: had $s_1$ been split into two statements, $F$ would be $2/3$.
Answer relevance measures whether the answer addresses the question that was asked, and it does so in reverse. From the answer alone, the model generates $n$ questions $q_1, \dots, q_n$ that the answer would answer. Each generated question and the real question $q$ are embedded, and $\mathrm{sim}(q, q_i)$ is the cosine similarity of the two embedding vectors:
$$AR = \frac{1}{n}\sum_{i=1}^{n}\mathrm{sim}(q, q_i).$$
An answer to a different question generates questions close to that other question, so each $\mathrm{sim}(q, q_i)$ falls, and $AR$ with it. The authors state that the score also penalises an answer that is incomplete or contains redundant information; the route is the same, since questions generated from such an answer cover only part of $q$, or cover topics that $q$ does not contain.
The score is blind to factuality by construction, as its authors state. In the refund example, the wrong answer "30 days" generates "how long is the refund window?" as readily as the correct "14 days" does: the generated question depends on what the answer is about, not on whether it is true.
Context relevance is the share of the retrieved context that is needed to answer $q$. The model extracts from $c(q)$, without changing them, the sentences needed to answer $q$ — or returns "Insufficient Information" when there are none — and the score divides their number by the length of the context:
$$CR = \frac{|S_{\text{ext}}|}{\text{number of sentences in } c(q)}.$$
Worked. The retriever returns three passages of 20 sentences in total, and one sentence states the 14-day refund window. Then $|S_{\text{ext}}| = 1$ and $CR = 1/20 = 0.05$, although the context contains the correct answer.
The score is therefore a penalty on length, not on error: every retrieved sentence enters the denominator, and only a needed sentence enters the numerator. The authors motivate the penalty by the cost of long contexts and by the weaker use a model makes of information placed in the middle of a long context (Liu et al., 2024).
All three scores are judgements of a model, so each inherits the biases of a judge; and answer relevance is a cosine between embeddings, which cannot become a factuality score however well it correlates.
The three scores above share one property, and naming it divides evaluation in two.
| Reference-based | Reference-free | |
|---|---|---|
| Compares the answer with | a gold answer written in advance | the question and the retrieved context, both available at run time |
| Instruments | exact match, ROUGE, BERTScore | a model acting as judge — the three scores above |
| Needs | a labelled set per corpus | no labels |
| Fails when | the answer is right in different words | the judge is biased |
Why the left column is not enough. It measures word overlap, not correctness: against the gold answer "14 days", the perfectly correct "two weeks" scores zero on exact match and close to zero on ROUGE. The effect is not a curiosity of that one example — re-running 23 summarization systems under a single protocol against expert human annotations, ROUGE and BERTScore correlate poorly with human judgement (Fabbri et al., 2021). And a corpus that changes weekly never has a current labelled set at all.
Why the right column is not enough either. The score is produced by a model, so it inherits that model's biases. G-Eval gives the judge the criterion, has it write its own evaluation steps, and takes the probability-weighted sum over the score tokens rather than the integer the model emits, reaching a Spearman Correlation Coefficient of 0.514 with human judgement on that same benchmark — ahead of every reference-based metric in the table there (Liu, Y. et al., 2023). Its own authors nevertheless report a "bias towards the LLM-generated texts."
So the two are used together. Report a judged score with a second judge and the disagreement between them, keep a reference-based metric beside it, and treat a difference that only one of the two can see as a hypothesis rather than as a result.
The pipeline built so far fixes three decisions before it has read a single passage: it retrieves for every query, it retrieves with the query as the user wrote it, and it accepts whatever the top $k$ contains. Agentic RAG returns these decisions to run time, where they can depend on what retrieval has already returned.
The survey literature arranges RAG systems on four rungs, which differ in control flow rather than in quality (Gao et al., 2024):
| Rung | What it adds | Shape |
|---|---|---|
| Naive RAG | index → retrieve top-$k$ → generate | a chain |
| Advanced RAG | optimisation around that chain — query rewriting (Ma et al., 2023) and expansion before retrieval, re-ranking and context compression after it | still a chain: one static pass |
| Modular RAG | modules exchanged and re-ordered: routing, memory, repeated retrieval | a graph |
| Agentic RAG | the agent decides the retrieval strategy at run time | a loop |
Most production systems stand on the second rung. It improves every step of the chain, but the steps and their order are still fixed before anything has been read. Whether the fourth rung repays its additional calls is decided by measurement, not by its position on the ladder.
The same four rungs, drawn as the control flow each one executes for a single query (the first three after Singh et al., 2026, Figs. 3–5):
In the first three the path is fixed by the designer of the system, even where it branches or repeats; in the last it is chosen by the model at run time, and the number of retrievals is unknown before the query arrives.
Agentic RAG is retrieval-augmented generation in which an agent, and not a fixed pipeline, decides how to retrieve (Singh et al., 2026).
In the agentic RAG retrieval is a tool inside the agent loop (→ L-ReAct): the model reasons, calls a tool, observes the result, and repeats until it decides to answer. Every retrieval is therefore preceded by a decision, and every result is read before the next decision is made.
The loop makes five decisions that the static pipeline makes once or not at all:
Each return edge in the diagram costs one more retrieval and at least one more model call, so a production loop also carries a stopping criterion — a maximum number of iterations or a token budget — after which it answers from the evidence it holds.
Each of the five decisions is an instance of a pattern the agent literature already names. Singh et al. (2026), §3, build agentic RAG from four such patterns:
| Pattern | Definition | Its role in the retrieval loop |
|---|---|---|
| Tool use | the model calls external functions and reads their results | retrieval, SQL and web search are tools; the source is chosen by choosing the tool |
| Planning | the task is decomposed into sub-tasks before or during execution | a multi-hop question becomes a sequence of sub-questions, one retrieval each |
| Reflection | the model critiques its own intermediate output and revises it | judging the evidence; checking that the answer is supported |
| Multi-agent collaboration | several agents, each with its own role and tools, divide the task | retrieval, judgement and writing are assigned to different agents |
The diagram is the reflection pattern (after Singh et al., 2026, Fig. 8).
What an agent can do after a failed retrieval is bounded by the operations its retrieval tool
offers. A tool with one operation, search(query), permits one response to a poor result:
rephrase the query and search again. An agent that can additionally search lexically, restrict a
search to one named entity, and include or exclude documents from its own candidate set scores
22.5 % higher in exact match, relative, than the strongest agent limited to issuing queries
(Hui et al., 2026). The tool interface is therefore a design
decision of the same weight as the embedder.
The agent knows its retrieval options only from their definitions in its context. Each tool is presented to the model as a name, a natural-language description and a parameter schema; the model selects a source by emitting a call to one of these names with arguments that satisfy the schema, and nothing outside the definitions is visible to it. Whether a question about an order reaches the SQL database rather than the index over the manuals is therefore decided by what the two descriptions say. The definitions belong to what Yang et al. (2024) call the agent–computer interface (ACI): the commands available to the model and the form in which their results are returned to it.
Self-RAG (Asai et al., 2024) is a language model trained to emit reflection tokens — special tokens with which it requests a retrieval, grades a retrieved passage and grades its own output. They encode the three judgements the static pipeline never makes: retrieve at all? · is this passage relevant? · is the answer supported by it? Agentic RAG makes the same judgements with an unmodified model, through prompts and tool calls in the agent loop; Self-RAG moves them into the weights, which requires training the generator.
One trace that the static pipeline cannot produce. The question is "How long is the refund window?", asked of a corpus in which the answer is divided between two documents:
search("refund policy") → §4: "Refunds are governed by the terms in Annex B."
search("Annex B refund window") → Annex B: "14 days from delivery."
answer: 14 days from delivery.
The first result does not answer the question; it names where the answer is. The second query could not have been written in advance, because it contains a term — Annex B — that appears only in the first result. A static pipeline embeds the original question once, retrieves once, and stops.
A fixed-context reader cannot outperform its retriever. It sees exactly the top $k$ chunks it was handed, so "if the embedding search retriever fails to surface the gold article using the provided question, the fixed-context baselines are guaranteed to never see the gold article" (Packer et al., 2024). That is a ceiling, not a tendency: whatever the reader's quality, the evidence it can see is bounded above by recall@$k$, and no amount of prompt work on the reader raises it.
An agent that can query again and page onwards is not bounded that way. A second query with different wording, or the next page of the same ranking, reaches documents the first call never showed. Agentic retrieval therefore converts a one-shot top-$k$ into a search over the index, and the ceiling moves from recall@$k$ to recall over the whole ranking.
Where it matters most is the multi-hop question, in which the value retrieved is itself the key to the next lookup, so a single retrieval cannot succeed by construction. Packer et al. (2024) measure it on a synthetic nested key–value task: 140 pairs of random identifiers (UUIDs), about 8,000 tokens, in which a value may itself be a key, so that $n$ nesting levels require $n+1$ lookups. The identifiers are random, so no model holds them in its weights, and the fixed-context baselines receive all 140 pairs in their context window: the only difficulty is chaining the lookups. GPT-3.5 falls to 0 % accuracy at one nesting level, GPT-4 by three. MemGPT with GPT-4, which looks up each key with a search function call, is unaffected by the nesting depth; with GPT-4 Turbo or GPT-3.5 it improves on the corresponding baseline but still declines from two levels, "as a result of failing to perform enough lookups".
The gain is theoretical until the agent uses it: the same authors observe that their agent "will often stop paging through retriever results before exhausting the retriever database". Agentic RAG buys a higher ceiling, not a guarantee of reaching it.
The architectures differ in where the retrieval decisions are made and how many contexts hold them. One agent may make every decision in one context, or the decisions may be distributed over several agents, each of which sees only its own part (Singh et al., 2026, §5):
| Architecture | Who decides | Typical use |
|---|---|---|
| Single-agent router | one agent selects the source and tool for each query | few sources, one domain |
| Multi-agent | a coordinator delegates to retrieval agents, one per source type, which run in parallel | many heterogeneous sources |
| Hierarchical | agents delegate to agents over several levels; results are aggregated upwards | large tasks that decompose recursively |
| Adaptive | a classifier over the query selects no, single-step or multi-step retrieval | mixed workloads of simple and complex questions |
| Corrective | an evaluator grades the retrieved documents and triggers a new query or another source | corpora with gaps |
| Graph-based | the agent retrieves over a knowledge graph — entities as nodes, typed relations as edges, each fact a triple $\langle$subject, relation, object$\rangle$ — as well as a vector index | questions about relations between entities |
| Agentic document workflow (ADW) | agents parse a document, keep state across steps, retrieve reference material and apply business rules | processing a document — an invoice, a contract — into a structured result |
The last row changes the task rather than the control flow: the input is a document to be processed, not a question to be answered, and the output is a structured record rather than an answer. The term was introduced by a framework vendor (LlamaIndex) and is not yet established in the research literature; the survey's own diagram of it is taken from that vendor's blog.
Agentic RAG buys recall on questions that one retrieval cannot answer, and pays for it in calls, latency and predictability; on questions that one retrieval does answer, it buys nothing.
| Static RAG | Agentic RAG | Mechanism | |
|---|---|---|---|
| Multi-hop questions | bounded by recall@$k$ of one query | a later query may use a term from an earlier result | the retriever ceiling (Packer et al., 2024) |
| Simple questions | sufficient | same answer, more calls | a multi-step loop on every query takes 3.33× the time of single-step retrieval; routing reaches the same F1 at 1.46× (Jeong et al., 2024) |
| Knowledge source | fixed at design time | selected per query | the gain lies in the decision: the web added when the corpus failed beats the web always added (Yan et al., 2024) |
| Cost per query | one retrieval, one model call | one retrieval and at least one model call per iteration | the number of iterations is decided at run time, so the cost varies per query |
| Poor index | bounds the answer | still bounds the answer | the loop chooses only among what the index returns |
| Traceability | one path for every query; a failure reproduces | a different path per query | the trace must be logged and evaluated step by step, not only the final answer (Singh et al., 2026, §10) |
| Termination | after one pass | only by a stopping criterion | the model may stop too early (Packer et al., 2024) or invoke tools excessively (Singh et al., 2026, §10) |
lecture-RAG-exercises.ipynb — by hand: recall@$k$ and precision@$k$; TF-IDF; reciprocal rank
fusion; the curse of dimensionality; an HNSW search and the size of the index it needs.
lecture-RAG-lab.ipynb — build it: a tiny RAG index from scratch, then a vector database behind
a search tool your agent decides to call, an evaluation set of your own, and a sparse/dense/fused
comparison scored against it.
The six questions, answered.
Peer-reviewed papers
[CLS]/[SEP] sentence-pair input and the classification layer on the final [CLS] vector: the cross-encoder.search.text-embedding-ada-002 60.99 → 52.7) that argues
for evaluating the embedder on your own documents.Preprints & other non-peer-reviewed papers
Textbooks & monographs
Blogs, documentation & other web sources
compact, refine, tree_summarize).
(Cited for the vocabulary only; it reports no measurement.)