← govind p lal
AI and ML Bootcamp · Lecture 11 · lecture notes

RAG - Part 2: Context Rot, Evaluation, HyDE, GraphRAG, RAPTOR, PageIndex

In short — Why does a bigger context window not mean better answers, and how do we test that? This lecture covers context rot and the needle-in-a-haystack evaluation. It then shows how ChatGPT does memory without RAG. Last come the RAG upgrades: HyDE query transformation, GraphRAG, RAPTOR and the vectorless PageIndex.

Context Engineering and Context Rot

builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026, Lecture 5: Week 4 - AI&ML | Transformers: Part 2 | 07/02/2026

Context Engineering Optimising how context is passed to the LLM, so the underlying system is used as efficiently as possible.

Context Rot As the input token count grows, LLM performance goes down.

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?

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

!

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

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026

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:

LayerContent
1. Session metadataDevice, location/IP, subscription, usage. Injected at session start
2. Explicit facts (facts.md)Markdown file: name, job, age, interests, habits
3. Conversation summariesSummaries of the last 15-20 chats
4. Current chat windowSliding window of the ongoing conversation

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

adds to earlier · HyDE (hypothetical document embeddings): the full mechanism for the query transformation step only mentioned in L10-c9. · builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026

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.

Vague queryLLM hypotheticalanswerEmbed answerVector DB searchBetter answer
HyDE flow.

GraphRAG and RAPTOR

builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026

GraphRAG - RAG with relationships.

RAPTOR - Recursive Abstractive Processing for Tree-Organised Retrieval. Used when context is too big even for RAG.

PageIndex: Vectorless RAG

builds on Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026, Lecture 10: Week 9 - AI&ML | RAG from the Ground Up - Part 1 | 13/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

Idea: find content in a book by checking its table of contents, not reading every page.

Vector RAGPageIndex
Retrieval bySimilarityReasoning
StructureDestroyed by chunkingPreserved
StorageVector DBTree
Benchmark quotedBasic RAG about 54%About 99% by default
enough infonot enoughPDFTree indexsections, node idsLLM reasons on treeRead chosen sectionsAnswer
PageIndex agent loop.

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.