Sub topic 10
Multi-Step Retrieval: Using Earlier Results to Shape Later Queries
This chapter assumes you can run a basic RAG pipeline, know how vector or BM25 search returns a ranked list, and can read Python. It also assumes you have seen query decomposition and reciprocal rank fusion (RRF) in earlier chapters.
Why one round of search is not enough
Multi-query expansion and RRF both run all their searches at once. Every query is written before any result is seen. That works when the question can be rephrased in several ways but the answer sits in one place.
Some questions cannot be planned up front. Take this one: "Who manages the team that built the billing service?" The first search finds the team name. The second search needs that name to find the manager. You cannot write the second query until the first result comes back.
Multi-step retrieval means running searches in sequence, where each query is built from what the previous searches returned.
Parallel versus sequential retrieval
| Property | Parallel (multi-query, decomposition) | Sequential (multi-step) |
|---|---|---|
| When queries are written | All before any search | Each after the previous result |
| Latency | One search round | One round per step |
| Handles dependent facts | No | Yes |
| Merging | RRF over ranked lists | Pool passages across steps |
Decomposition can feed either mode. Independent sub-questions can run in parallel. Dependent sub-questions must run in order.
Worked example
The function below does one hop: it searches, then asks the model for a follow-up query that uses the passages found so far. You can run it with the stub functions at the bottom.
from typing import Callable
def two_hop_retrieve(
question: str,
llm: Callable[[str], str],
search: Callable[[str, int], list[str]],
top_k: int = 2,
) -> list[str]:
first = search(question, top_k)
prompt = (
f"Question: {question}\n\nPassages:\n"
+ "\n".join(first)
+ "\n\nWrite one search query for the missing fact "
"needed to answer the question. Return only the query."
)
second_query = llm(prompt).strip()
second = search(second_query, top_k)
return first + [p for p in second if p not in first]
docs = {
"billing": "The billing service was built by the Payments team.",
"payments": "Dana Ortiz manages the Payments team.",
}
def fake_search(query: str, k: int) -> list[str]:
hits = [t for key, t in docs.items() if key in query.lower()]
return hits[:k]
def fake_llm(prompt: str) -> str:
return "Who manages the Payments team?"
print(two_hop_retrieve("Who manages the team behind billing?", fake_llm, fake_search))The first search matches the billing passage. The model sees "Payments team" in it and writes a query about that team. The second search returns the passage naming the manager.
What the second query depends on
Quality depends on what you show the model when it writes the next query.
- Show the question and the passages, not just the last passage. The model needs the original goal.
- Ask for the missing fact, not for a rewrite of the question. Otherwise it tends to repeat the first query.
- Deduplicate passages across steps, as the example does, so the pool does not fill with repeats.
Common errors
- Repeating the same query. The model proposes a query you already ran. Keep a set of past queries and stop if you see a repeat.
- Chasing a wrong first hit. An off-topic first result leads the second query further away. Check relevance before building on a passage.
- Passing too much context. Long pools slow each call and bury the useful passage. Cap the pool or keep only the top passages per step.
- No exit. Without a step limit, a loop can run until cost or time runs out. The next chapter covers the loop and its stopping rules.
Exercise
A user asks, "Which university did the author of the 'Attention Is All You Need' lead paper attend?" Why can parallel multi-query search with RRF fail here, and what does multi-step retrieval do differently?
Show answer
The answer needs two dependent facts: the author's name, then that person's university. Parallel queries are all written before any result is seen, so none can contain the author's name. Multi-step retrieval searches for the author first, reads the name from the passages, and then writes a second query using that name.