HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1282 min read

Context Rot (Effective Context vs Window Size)

Your 200K Window Is Not 200K of Attention

AI engineeringAI engineering / RAG & context management

The scenario

Your support bot is good. 8 retrieved chunks, ~6K tokens of context, sharp answers. Product says: "the model takes 200K — just stuff in the whole knowledge base." You bump top_k from 8 to 60. Now ~90K tokens. Cost triples, latency doubles, and accuracy DROPS. Users report the bot "ignoring" a doc that is literally sitting in the prompt.

Nothing is broken. You hit context rot.

What's actually happening

A context window is a hard limit, not a performance guarantee. Two effects degrade as you fill it:

1️⃣ Position effects ("lost in the middle"). Retrieval accuracy across a long prompt is U-shaped — strong at the start, strong at the end, weakest in the middle. The same fact at 55% depth is measurably less likely to be used than at the top.

2️⃣ Distractor pressure. Attention is a softmax over all tokens — a fixed budget that gets divided. 52 near-miss chunks about "refund policy" don't sit there inertly; they pull probability mass away from the one chunk with the answer. Near-misses hurt more than plainly irrelevant text.

Both are signal-to-noise problems, not size problems. 6K of precise context beats 90K of mostly-plausible context.

Measure it, don't guess

Public needle-in-a-haystack scores are too easy: one verbatim fact, no distractors. Models ace them, you conclude long context is "fine", and your pipeline still rots. Build your own curve:

  • 30 real queries with known-correct answers
  • run each at top_k = 5, 15, 40
  • plot end-to-end answer accuracy per k — NOT retrieval recall@k

recall@k rises monotonically forever. Accuracy usually peaks around k=5–15, then bends down. That peak is your real context budget.

What to do instead of stuffing

  • Retrieve wide, pass narrow. Fetch 50, rerank with a cross-encoder, keep 5–8.
  • Order deliberately. Put your strongest chunk LAST, right before the question, so it lands in the high-attention tail. Most pipelines dump chunks in similarity order — which parks the best evidence in the weak middle.
  • Set a relevance floor. Drop anything under the score threshold even if you end up passing 2 chunks instead of 8.
  • Pin the task at both ends: instructions on top, restate the actual question after the documents.
  • In agent loops, compact instead of accumulating — summarize old tool results rather than carrying every one forward.

The trap that looks like a fix

Adding "use ALL of the provided documents" to your prompt. It doesn't redistribute attention. It just makes the model cite more loosely and invent links between distractors.

Your exercise

Change exactly one thing in your current RAG prompt: move the top-ranked chunk from first position to last, immediately before the user's question. Run your 20 hardest queries both ways and count wins.

Then answer: at what k does your accuracy curve actually peak — and is your top_k above it right now?