RAG - Part 2: Context Rot, Evaluation, HyDE, GraphRAG, RAPTOR, PageIndex
Context Engineering and Context Rot
Context Engineering Optimising how context is passed to the LLM, so the underlying system is used as efficiently as possible.
- First understand how the LLM behaves at scale: limits, patterns, quirks.
- Then build the context pipeline (agent, document system, etc.) around that behaviour.
- Example of a known pattern: LLMs pay most attention to the start and end of context and least to the middle (U-shaped).
Context Rot As the input token count grows, LLM performance goes down.
- All tokens are not treated the same way. With long context, attention similarity scores between many tokens become very similar.
- Advertised windows (128K, 256K, now 1M) are bigger than the effective window. A '1M' model may be effective at only around 300-400K.
- Past that, the model stops giving every token equal importance.
- Chroma's research blog showed this across closed and open models (Gemini, GPT, Claude, open source). X axis = input length, Y axis = score. Score falls as length rises.
Needle in a Haystack (NIAH)
Needle in a Haystack - an approach to evaluate an LLM's retrieval over long context. Answers: can my model retrieve the right information at large context?
- Needle - the answer we want. A pinpointed sentence containing the answer.
- Haystack - all the other context passed along with the needle. A big chunk of document.
- Distractors - passages that look almost relevant but answer something else. They can make the model hallucinate.
Example
Question: what was the best writing advice I got from my college classmates? Needle: the advice from classmates (write every week). Distractor: the best writing tip from my college professor (read every day). The model may confuse the two.
Chroma's Improvement: Non-Lexical Retrieval
The Problem Classic NIAH is only lexical (matching the same words). The question and needle share words, so you could just search for them. It does not test semantic retrieval, and models score near perfect on it.
The Solution Make the needle non-lexical: semantically related but with different words, so the model has to reason.
Example
Question: which character has been to Helsinki? Old needle: Yuki lives in Helsinki. New needle: Yuki lives next to the Kiasma museum. The model must find Yuki, then work out that the museum is in Helsinki, and so conclude Yuki lives in Helsinki.
Lexical = same familiar words. Non-lexical = same meaning, different words. The improved test mimics real use.
Eval Setup Across Increasing Context
Goal: test public models (closed and open source) on increasing token length and compute a score.
Haystack sizes: 1 page -> 10 pages -> 100 pages -> 1,000 pages -> 10,000 pages
- Same needle and the same set of about 100 questions at every context size.
- Only the added irrelevant text and distractors grow.
- Needle position and paragraph order are shuffled sometimes.
- Each API call is a fresh instance, so the model has no memory of earlier questions (no bias).
- Result: score falls as context grows.
If you assume every token is weighted the same, you build a flawed system. Knowing this separates a good from an excellent context engineering design.
ChatGPT Memory Is Not RAG
Common assumption: ChatGPT stores past chats in a vector DB and retrieves. Wrong. RAG needs a vector DB and retrieval, which is overhead. Reverse engineering showed four layers:
| Layer | Content |
|---|---|
| 1. Session metadata | Device, location/IP, subscription, usage. Injected at session start |
| 2. Explicit facts (facts.md) | Markdown file: name, job, age, interests, habits |
| 3. Conversation summaries | Summaries of the last 15-20 chats |
| 4. Current chat window | Sliding window of the ongoing conversation |
- No embeddings, no vector search.
- Token-efficient: all four layers are small. Accepted trade-off.
- Uploaded PDFs are passed in directly, not RAG'd.
- Anthropic's Claude similarly keeps a memory.md.
- Cursor does use RAG.
Why still use RAG? When data cannot be summarised and minute details matter, e.g. a legal company like Harvey (court cases look alike but verdicts differ) or hospital data.
HyDE: Query Transformation
The Problem Users write vague queries. Assume the user is dumb and can ask anything. Bad: 'What's the fix?' Good: 'How do I fix this NameError?'
The Solution HyDE - the LLM first writes a hypothetical (generalised, fake) answer, and that answer is embedded and used to search.
- The fake answer mentions things like runtime error and NameError, which narrows the search.
- Applied to every query, with no check for good or bad.
- Cost: about one second extra. Worth it versus a wrong answer.
GraphRAG and RAPTOR
GraphRAG - RAG with relationships.
- Nodes - entities (people, organisations). Edges - relationships (e.g. John works at OpenAI).
- Stored in a graph database instead of a vector DB.
- Useful for relationship-heavy tasks, e.g. social networks (Snapchat uses it).
- Edges can be directed or undirected, depending on use case.
RAPTOR - Recursive Abstractive Processing for Tree-Organised Retrieval. Used when context is too big even for RAG.
- Summarise, then organise into levels like a tree.
- Top level = most important information; deeper levels add detail.
- Works like a table of contents you can traverse.
- Seen mostly in research, not much in industry.
PageIndex: Vectorless RAG
Idea: find content in a book by checking its table of contents, not reading every page.
| Vector RAG | PageIndex | |
|---|---|---|
| Retrieval by | Similarity | Reasoning |
| Structure | Destroyed by chunking | Preserved |
| Storage | Vector DB | Tree |
| Benchmark quoted | Basic RAG about 54% | About 99% by default |
- Free to use with an API key; open source.
- Submit document, it returns a tree (abstract, sections, node ids, page indexes).
- Faster, less context, more accurate, shows references.
- Essentially an LLM plus PageIndex is an agent, since it has the retry loop.
- RAG still preferred when 100% accuracy matters (lives, crucial matters).
How to choose: build your own eval with 10-20 questions whose answers you know, and compare your best RAG against PageIndex.
Glossary
- Context rot
- Drop in LLM performance as the number of input tokens increases.
- Needle in a Haystack
- Eval that hides an answer sentence (needle) in long unrelated text (haystack) and checks retrieval.
- Distractor
- Passage that looks like the answer but answers something else.
- Lexical
- Matching by the same words.
- HyDE
- Generate a hypothetical answer to the query and search with its embedding.
- GraphRAG
- RAG over a graph database of entities and relationships.
- RAPTOR
- Hierarchical tree of summaries for retrieval over huge context.
- PageIndex
- Vectorless RAG where an LLM reasons over a document's tree index.