Day 118 Β· Beyond one shelf

Advanced RAG Patterns

You will be able to
  • Decompose multi-hop questions into dependent sub-queries and execute them sequentially
  • Sketch when graph-RAG, RAPTOR-style summary trees, and agentic retrieval each earn their complexity
  • Route between structured (SQL) and unstructured (vector) sources for a mixed question
  • Assemble the week into a failure-mode taxonomy and a debugging flowchart you will use on the capstone
Today's ~120 minutes
Spaced-rep: due cards + bi- vs cross-encoder from memory (D117)10 min
ELI5 + tech read25 min
Guided: multi-hop + routing labs40 min
Practice: prescription drill18 min
Project: debugging flowchart22 min
Quiz + flashcards5 min

Builds on: Day 117 β€” Reranking & query transforms Β· Day 113 β€” RAG architecture Β· Day 111 β€” Tool use & function calling

The analogy

Some questions can't be answered from one shelf. "Did the author of our expense policy also write the security policy?" needs three trips: find who wrote the expense policy, find who wrote the security policy, compare. A single search for the whole question retrieves a muddle β€” no one page contains the answer, because the answer doesn't LIVE anywhere; it has to be assembled.

The advanced patterns are all ways of making multiple, smarter trips. Multi-hop decomposition breaks the question into steps where each search uses the previous answer. A summary tree (RAPTOR) adds "shelf summaries" and "floor summaries" to the library so zoomed-out questions ("what are the big themes?") have something zoomed-out to retrieve. Graph-RAG draws a map of which people, teams, and documents connect to each other, so "everything related to X" follows edges instead of similarity. And agentic retrieval hires the intern from next week early: let the model itself decide what to search next, look at what came back, and search again β€” an open-book exam where the student may return to the shelves as often as needed.

Why this matters on the job

One-shot retrieval tops out fast on real corpora: enterprise users ask comparison questions, aggregate questions ("how many customers mentioned pricing?"), and questions whose answers span three systems β€” the exact queries that make naive RAG look broken in a pilot. Knowing which pattern fixes which failure β€” and that each adds latency, cost, and failure surface β€” is the difference between an engineer and a framework-user. The debugging flowchart you build today is a genuine FDE artifact: when a customer says "it answered wrong," you'll walk the flowchart live instead of shrugging. It also completes the mental model your capstone and Day 136's evals depend on.

Guided practice

guided 1

Multi-hop over a corpus where one-shot fails

25 min
  1. Create multihop_lab.py with the starter code: a corpus with document authorship facts and a hand-written two-hop plan for the question "Did the author of the expense policy also write the security policy?"
  2. Run the one-shot baseline: the full question retrieves a muddle (verify: no single chunk contains the answer).
  3. Run the multi-hop version: hop 1 retrieves the expense policy's author chunk; a tiny extractor pulls the name; hop 2's query is BUILT from that name; the synthesizer compares. Note the dependency β€” hop 2 could not have been written in advance.
  4. Poison hop 1: change the extractor to return the wrong name and watch hop 2 confidently retrieve irrelevant evidence. Write one sentence on why intermediate verification matters.
  5. In production the plan comes from an LLM ("split this question into sequential sub-queries; later queries may reference earlier answers as ⟨answer1⟩") β€” the printed PLAN_PROMPT shows exactly that prompt.
🐍 python β€” editable, runs in your browser
Ctrl/⌘+Enter runs · Tab indents · numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 2

Route the question: SQL, vectors, or both

15 min
  1. Append the starter code: a mini "warehouse" (a list of ticket dicts), the vector corpus from exercise 1, and a keyword-based router standing in for an LLM classification call (the real router prompt is printed).
  2. Run the three test questions: an aggregate ("how many tickets mention refunds"), a policy lookup ("what needs pre-approval"), and a mixed one ("are refund complaints against our stated policy?").
  3. Verify the aggregate goes to the SQL-ish path and returns an exact count β€” then force it through vector retrieval instead and see why that's malpractice (it returns 2 example tickets, not a count).
  4. For the mixed question, confirm both paths run and note what the synthesis step would need to cite: a number from one source, a quote from the other.
🐍 python β€” editable, runs in your browser
Ctrl/⌘+Enter runs · Tab indents · numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)

On your own

Pattern-matching drill: prescribe, don't implement

18 min

For each symptom below, prescribe the cheapest adequate fix from this week's toolbox (chunking / hybrid / rerank / rewrite / HyDE / decomposition / RAPTOR / graph-RAG / routing / freshness), plus one sentence of reasoning. Then rank your prescriptions by implementation cost.

  1. "Summarize our Q2 customer themes" retrieves five random specific tickets.
  2. "Compare the old and new expense policies" answers from only one of them.
  3. Follow-up questions in chat retrieve nonsense.
  4. "Who has worked with vendor Acme across any team?" misses connections spread over many docs.
  5. Answers cite a policy retired two months ago.
  6. "SKU-9981 warranty length" returns unrelated warranty prose.

No peeking until done. Reasonable answers: 1 RAPTOR-style summaries (no zoomed-out chunks exist); 2 decomposition (two targeted retrievals + compare); 3 query rewriting; 4 graph-RAG (relationship question); 5 freshness/incremental indexing + tombstones; 6 hybrid (exact identifier).

Ship before you stop

The RAG debugging flowchart

Create rag_debugging.md β€” the artifact you will physically use during the capstone and can show in interviews. Part 1: the failure taxonomy as a table (symptom β†’ stage β†’ root cause β†’ fix β†’ which day taught it) covering at least 9 failures from this week. Part 2: a flowchart (Mermaid or ASCII) starting from "answer is wrong" that branches on OBSERVABLE checks in debugging order: was the doc ingested? β†’ is the fact intact in one chunk? β†’ is that chunk in stage-one top-20? β†’ in reranked top-3? β†’ did the prompt contain it? β†’ did the model use it faithfully? Each leaf names the fix. Part 3: three symptoms where the fix is an ADVANCED pattern (decomposition, routing, summaries), so you don't over-prescribe them for stage-level bugs. Commit it.

Rubric β€” check what you completed (0/6)

Common mistakes & misconceptions

  • Prescribing agentic/multi-hop retrieval for stage-level bugs. If the chunk was torn or the identifier unsmellable, fix chunking or add BM25 β€” advanced patterns multiply cost and failure surface.
  • Letting hop-2 run on an unverified hop-1 answer. Dependent hops compound errors; verify intermediate extractions (or at least carry confidence forward) when hops chain.
  • Answering aggregate questions from a vector index. "How many…" needs SQL over records; similarity search returns examples, not counts β€” route, don't retrieve.
  • Building RAPTOR trees or knowledge graphs before query logs demand them. Both are heavy at ingest and painful on updates; earn them with observed thematic/relationship queries.
  • Forgetting deletions. Removing a doc from the source without tombstoning its chunks means the retired policy keeps answering β€” freshness includes forgetting.
  • Treating the debugging flowchart as documentation. It is a runbook: walk it top-down on every capstone failure, and update it when a new failure class appears.
Knowledge check

Q1. "Which of our policies were written by the same person?" fails with one-shot retrieval because…

Q2. A user asks "how many tickets mentioned refunds last quarter?" The correct architecture move is…

Q3. What problem do RAPTOR-style summary trees solve?

Go deeper β€” curated resources

paperRAPTOR paper β€” recursive abstractive retrieval (Sarthi et al.) β†—30 mindocsLlamaIndex β€” advanced retrieval (routing, sub-questions) β†—25 minarticleAnthropic β€” building effective agents (augmented LLM & routing sections) β†—20 min
If you have a third hour
  • GraphRAG (Microsoft) β€” community summaries β€” Combines both structure patterns: build an entity graph, detect communities, summarize each community β€” graph structure plus RAPTOR-style altitude in one system.
Done means
  • Multi-hop lab run, including the poisoned-hop experiment
  • Router demonstrated on all three question types, with the malpractice case shown
  • All six prescriptions reasoned and cost-ranked
  • rag_debugging.md committed with taxonomy + flowchart
  • Quiz β‰₯ 2/3
How this connects

← Back: This day is Week 17's synthesis: Day 113's stages became Day 114–117's fixes, and today's taxonomy files every one of them. The router is Day 111's tool-choice pattern pointed at data sources.

Forward β†’: Tomorrow (Day 119) the capstone kicks off and your flowchart becomes a living runbook. Agentic retrieval gets its full treatment as the agent loop on Days 120–122, and Day 136 turns each taxonomy row into a metric.

Unlocks: D121 Agents II β€” Planning & Decomposition