Exercises — Retrieval-Augmented Generation (RAG) architecture
Before we start, a one-line reminder of the vocabulary we will lean on, each already built in the parent:
- A vector is just a list of numbers — an arrow in space pointing some direction with some length. See Vector Embeddings.
- Cosine similarity measures the angle between two such arrows, ignoring their length. See Cosine Similarity & Nearest Neighbour Search.
- ==top-== means "keep only the best-scoring chunks". Here is a small whole number like 3.
Level 1 — Recognition
Recall Solution
- Indexing — offline, done once: chunk every document, turn each chunk into a vector, store the vectors.
- Retrieval — online: embed the query, score chunks, keep top-.
- Generation — online: paste chunks into the prompt, LLM writes the answer.
Mnemonic from the parent: CERG = Chunk → Embed → Retrieve → Generate.
Recall Solution
- = the query (what the user asked).
- = the answer the model writes.
- = a single retrieved passage (a chunk) — the thing we sum over because we don't know in advance which passage holds the answer.
The factor is the retriever (how likely this chunk is chosen); is the generator (how likely the answer given query + chunk).
Level 2 — Application

Recall Solution
Cosine formula: .
First .
Chunk A : dot . . Same direction — A is just scaled up ×2. Look at the red arrow lying exactly along .
Chunk B : dot . . Very close in angle, slightly off.
Chunk C : dot . . Exactly opposite direction — maximally irrelevant.
Order: A (1.0) > B (0.96) > C (−1.0). Notice A won even though its numbers were biggest — cosine divided the magnitude out, exactly as the parent note promised.
Recall Solution
Why exponentiate? Raw scores can be any real number (even negative). is always positive, and dividing by the total forces the outputs to sum to 1 — a valid probability distribution.
- , , .
- Denominator .
- .
- .
- .
Chunk 1 grabs ~84% of the probability mass. A score gap of just 2 became a probability that is ~7× larger — softmax is sharp, which is why summing over only the top- chunks throws away almost nothing.
Level 3 — Analysis

Recall Solution
(a) Raw dot product:
- Raw dot ranks A > B.
(b) Cosine:
- .
- .
- Cosine ranks B > A.
(c) The disagreement: B points exactly along (angle 0°, perfect topical match) but is a short vector. A points almost along but is 10× longer, so its raw dot product is inflated by magnitude. Look at the figure: the amber arrow (B) sits on the axis; the cyan arrow (A) is longer but tilted off. Raw dot product rewarded length; cosine rewarded direction. Cosine is the honest topical judge. This is exactly the "raw dot product biases toward high-norm chunks" mistake from the parent.
Recall Solution
- With you keep chunks 1–2: one relevant, one distractor. The relevant chunk sits at the top of the prompt where models attend well.
- With you keep all five: now three distractors sit in the middle of the prompt. Models suffer the lost-in-the-middle effect — they under-attend to mid-prompt text — so the answer buried at position 5 may be ignored, while a middle distractor misleads the model.
- Recall vs precision: more raises recall (higher chance the answer is somewhere in context) but lowers precision (fraction of context that is actually useful). Beyond a point the added distractors + longer context cost more accuracy than the extra recall buys.
- Fix: small (≈3–8) plus a re-ranker to push the truly relevant chunk to the top.
Level 4 — Synthesis
Recall Solution
Apply the parent's decomposition directly:
- Term 1: — the good passage is likely retrieved and produces a good answer, so it contributes a big chunk of the total.
- Term 2: — the weak passage is less likely retrieved and produces poor answers, so it barely helps.
- Marginal
Reading it: the answer quality is a weighted average over passages, weighted by how likely each is retrieved. Retrieval quality ( on the good passage) and generator grounding () multiply — improving either lifts the whole system. This is why the parent says "retrieval quality dominates".
Recall Solution
Template:
Use ONLY the context to answer. If the context does not
contain the answer, reply exactly: "Not found in context."
Context: Orders over $50 ship free.
Q: Do you ship to Canada?
A:
Ideal behaviour: the model outputs "Not found in context." — because the retrieved chunk says nothing about Canada.
Which mistake this defends against: the parent's "RAG eliminates hallucination" trap. Grounding alone does not stop confabulation when retrieval is off-topic; you must instruct the model to abstain. Combine with measuring retrieval quality separately (recall@k). See Prompt Engineering and LLM Hallucination.
Level 5 — Mastery
Recall Solution
Choose (b) RAG. Reasons:
- Freshness without retraining — update a document in the vector store and the system is instantly correct; fine-tuning nightly is slow, costly, and always a day stale. Facts become data, not parameters.
- Provenance — RAG can cite the exact chunk it used; a fine-tuned model's knowledge is diffused across weights with no citation. See Fine-tuning vs In-context Learning.
- Lower hallucination on volatile facts — prices/stock in the prompt are copied, not recalled from foggy memory.
Failure mode of RAG: if retrieval returns an outdated or wrong chunk (e.g. stale index), the model confidently repeats it. Mitigation: keep the index refresh tight, measure recall@k, add a re-ranker, and instruct the model to say "Not found" when context is thin.
Recall Solution
(a) Chunks per document. With stride 350 over 5000 tokens: number of chunks chunks per document. Total chunks.
(b) Index size. Each vector bytes. Total bytes bytes GB (raw vectors, before index overhead).
(c) Why not one giant chunk per doc? A 5000-token chunk produces a diluted embedding — one vector must represent many unrelated sub-topics, so cosine matching becomes vague and the right passage never rises to the top. It also wastes the LLM's context window and drowns the answer among irrelevant text. Chunking exists precisely to keep each vector topically focused. See Chunking Strategies.
Recall Quick self-check (reveal after attempting all)
Order the five levels by what they test ::: L1 recognise pieces, L2 apply cosine/softmax, L3 analyse ranking/context tradeoffs, L4 synthesise the full probability + prompt, L5 design a whole system. The single biggest lever on RAG quality ::: retrieval quality (good chunking + embeddings + top-k re-ranking), not the LLM.