Loading
0x90Lesson 10 of 11

Advanced RAG patterns

Rewrite queries, hop across documents, let an agent decide when to search, and know when not to use RAG.

22 min 5-question quiz 2 code exercises
By the end of this lesson you can
  • Improve recall with query rewriting and expansion
  • Answer multi-hop questions with several retrieval steps
  • Recognize when agentic RAG, GraphRAG or a long context fits better

The basic pipeline - search once, answer once - covers many questions. These patterns handle the rest:

  • Query rewriting. In a chat, “what about electronics?” means nothing on its own. Ask a model to rewrite it into a standalone question (“What is the return policy for electronics?”) before searching.
  • Query expansion / multi-query. Search several phrasings or synonyms and fuse the results (with RRF) to catch vocabulary mismatches.
  • HyDE (Gao et al., 2022). Have a model write a hypothetical answer and search with its embedding - answers often look more like the target passages than questions do.
  • Multi-hop retrieval. “Who manages the team that owns the billing service?” needs two lookups: find the owner team, then its manager.
  • Agentic RAG. Give the model a search tool and let it decide whether, what and how often to search, reading results before searching again.
  • GraphRAG. Extract entities and relationships into a graph to answer “global” questions (“what are the main themes across all incident reports?”) that no single chunk answers.

Try it

Which pattern fixes it?

Match each failure to the technique that addresses it.

0 of 5 sortedScore 0/0
  • “Follow-up “and for sale items?” retrieves nothing useful”

  • “Users say “money back”, documents say “reimbursement””

  • ““Which office does the manager of the billing team work in?””

  • “The whole knowledge base is one 30-page handbook”

  • “The first search shows the question needs a policy from another department”

two_hops.py
1owner = {"billing-service": "Payments team"}
2manager = {"Payments team": "Dana Ruiz"}
3team = owner["billing-service"]
4print(team, "->", manager[team])
Output
Payments team -> Dana Ruiz

Key takeaways

  • Rewrite follow-ups into standalone queries; expand queries to beat vocabulary mismatch.

  • Multi-hop and agentic RAG chain several searches; GraphRAG handles corpus-wide questions.

  • If the knowledge fits in the context window, you may not need retrieval at all.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Python

Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.

Exercise 1

Expand queries with synonyms

+25 XP

The first line is a JSON list of synonym groups. For each following query, find the first group with a member that appears in the query (checking groups, then members, in order). Print the query, then the query with that member replaced by each other member of its group, joined by |. Queries with no match are printed unchanged.

  • Three queries
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Answer a two-hop question

+25 XP

Lines before --- are facts: subject|relation|object. Lines after are questions: start|relation1|relation2. Follow both relations and print start -relation1-> X -relation2-> Y. If a hop has no fact, print unknown in its place and stop there.

  • Ownership questions
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: