Spot the augmentation config bug in this production RAG YAML that's causing 'lost in the middle' faithfulness regressions on long-context queries.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Click any words you think contain an error. Click again to unmark.
The bug is chunk_order: similarity_desc. It buries the second best chunks in the prompt's middle, where attention is weakest. Reorder so strong chunks sit at the head and tail.
Imagine handing someone a thick stack of pages and asking them to answer from it. People skim the first few pages carefully, glance at the last page, and barely read the middle. LLMs behave the same way: they pay strong attention to the start of a prompt, decent attention to the end, and much weaker attention to the middle. This is called 'lost in the middle.' Now the bug: the config sorts retrieved chunks strictly best to worst, so the top chunk sits first (good) but the second best chunk lands in that ignored middle (bad). The fix is to spread your strongest chunks across the head and the tail, where the model actually reads, and let the so so chunks fill the middle valley nobody attends to.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: name the buggy line, explain the U-shaped lost-in-the-middle curve, give the head_tail_alternating fix, and add the long context doesn't fix it nuance plus how you'd eval it.
def head_tail_alternating(chunks):
# chunks already sorted best -> worst by rerank score
n = len(chunks)
ordered = [None] * n
left, right = 0, n - 1
for i, c in enumerate(chunks):
if i % 2 == 0: # ranks 1,3,5 -> head inward
ordered[left] = c
left += 1
else: # ranks 2,4,6 -> tail inward
ordered[right] = c
right -= 1
return ordered # strong chunks at both ends, weak in the middle| Chunk order policy | Where best chunk lands | Where 2nd-best lands | Long-context faithfulness |
|---|---|---|---|
| similarity_desc (the bug) | Head (attended) | Middle valley (under-read) | Regresses on long queries |
| similarity_asc (reversed) | Tail (OK) | Middle valley (under-read) | Still loses the middle |
| head_tail_alternating (fix) | Head (attended) | Tail (attended) | Recovers wasted positions |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming a 200k-token context window eliminates the lost in the middle effect. A larger window makes the valley shallower but it never goes away; position still matters.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.