Course map

Topic 221 / Specialist

Retrieval-augmented generation (RAG)

Chapter 13.13 · Retrieval-Augmented Generation

What and why

An LLM serving pipeline (→ 220) generates text from what it already encodes in its weights plus whatever context is given in the prompt — it has no built-in way to answer from information outside that context, and no way to cite where an answer came from. Retrieval-augmented generation closes that gap: before generating, retrieve relevant material from an external corpus and place it in the prompt as grounding, so the model answers from supplied evidence instead of, or in addition to, what it already encodes. Builds on: → 220 (LLM serving and inference infrastructure).

Diagram

Corpus ─► chunk ─► embed ─► index ─┐
                                    ├─► retrieve ─► rerank ─► prompt (chunks + query)
                       query ───────┘                              │
                                                                    ▼
                                                     generate (→220) ─► grounded answer

How it works

  1. Ingest: source documents are collected from wherever the corpus lives, with enough metadata (source, date, access permissions) to support filtering and citation later.
  2. Chunk: documents are split into smaller pieces, because retrieval and the model's context window both work on bounded-size units, not whole documents; chunk size trades context (larger chunks preserve more surrounding meaning) against precision (smaller chunks let retrieval pinpoint the exact relevant passage instead of pulling in a whole document's worth of mostly irrelevant text).
  3. Embed: each chunk is converted into a vector representation capturing its meaning, so semantically similar text produces similar vectors even when the wording differs (→ 222 covers the vector mechanics in depth).
  4. Index: embedded chunks are stored in a structure supporting fast similarity lookup (→ 222) and, in a hybrid setup, alongside a traditional keyword index (→ 209) for exact-term matches embeddings alone can miss.
  5. Retrieve: given a query, the pipeline finds the chunks most relevant to it, by vector similarity, keyword match, or both — this step determines whether the generated answer will actually be grounded in correct material.
  6. Rerank: a second, typically more precise pass re-orders the retrieved candidates, because the fast retrieval step (optimized for speed over many chunks) is often less accurate than a slower model that can afford to score just the retrieved shortlist.
  7. Generate with grounding: the reranked chunks are placed into the prompt alongside the original query, and the LLM (served per → 220) generates an answer instructed to rely on the supplied chunks — what lets the system answer from material it was never trained on, and what makes citing a source possible.
  8. Corpus freshness and access control: because retrieval re-reads the corpus at query time rather than baking it into model weights, updating the corpus updates what the system can answer immediately — but retrieval must also respect the same access permissions as the source corpus, or it can surface restricted material to a user who queries for it.

Practical example

(illustrative). A support-answer system has 10,000 help articles chunked into roughly 40,000 passages. A user question is embedded and compared against those 40,000 passage vectors, retrieving the 20 most similar; a reranking pass narrows those 20 down to the 3 most relevant; those 3 passages — not the other 39,997 — are placed in the prompt, and the model generates an answer citing those 3 sources. If none of the 40,000 passages actually address the question, retrieval returns only weakly relevant material, and the grounded answer should reflect that uncertainty rather than fabricate a confident one.

When to use it

  • Use when: answers need to be grounded in a specific, possibly changing corpus the model wasn't, and can't practically be, trained on directly, or citation and traceability to source material matter.
  • Avoid when: the question doesn't depend on any external corpus — the model's general knowledge already answers it — or the corpus is small and static enough to place entirely in the prompt without retrieval.
  • Failure scenarios: chunking that splits a crucial fact across two chunks so neither retrieval nor the model ever sees it whole; retrieval quality that looks fine in aggregate metrics but misses exactly the passage a specific hard question needed; retrieval surfacing access-restricted content a user shouldn't see because the index wasn't filtered by the source corpus's permissions; evaluating only final answer quality and never retrieval quality separately, so a retrieval failure gets misdiagnosed as a generation problem.
  • Simpler alternative: put the entire relevant document directly in the prompt with no retrieval step, when the corpus is small enough to fit within the model's context window.

Practice

(Process kind — design exercise; 20–30 min, no lab services). For a corpus of 5 short documents you write yourself (a paragraph each), manually chunk them into 2 passages each, pick a query, and by inspection — no real embeddings needed — decide which passages a retrieval step should return, then write the prompt you'd send the model including those passages. Done when you can state, separately, whether your retrieval step and your generation step each succeeded or failed for that query.

Key takeaway

RAG grounds generation in a corpus by retrieving relevant chunks before the model answers — retrieval quality and generation quality must be evaluated separately, because a bad answer can come from either. Done when you can: diagnose whether a bad RAG answer was a retrieval failure or a generation failure. Used next in → 222 (the vector search mechanics behind this pipeline's retrieve step).

References

None — this topic makes no external claims.

Source directory

Search the guide