4.4.11 · D3Alignment, Prompting & RAG

Worked examples — Retrieval-Augmented Generation (RAG) architecture

2,853 words13 min readBack to topic

This page is a drill sheet. The parent note (RAG architecture) built the machinery: embeddings, cosine similarity, the retrieval softmax, and the prompt-assembly step. Here we push every button — every sign of cosine, the degenerate zero-vector case, the limiting behaviours of softmax, a real word problem, and an exam twist — so no scenario surprises you.

Before we start, one reminder built from zero.


The scenario matrix

Every RAG numeric task lands in one of these cells. The examples below fill all of them.

# Case class What is weird about it Example
1 Same-direction chunk (best case) , angle Ex 1
2 Partially-on-topic , e.g. Ex 1
3 Orthogonal / off-topic , angle Ex 2
4 Opposite direction (contradiction) , angle up to Ex 2
5 Magnitude trap (long vs short chunk) dot product misleads, cosine fixes Ex 3
6 Zero-vector degenerate input division by Ex 4
7 Softmax over scores (top- weighting) turns scores into probabilities Ex 5
8 Softmax limiting behaviour equal scores → uniform; huge gap → one-hot Ex 6
9 Real-world word problem (end-to-end) pick , assemble prompt, latency Ex 7
10 Exam twist (normalisation equivalence) why dot = cosine after normalising Ex 8

Example 1 — best case and partial case (cells 1 & 2)

Figure 1 below draws all four arrows from the origin. Look at the two arrows lying flat along the horizontal axis — the navy query and magenta overlap in direction; that overlap is what "cosine " looks like. The violet arrow splits the corner at , and the orange arrow shoots straight up, a full right angle away.

Figure — Retrieval-Augmented Generation (RAG) architecture

Figure 1. Query and three chunks. Navy = , magenta = (same direction, sim 1), violet = (45°, sim 0.707), orange = (90°, sim 0).

  1. Score A. . Why this step? points along the same ray as (angle ), so cosine hits its maximum — cell 1. In Figure 1 this is the magenta arrow lying on top of the navy one.
  2. Score B. . Why this step? sits at from the query — half on-topic — cell 2 (the violet arrow in Figure 1).
  3. Score C. . Why this step? points straight up, from — perpendicular, no topical overlap (the orange arrow in Figure 1; previews cell 3).
  4. Rank: . Retrieve .
Recall Verify

's numbers are 3× bigger than the query yet cosine stayed exactly 1 — the length divided out. If you had used the raw dot product you'd still pick here, but for the wrong reason (magnitude). That trap is Example 3.


Example 2 — orthogonal and opposite (cells 3 & 4)

In Figure 2, notice the dashed navy arc sweeping from the query all the way round to : that is the half-turn. The violet arrow standing at a right angle is the "" picture; the magenta arrow pointing backwards is the "" picture. Direction, not length, is doing all the work.

Figure — Retrieval-Augmented Generation (RAG) architecture

Figure 2. Same query (navy). Violet at 90° gives sim 0; magenta pointing backwards at 180° gives sim −1. The dashed arc is the half-turn from to .

  1. Score D. . Why? Angle is exactly ; . "Neither for nor against" — cell 3 (violet arrow in Figure 2).
  2. Score E. . Why? points the opposite way (). Cosine reaches its minimum — cell 4 (magenta arrow in Figure 2). A negative score means "points away from the query's topic."
  3. Ranking meaning. In nearest-neighbour search we keep the highest cosine, so (0) ranks above (). Both are bad, but is worse.
Recall Verify

Range check: cosine is always in because (Cauchy–Schwarz). Our values and sit inside — sane. A cosine of is the most anti-relevant a chunk can be.


Example 3 — the magnitude trap (cell 5)

  1. Raw dot products. . . Why this step? Un-normalised dot product is inflated by length; wins only because its numbers are big — wrong.
  2. Cosine of S. . Why? Same direction as query → perfect topical match.
  3. Cosine of L. . Why? is off; dividing by its length cancels its unfair size.
  4. Verdict. Dot product ranks (wrong). Cosine ranks (right). Retrieve .
Recall Verify

Sanity: is literally identical in direction to the query, so nothing should beat it. Cosine agrees (). This is the whole reason production stores normalise vectors once at index time — see Example 8.


Example 4 — the zero-vector degenerate case (cell 6)

  1. Norm of Z. . Why this step? Cosine divides by , so we must check it first.
  2. The formula breaks. indeterminate (both numerator and denominator are ). Why? A zero vector has no direction, so "angle between them" is meaningless — there is no to take a cosine of.
  3. Engineering fix. Systems either (a) drop zero-norm chunks at index time, or (b) define the similarity as so they rank last. Never let reach production.
Recall Verify

Indeterminate, not infinite: here both and , so we get , which has no value — you cannot decide it by the formula alone. And the limit is not fixed either: if a chunk shrinks toward along a direction at angle to , stays equal to the whole way (length cancels top and bottom), so the limiting value depends on the approach direction — different directions give different answers. That path-dependence is exactly why has no well-defined similarity and must be special-cased.


Example 5 — softmax turns scores into retrieval weights (cell 7)

  1. Exponentiate each score. , , . Why this step? Scores can be negative; is always positive, and it magnifies gaps so the best chunk stands out.
  2. Sum them (over all chunks). . Why? This normaliser — the denominator running over every chunk — forces the outputs to add to exactly , a valid probability distribution.
  3. Divide. , , . Why? A score gap of became a probability ratio of — softmax is sharp.
  4. Interpretation. The generator is conditioned mostly on chunk 1, so summing over just the top- (here top-1 or top-2) loses almost nothing.
Recall Verify

✓ — a proper distribution. This is why summing over only the top- passages (rather than the whole corpus) barely changes the answer: the tail probabilities are tiny.


Example 6 — softmax limits and temperature (cell 8)

Figure 3 shows the same scores pushed through three temperatures, so you can see the distribution slide from flat to peaked.

Figure — Retrieval-Augmented Generation (RAG) architecture

Figure 3. Softmax of at (flat, magenta), (plain, violet), (peaked, orange). Lower temperature concentrates the probability on chunk 1.

  1. Case (a): equal scores. each. Why? Identical scores cancel inside the fraction → uniform. The retriever is maximally unsure; every chunk equally likely — the same shape a very high temperature would force.
  2. Case (b): one huge score. dwarfs , so and . Why? As the gap , softmax approaches a one-hot vector — retrieval collapses to a single confident chunk, the same shape a very low temperature would force.
  3. Takeaway. Whether from the scores themselves (these two cases) or from the temperature dial (Figure 3), softmax slides between "hedge across everything" (flat/uniform) and "bet on one" (peaked/one-hot).
Recall Verify

Case (a): ✓. Case (b): to 40+ decimal places — confirming the one-hot limit. Both extremes are valid probability distributions.


Example 7 — real-world word problem, end to end (cell 9)

  1. Choose . There is a clear score cliff between chunk 2 () and chunk 3 (). Keep . Why this step? Chunks 3–5 are near-irrelevant distractors. Stuffing them in risks the "lost-in-the-middle" effect (models skim over mid-prompt text) and wastes tokens — a small, precise beats a big noisy one.
  2. Assemble the prompt. SYSTEM: Answer ONLY from context; say "not found" if absent. CONTEXT: <chunk 1> <chunk 2> QUESTION: How long is the refund window? ANSWER: Why? Putting the fact in the prompt lets the model copy, not invent → grounded answer "30 days" with a citation.
  3. Latency of the extra 3 chunks. Tokens saved . Cost ms. Why? blocks, each ms ms saved and less distraction. Win–win.
Recall Verify

tokens; ms. Small in absolute terms, but multiplied across millions of queries it matters — and the quality gain from dropping distractors is the bigger prize, which is why re-ranking a small candidate set is so effective.


Example 8 — exam twist: dot product = cosine after normalising (cell 10)

  1. Norm of d. . Why this step? Normalising means scaling to length 1; we need the length first. (Recall the hat: .)
  2. Normalise. . And already, since . Why? Dividing by the norm keeps the direction, sets the length to 1.
  3. Dot of normalised vectors. . Why? When both lengths are 1, the two factors in the denominator of cosine are both , so the cosine formula collapses into just the dot product.
  4. Cosine of originals. . Identical to step 3.
  5. The shortcut, stated. Because always (not just for these numbers), you can normalise every vector once at index time and then rank by the cheaper dot product — the ranking is provably identical to cosine.
Recall Verify

Both routes give ✓. This is why systems normalise once at index time (Phase A) and then use fast dot-product nearest-neighbour search — same ranking as cosine, less compute. Contrast Example 3, where skipping normalisation broke the ranking.