Search Tutorials
--Ads--Query Decomposition for Multi-Hop Questions
Break complex questions into sub-questions and answer them in sequence.
Some questions are really several questions in one. "Compare the leave policies of our India and UK offices" needs two separate retrievals. "Who manages the team that owns the payments service, and what is their on-call schedule?" needs one answer before the next question can even be asked. A single search cannot handle either well. Query decomposition splits a complex question into simpler sub-questions, answers each with its own retrieval, and combines the results.
Two kinds of decomposition
| Parallel | Sequential (multi-hop) | |
|---|---|---|
| Example | "Compare the India and UK leave policies" | "What is the on-call schedule of the team that owns the payments service?" |
| Sub-questions | Independent: India policy? UK policy? | Dependent: Which team owns payments? Then: what is that team's on-call schedule? |
| Execution | All at once | One after another, feeding answers forward |
Planning the sub-questions
Ask the LLM for a plan as structured output. Each step can refer to earlier answers with a placeholder such as {answer_1}.
from pydantic import BaseModel
import anthropic
from rag_helpers import ask
from retriever import DenseRetriever # from the top-k search lesson
client = anthropic.Anthropic()
class Plan(BaseModel):
needs_decomposition: bool
sub_questions: list[str] # may contain {answer_1}, {answer_2}, ... placeholders
PLANNER = """Break the user's question into the smallest set of simple search questions needed
to answer it (at most 4). If a step depends on an earlier answer, refer to it as {answer_N}.
If the question is already simple, set needs_decomposition to false and return it unchanged.
Question: """
def plan(question: str) -> Plan:
response = client.messages.parse(
model="claude-opus-5-5",
max_tokens=1000,
output_config={"effort": "low"},
messages=[{"role": "user", "content": PLANNER + question}],
output_format=Plan,
)
return response.parsed_output
print(plan("What is the on-call schedule of the team that owns the payments service?"))
needs_decomposition=True sub_questions=['Which team owns the payments service?',
'What is the on-call schedule of {answer_1}?']
Executing the plan
def answer_from(question: str, retriever: DenseRetriever, k: int = 4) -> tuple[str, list]:
hits = retriever.search(question, k=k)
context = "\n\n".join(f'<source id="{h.id}">\n{h.text}\n</source>' for h in hits)
answer = ask(f"<sources>\n{context}\n</sources>\n\nAnswer briefly using only the sources. "
f"If they do not contain the answer, say 'not found'.\n\nQuestion: {question}")
return answer, hits
def decomposed_answer(question: str, retriever: DenseRetriever) -> str:
p = plan(question)
if not p.needs_decomposition:
return answer_from(question, retriever)[0]
answers: dict[str, str] = {}
notes = []
for n, sub_q in enumerate(p.sub_questions, start=1):
concrete = sub_q.format(**answers) if answers else sub_q # fill in earlier answers
answer, hits = answer_from(concrete, retriever)
answers[f"answer_{n}"] = answer
notes.append(f"Q{n}: {concrete}\nA{n}: {answer}\nSources: {[h.id for h in hits]}")
return ask("Using the step-by-step findings below, answer the original question. "
"Keep the source ids as citations. Say clearly if any step was not found.\n\n"
+ "\n\n".join(notes) + f"\n\nOriginal question: {question}", effort="medium")
Independent sub-questions (those without placeholders) can run in parallel threads to save time; the example runs them in order for simplicity.
Placeholders are filled with whole answers, which may be long sentences. For production use, ask the step to return a short entity (for example with a structured output field) before substituting it. Also stop early when a step returns "not found", rather than searching with a meaningless sub-question.
Cost and when to use it
Decomposition multiplies LLM calls and retrievals: a three-step plan means at least five LLM calls. Use it when questions genuinely combine several facts, such as comparisons, multi-entity questions and chains of references, and let the planner return the question unchanged otherwise. For open-ended research questions, an agent that decides its own steps is often a better fit: see Agentic RAG and Multi-Hop Retrieval.
Key takeaways
- Decompose complex questions into simple sub-questions with their own retrieval.
- Parallel sub-questions are independent; sequential ones feed earlier answers into later steps.
- Plan with structured output, execute step by step, then synthesise with citations.
- Skip decomposition for simple questions; it multiplies cost and latency.