Level 300: Building with gen AI · Module 300.1

A coworker pulls a folder from a wooden card-catalogue drawer while a colleague holding a coffee cup watches.
Find it, then answer

How retrieval works · Technical gear

Reviewed and published, version 1.0.0. As of 19 Nov 2026. It is not a credential. Results are those of the cited papers, on their benchmarks and models, at their dates.

Objectives. 300.1-a: describe retrieval-augmented generation as a retrieval step over a chosen corpus followed by a generation step conditioned on the retrieved passages. 300.1-b: state what retrieval changes (the generator's input context, which is used unevenly by position) and what it does not guarantee (faithful reading, use of mid-context evidence, or separation of instructions from data). 300.1-c: attribute a wrong answer to retrieval (a required gold passage not retrieved) or to generation (all required gold passages retrieved, answer wrong or unsupported).

1. The two-stage model

Step 1, FIND [s1]: Search the chosen collection. The retriever returns passages relevant to this question. Step 2, WRITE [s1]: Write from the input. The model receives the question and retrieved passages, then writes an answer.
The retriever and generator are distinct parts (s1). Retrieval supplies passages for this question; it does not train the model on the collection.
Figure data as text

Find, then write

  1. FIND [s1] · Search the chosen collection — the retriever returns passages relevant to this question.
  2. WRITE [s1] · Write from the input — the model receives the question and retrieved passages, then writes an answer.

Objective: 300.1-a. Text equivalent of figures/find-then-write.svg.

Lewis et al. (2020) define RAG as a parametric generator combined with a non-parametric memory: a dense vector index of Wikipedia, accessed by a pretrained neural retriever.1 The retriever $p_\eta(z \mid x)$ returns the top-$k$ passages $z$ for input $x$; the generator $p_\theta(y_i \mid x, z, y_{1:i-1})$ produces output tokens conditioned on the input, a passage and the tokens so far.1

Retriever. The retriever is based on Dense Passage Retrieval (DPR), a bi-encoder: a query encoder $q(\cdot)$ and a document encoder $d(\cdot)$, both BERT-based, with1

p_\eta(z \mid x) \;\propto\; \exp\!\left(\mathbf{d}(z)^{\top}\mathbf{q}(x)\right)

so top-$k$ retrieval is a maximum inner product search over precomputed document vectors.1 Karpukhin et al. introduce DPR as dense retrieval with dual encoders trained on question–passage pairs, and report that it outperforms a Lucene BM25 baseline by 9% to 19% absolute in top-20 passage retrieval accuracy on four of the five open-domain QA datasets they test; on the fifth, SQuAD, BM25 does better.2

Generator. The generator is BART; to condition on a passage, the authors "simply concatenate" the input $x$ and the retrieved content $z$.1 In the RAG-Sequence form the model marginalises over the retrieved passages:1

p(y \mid x) \;\approx \sum_{z \,\in\, \text{top-}k\left(p_\eta(\cdot \mid x)\right)} p_\eta(z \mid x)\, \prod_{i} p_\theta(y_i \mid x, z, y_{1:i-1})

(RAG-Token instead marginalises per token.1 This course also counts as RAG the simpler pattern of placing several retrieved passages together in one prompt: it has the same two stages. How a given product combines passages is something to check in its documentation, not assume.)

Corpus. The non-parametric memory is a chosen corpus: in the paper, a December 2018 Wikipedia dump split into disjoint 100-word chunks, about 21 million passages.1 (The course's own gloss, not a claim of the paper: answering a query does not train $\theta$ on the corpus; the retriever surfaces $k$ chunks per query.)

Misconception: "the model has read our documents and remembers them." In a RAG system the retrieval corpus is external to $\theta$, and retrieval does not write it into $\theta$. The generator conditions on the original input $x$ and the retrieved $z$, through parameters $\theta$ that the authors call parametric memory.1 So three things can carry information into $\hat{y}$: what the user supplied in $x$ (the question, instructions, pasted text), what is stored in $\theta$ from training, and the retrieved passages. Retrieval is not a claim about absence: it does not show that a document's content is missing from $\theta$ or from $x$. What it does bound is the retrieved evidence: a document absent from the index cannot appear in $R_k$, whatever the generator's quality.

Lewis et al. show the index's effect with a hot-swap: replacing the 2018 Wikipedia index with a 2016 one (and vice versa) changes the system's answers about world leaders to match the index, without retraining.1 That demonstrates updating through the index. It is not evidence that every answer follows the index, or that the generator has no relevant parametric knowledge.

In the Big Widget example used in the other gears, the replaced warranty-labour policy postdates the model's training, is not in $x$, and was never added to the collection. Those three conditions are stipulated by the scenario, not inferred from retrieval. The observed trace: retrieval returns the stale policy, and the generator answers fluently from it, citing it. NIST AI 600-1 names that output risk confabulation: "the production of confidently stated but erroneous or false content".3 The root cause is coverage. A prompt change cannot put the missing page into $R_k$; an abstention instruction may suppress the wrong answer, which limits the harm without repairing coverage. The fix is to update the corpus, then re-run the failing query and check both that the new passage is in $R_k$ and that $\hat{y}$ is supported by it.

Each stage is separately measurable. DPR is evaluated on retrieval alone, as top-$k$ accuracy: the fraction of questions for which at least one of the top $k$ retrieved passages contains the answer.2 Section 3 builds the diagnosis on that separation.

2. What retrieval changes in the input, and what it cannot guarantee

It changes the context, not the model. Retrieval adds tokens to the generator's input: each retrieved passage is concatenated with the input.1 Nothing in $\theta$ changes per query. That is a statement about weights, not about retention. The application may log $x$, $R_k$ and $\hat{y}$, keep conversation memory, or pass data to a provider under its own terms. Check the deployment's retention policy. Section 4 asks you to log $R_k$, and that log is itself sensitive.

Position matters. Liu et al. test models on multi-document question answering, in which exactly one of $k$ documents contains the answer and the rest are distractors, and on a synthetic key–value retrieval task, varying the position of the relevant item.4 Performance is often highest when the relevant information is at the beginning or end of the input context and degrades significantly when the model must use information in the middle of a long context, including for models with extended context windows.4 The result is a U-shaped accuracy curve over position.4 These are 2023 models on research tasks; the finding motivates testing position effects in your own build, not a numeric prior for it.

For a pipeline, this means the retrieval step's recall at $k$ bounds the retrieved evidence the generator can use through this path. It does not bound everything the generator draws on, since $x$ and $\theta$ also contribute, and it is not a measure of what the generator does use. Liu et al. find that reader performance saturates long before retriever recall; raising $k$ can raise recall while burying the gold passage mid-context.4

What retrieval does not guarantee.

  1. Faithful reading. Conditioning on a correct passage does not ensure the output is entailed by it. Confabulation is listed by NIST as a risk of generative AI as such.3
  2. Use of mid-context evidence. See above.4
  3. Instruction/data separation. Greshake et al. argue that LLM-integrated applications "blur the line between data and instructions", and introduce indirect prompt injection: adversaries plant prompts in data the application is likely to retrieve, with no direct interface to the system. They demonstrate the attack against real-world LLM-integrated applications.5 A retrieved passage shares the context window with the system prompt. The course's own reading: role markers and delimiters exist, but nothing in the input guarantees that the model treats retrieved text only as data. That is not an impossibility result for every architecture. The course's design rule follows: a system prompt is not a security boundary, so there is no direct path from $\hat{y}$ to a consequential tool call; a deterministic gate outside the model sits between them. Defences and design limits are in 300.6.

Misconception: "if it cites a document, the answer is right." Two checks are needed, in order.

Authenticity. A citation in $\hat{y}$ is a claim. If the model writes it, it is generated text: NIST AI 600-1 notes that outputs "may also include confabulated logic or citations that purport to justify or explain the system's answer".3 A generated label may resolve to no passage in $R_k$, to the wrong one, or to a document that does not exist. If the system attaches labels from the retrieval step instead, they are only as good as that mechanism; check which the deployment does. Either way, first confirm against the trace that each cited id exists, is the document it claims to be, and is in $R_k$.

Support. A validated citation identifies a retrieved passage; it is still not evidence of entailment. Es et al. define faithfulness as whether the claims in the answer can be inferred from the retrieved context, and measure it separately from context relevance.6 Their faithfulness score extracts a set of statements $S$ from the answer and checks each against the context; with $V \subseteq S$ the statements the context supports,6

F \;=\; \frac{|V|}{|S|}.

A fully cited answer can have $F < 1$ (this follows from the definition). Then check currency: a cited, entailing passage can still be stale. Lewis et al. list providing provenance for model decisions as an open problem that the non-parametric memory helps with.1 Note that RAGAs uses an LLM to extract and verify the statements,6 so its numbers are themselves model judgments, which 300.5 teaches you to calibrate against people.

3. Attributing a failure: retrieval or generation

Misconception: "every wrong answer is a hallucination, and nothing more can be said." With a trace (query $x$, retrieved set $R_k$, answer $\hat{y}$) and a labelled gold passage set $G$ (the passages required for a correct answer), the first failing stage is attributable:

ConditionAttributionTypical fixes
$G \not\subseteq R_k$Retrieval failureCorpus coverage and currency; chunking; query encoding or hybrid search; $k$
$G \subseteq R_k$ and $\hat{y}$ wrong or not supported by $R_k$Generation failurePassage count and ordering (position effects4); instructions; model; faithfulness checks

This mirrors how RAG evaluation is decomposed in the literature: RAGAs separates context relevance (how much of the retrieved context is relevant to the question) from faithfulness and answer relevance,6 and DPR reports retrieval accuracy before any generation.2

Edge cases:

Worked traces (fictional Big Widget data; each needs one gold passage).

Trace A
  query:     How many days does a dealer have to file a warranty claim?
  retrieved: [1] travel-and-expenses §2   [2] canteen-hours
  answer:    Dealers have 30 days to file a warranty claim.
  -> G ⊄ R_k: retrieval failure (the answer cannot be grounded in R_k)

Trace B
  query:     How many days does a dealer have to file a warranty claim?
  retrieved: [1] warranty-policy §4: "Dealers must file a claim within 60 days of the repair date."
  answer:    Dealers have 30 days to file a warranty claim [1].
  -> citation [1] is in R_k; G ⊆ R_k, answer contradicts [1]: generation failure (cited, unfaithful; F = 0)

4. Operating checklist

The retrieval parts of the three yeses: Get to Yes · Stay at Yes · Recover to Yes. Each is a decision by a named person at a gate (allow, constrain, hold, deny), never by the generator. A yes is scoped to the corpus snapshot, query types and checks it names.

Trace or receipt? A trace ($x$, $R_k$, $\hat{y}$) holds prompts, passages and outputs, so it is a private, access-controlled work record, kept only where the workplace's policy allows. A receipt, in this course, records the gate's decision (allow, constrain, hold, deny) and never carries prompts, outputs, payloads or personal data. Do not put a trace in a receipt you share. A receipt evidences the decision, not the correctness of $\hat{y}$.

Further learning (optional, advanced, not part of this lesson):

Sources

  1. s1 Patrick Lewis, Ethan Perez, Aleksandra Piktus, et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Paper. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩
  2. s2 Vladimir Karpukhin, Barlas Oğuz, Sewon Min, et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Paper. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩ ↩ ↩
  3. s5 National Institute of Standards and Technology (2024). Artificial Intelligence Risk Management Framework: Generative Artificial Intelligence Profile (NIST AI 600-1). Official doc. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩ ↩ ↩
  4. s3 Nelson F. Liu, Kevin Lin, John Hewitt, et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Paper. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩ ↩ ↩ ↩ ↩ ↩ ↩
  5. s6 Kai Greshake, Sahar Abdelnabi, Shailesh Mishra, Christoph Endres, Thorsten Holz, Mario Fritz (2023). Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection. Paper. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩
  6. s4 Shahul Es, Jithin James, Luis Espinosa-Anke, Steven Schockaert (2024). RAGAs: Automated Evaluation of Retrieval Augmented Generation. Paper. Checked 2026-11-19 by source-checker round-3 re-check (separate Claude session b1741703, Claude Opus 5.5). ↩ ↩ ↩ ↩

Next: the quiz