Sub topic 8
Step-Back Queries for Broader Context
This chapter assumes you have a working RAG pipeline (index, retrieve, generate), know how embedding or BM25 search returns a ranked list, and can call an LLM with a text prompt. You should also be able to read Python.
The problem: the question is too narrow
Some questions are so specific that no single passage matches them. The answer depends on a general principle or background fact that the documents state elsewhere, in broader terms.
Take the question "Why does my 3 MB upload fail with a 413 error on the free plan?" A search for that text finds passages about 413 errors, but the passage that explains the plan's size limit may talk about "request payload quotas by tier". The question is a detail. The documents hold the rule behind it.
A step-back query is a broader question that the model writes from the original one. Instead of asking about the detail, it asks about the concept the detail depends on. For the example above, a step-back query could be "What are the request size limits for each pricing plan?"
How it works
The step is small. You ask the LLM for one more general question, search with both the original and the broad query, and give the generator the combined passages.
Searching with only the broad query is risky. It may return background that never touches the specific case. Keeping the original query means you still get passages that match its exact terms, such as the error code.
Implementation
This code reuses reciprocal_rank_fusion from the earlier chapter. The search function takes a query and a count, and returns a list of document ids. The llm function takes a prompt and returns text.
from typing import Callable
def reciprocal_rank_fusion(rankings: list[list[str]], k: int = 60) -> list[str]:
scores: dict[str, float] = {}
for ranking in rankings:
for rank, doc_id in enumerate(ranking, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)
def step_back_search(
question: str,
llm: Callable[[str], str],
search: Callable[[str, int], list[str]],
top_k: int = 5,
) -> list[str]:
prompt = (
"Rewrite the question below as a more general question about the "
"underlying concept, rule, or background it depends on. "
"Return only the rewritten question.\n\n"
f"{question}"
)
broad = llm(prompt).strip()
rankings = [search(question, top_k)]
if broad and broad != question:
rankings.append(search(broad, top_k))
return reciprocal_rank_fusion(rankings)[:top_k]If the model returns an empty string or repeats the question, the function skips the second search. That saves a call and avoids double-counting the same list in the fusion.
When to use it
Step-back suits some failures and not others. Compare it with the techniques from nearby chapters.
| Failure | Better fit |
|---|---|
| Answer needs a general rule or background fact | Step-back |
| Query wording differs from document wording | Multi-query expansion or HyDE |
| Question has several independent parts | Decomposition |
| Follow-up depends on earlier turns | Conversational rewriting |
Step-back works best on questions about a specific case that is governed by a general rule. Examples are troubleshooting, policy and "why does X happen" questions. It adds little for simple lookups such as "What is the capital of Peru?", where the narrow query already matches.
Common errors
- Going too broad. "What are plan limits?" may be fine, but "How do web services work?" returns generic text. Tell the model to stay in the same domain and to keep product names.
- Dropping the original query. Without it, you lose exact-match terms like error codes and identifiers.
- Applying it to every query. It costs one extra LLM call and one extra search. Use routing to apply it only where it helps.
- Not checking the result. Evaluate it on your own questions. A broader query can push relevant narrow passages out of the top results.
Exercise
A user asks, "Why did my invoice for March show a 2% fee on a wire transfer?" Search finds passages about wire transfers but none that explain fee rules. Write a step-back query for this question, and say why you would still search with the original question as well.
Show answer
A step-back query is "What fees apply to payments and how are they calculated?" It targets the general fee rules that explain the specific charge. The original question should still be searched because it contains exact terms, such as "wire transfer" and "invoice", that find passages about this specific case. Merging both result lists with reciprocal rank fusion covers both the rule and the case.