RAG-Fusion (default)

Multi-Query RAG diagramQuestion, Rewriter, Search 1, Search 2, Search 3, Fuse, Generator, Answer. Step captions follow the diagram.QuestionRewriterSearch 1EV cost per mileSearch 2Camry fuel economySearch 3insurance + upkeepFuseReciprocal Rank FusionGeneratorAnswerMulti-Query RAG diagramQuestion, Rewriter, Search 1, Search 2, Search 3, Fuse, Generator, Answer. Step captions follow the diagram.QuestionRewriterSearch 1EV costper mileSearch 2Camry fueleconomySearch 3insurance+ upkeepFuseReciprocalRank FusionGeneratorAnswer

All steps
Parts of this pattern
Question Input / output
A question with several facets, such as 'Is a Model Y cheaper to run than a Camry?'.
Rewriter LLM call
Rewrites the one question into three search queries, each worded the way a relevant document would be.
Search 1 Memory
The retriever, run with the first query; it returns its own ranked list of document IDs.
Search 2 Memory
The same retriever, run in parallel with the second query, returning a second ranked list.
Search 3 Memory
The same retriever with the third query, returning a third ranked list.
Fuse Aggregator
Merges the ranked lists, one per rewrite plus one for the original question: each document scores the sum of 1/(60 + rank) over every list it appears in.
Generator LLM call
Answers the original question from the top fused documents, citing each one.
Answer Input / output
An answer covering every facet the queries reached, with citations.
Question → Rewriter
The question goes to the rewriter before any search runs.Is a Model Y cheaper to run than a Camry?
Rewriter → Search 1
The first rewritten query. The original question is searched too, as a fourth query, so a drifting rewrite cannot crowd it out.Tesla Model Y electricity cost per mile at home charging rates
Rewriter → Search 2
The second rewritten query, searched at the same time.Toyota Camry 2.5L real-world fuel economy mpg
Rewriter → Search 3
The third rewritten query, for a facet the question only implied.EV versus petrol car insurance and maintenance costs per year
Search 1 → Fuse
The first ranked list of document IDs.1. D3 (EV charging costs) 2. D7 (5-year cost of ownership) 3. D1
Search 2 → Fuse
The second ranked list.1. D7 (5-year cost of ownership) 2. D9 (Camry mpg test) 3. D4
Search 3 → Fuse
The third ranked list.1. D5 (insurance by drivetrain) 2. D2 3. D7 (5-year cost of ownership)
Fuse → Generatortop 5
The fused top documents. One that appears in every list rises above any single list's winner.D7 0.0484, D3 0.0164, D5 0.0164, D9 0.0161, D2 0.0161
Question → Generator
The original question goes to the generator, so it answers what was asked rather than a rewrite.Question: Is a Model Y cheaper to run than a Camry?
Generator → Answer
The answer, citing the fused documents.Usually yes: about 4 cents a mile to charge at home [D3] against about 11 for fuel [D9], though insurance runs higher [D5]. Over 5 years the Model Y costs less to run [D7].

Sub-questions

Split the question into sub-questions, retrieve and answer each one separately, then combine the sub-answers into the final answer.

Multi-Query RAG: Sub-questions diagramQuestion, Decomposer, Retriever, Answer 1, Answer 2, Answer 3, Combiner, Answer. Step captions follow the diagram.QuestionDecomposerRetrieverone search per sub-qAnswer 1EV cost per mileAnswer 2Camry fuel costAnswer 3insurance + upkeepCombinerAnswerMulti-Query RAG: Sub-questions diagramQuestion, Decomposer, Retriever, Answer 1, Answer 2, Answer 3, Combiner, Answer. Step captions follow the diagram.QuestionDecomposerRetrieverone searchper sub-qAnswer 1EV costper mileAnswer 2Camryfuel costAnswer 3insurance+ upkeepCombinerAnswer

All steps
Parts of this pattern
Question Input / output
A question with several parts, such as 'Is a Model Y cheaper to run than a Camry?'.
Decomposer LLM call
Splits the question into independent sub-questions, each answerable on its own.
Retriever Memory
Searches the index once per sub-question and hands each branch its own documents.
Answer 1 LLM call
Answers the first sub-question from its own documents only.
Answer 2 LLM call
Answers the second sub-question from its own documents only.
Answer 3 LLM call
Answers the third sub-question from its own documents only.
Combiner LLM call
Writes the final answer from the three sub-answers, keeping their citations.
Answer Input / output
The combined answer, with each part traceable to its sub-answer.
Question → Decomposer
The question goes to the decomposer first.Is a Model Y cheaper to run than a Camry?
Decomposer → Retriever3 sub-qs
The independent sub-questions, each searched separately.1. What does a Model Y cost per mile to charge? 2. What does a Camry cost per mile in fuel? 3. How do their insurance and maintenance costs compare?
Retriever → Answer 1
The documents found for the first sub-question, and only those.[D3] EV charging costs at home and on the road; [D7] 5-year cost of ownership
Retriever → Answer 2
The documents found for the second sub-question.[D9] Camry real-world mpg test; [D4] average US fuel prices 2026
Retriever → Answer 3
The documents found for the third sub-question.[D5] insurance premiums by drivetrain; [D2] EV maintenance schedules
Answer 1 → Combiner
The first sub-answer, with its citations.About 4 cents a mile charging at home [D3].
Answer 2 → Combiner
The second sub-answer.About 11 cents a mile at 32 mpg and $3.50 a gallon [D9][D4].
Answer 3 → Combiner
The third sub-answer.Insurance about 15% higher for the Model Y [D5]; maintenance lower, with no oil changes [D2].
Question → Combiner
The original question goes to the combiner, so the final answer addresses it as asked.Question: Is a Model Y cheaper to run than a Camry?
Combiner → Answer
The final answer, built from the three sub-answers.Usually yes. Energy costs about 4 cents a mile against 11 [D3][D9], which outweighs the higher insurance [D5].

Step-back query

Search with an abstracted, more general version of the question as well as the question itself, so the answer gets background principles as well as specifics.

Multi-Query RAG: Step-back query diagramQuestion, Step-back, Specific search, General search, Generator, Answer. Step captions follow the diagram.QuestionStep-backSpecific searchGeneral searchGeneratorAnswerMulti-Query RAG: Step-back query diagramQuestion, Step-back, Specific search, General search, Generator, Answer. Step captions follow the diagram.QuestionStep-backSpecificsearchGeneralsearchGeneratorAnswer

All steps
Parts of this pattern
Question Input / output
A specific question, such as 'Is a Model Y cheaper to run than a Camry?'.
Step-back LLM call
Writes one more general question about the principles behind the original.
Specific search Memory
The retriever, run with the original question, finding documents about these two cars.
General search Memory
The same retriever, run with the step-back question, finding background on what drives running costs.
Generator LLM call
Answers the original question, using the background documents for principles and the specific ones for facts.
Answer Input / output
An answer that explains why as well as how much, with citations.
Question → Step-back
The question goes to the step-back model.Is a Model Y cheaper to run than a Camry?
Question → Specific search
The original question is searched as it stands, in parallel with the step-back.Is a Model Y cheaper to run than a Camry?
Step-back → General searchstep-back q
The more general question, searched for background.What determines the running cost of an electric car compared with a petrol car?
Specific search → Generator
Documents about the two cars themselves.[D7] Model Y vs Camry: 5-year cost of ownership; [D9] Camry real-world mpg test
General search → Generator
Background documents about what drives running costs.[B2] Electricity vs petrol price per mile explained; [B5] Why EV insurance costs more
Generator → Answer
The answer, combining principles and specifics.Usually yes. Electricity costs less per mile than petrol [B2], so the Model Y saves about 7 cents a mile [D7], partly offset by higher insurance [B5].

← → neighboursesc atlas

Knowledge & Memory

Multi-Query RAG

Also known as RAG-Fusion · Query expansion · Query decomposition · Sub-question retrieval

Rewrite one question into several queries, retrieve for each in parallel, then fuse the ranked lists before generating.

RAG-Fusion: No single inventor · see source

Raudaschl named and popularized RAG-Fusion in October 2023. Generating several queries and fusing their ranks predates it in information retrieval (query expansion; Reciprocal Rank Fusion, Cormack et al., 2009), and LangChain's MultiQueryRetriever appeared around the same time, so there is no single inventor.

Sub-questions: No single inventor · see source

LlamaIndex shipped its SubQuestionQueryEngine in May 2023. Decomposing a question into sub-questions predates it in prompting research (least-to-most, Zhou et al. 2022; Self-Ask, Press et al. 2022); LlamaIndex packaged it for retrieval.

Step-back query: Introduced by Zheng et al. (2023) · Google DeepMind

Step-back prompting first asks the model a higher-level question about the concepts behind the original, and retrieves for it alongside the original (ICLR 2024).

Measured over one run

RAG-Fusion

Nodes
8. Boxes in the diagram: models, tools, stores, people and plain code.
LLM calls
2. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
Tool calls
0. Messages sent to a tool node in the first scenario.
Steps
4. Steps in the first scenario, with repeated steps counted every time they run.
Scenarios
1. Distinct example runs you can switch between on the diagram.
Loop
no. Whether the flow cycles back on itself until a condition is met.
Parallel
yes. Whether two or more messages are in flight at the same time.
Branch
no. Whether the flow takes one path out of several, chosen at run time.
Human in the loop
no. Whether a person appears as a node, to approve, correct or answer.
Autonomy
low. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
Cost
medium. Relative token spend per request.
Latency
medium. Relative wall-clock time per request.

Sub-questions

Nodes
8. Boxes in the diagram: models, tools, stores, people and plain code.
LLM calls
5. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
Tool calls
0. Messages sent to a tool node in the first scenario.
Steps
4. Steps in the first scenario, with repeated steps counted every time they run.
Scenarios
1. Distinct example runs you can switch between on the diagram.
Loop
no. Whether the flow cycles back on itself until a condition is met.
Parallel
yes. Whether two or more messages are in flight at the same time.
Branch
no. Whether the flow takes one path out of several, chosen at run time.
Human in the loop
no. Whether a person appears as a node, to approve, correct or answer.
Autonomy
low. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
Cost
medium. Relative token spend per request.
Latency
medium. Relative wall-clock time per request.

Step-back query

Nodes
6. Boxes in the diagram: models, tools, stores, people and plain code.
LLM calls
2. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
Tool calls
0. Messages sent to a tool node in the first scenario.
Steps
3. Steps in the first scenario, with repeated steps counted every time they run.
Scenarios
1. Distinct example runs you can switch between on the diagram.
Loop
no. Whether the flow cycles back on itself until a condition is met.
Parallel
yes. Whether two or more messages are in flight at the same time.
Branch
no. Whether the flow takes one path out of several, chosen at run time.
Human in the loop
no. Whether a person appears as a node, to approve, correct or answer.
Autonomy
low. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
Cost
medium. Relative token spend per request.
Latency
medium. Relative wall-clock time per request.

Counted from the first scenario's timeline when the site is built.

Problem

One query gets one angle. Ask “is a Model Y cheaper to run than a Camry?” and a vector search returns pages about EV charging, because that is what the question’s wording is closest to. The Camry’s fuel economy, and the insurance and maintenance costs the question only implied, sit further away and never make the top five. The answer comes out one-sided, and nothing in the pipeline notices.

How it works

Multi-query RAG asks the question several ways before it searches. A rewriter model turns the question into three queries, each worded the way a relevant document would be: “Tesla Model Y electricity cost per mile”, “Toyota Camry real-world fuel economy”, “EV versus petrol insurance and maintenance costs”.

All three searches run at once: search 1, search 2 and search 3 each return their own ranked list. The fuser merges them with Reciprocal Rank Fusion: every document scores 1/(60 + rank) for each list it appears in, and the scores add up. A five-year cost-of-ownership article (D7) tops only one list, but it appears in all three, so it rises to first, ahead of documents that won a single list. Agreement across wordings is the signal.

The generator then gets the top fused documents and the original question, not any of the rewrites, and writes an answer that covers every facet.

The tabs show two other ways to fan out. Sub-questions splits the question into independent parts and answers each separately before a combiner merges them; this suits questions that are really several questions. Step-back query adds one more general question (“what determines the running cost of an EV compared with a petrol car?”) and retrieves background for it alongside the specifics.

When to use

  • Questions are ambiguous, have several facets, or are worded badly.
  • Recall matters more than cost: missing a relevant document is worse than an extra search.
  • Your retriever is sensitive to wording, as dense embeddings often are.

When to avoid

  • Queries are already precise, such as lookups by name or ID. The rewrites add cost and can drift off topic.
  • The sub-questions depend on each other (“who is the CEO, and where did she study?”). A fixed fan-out cannot use one answer to form the next query; use agentic RAG or self-ask.
  • The latency budget cannot absorb a rewriter call before the searches start.

Trade-offs

You pay one extra model call and N extra searches per question. The searches run in parallel, so latency grows by about one rewriter call, while cost grows with N. Fusion is cheap and needs no model. The gain is recall, and the risk is drift: a rewrite that wanders off topic pulls in documents the generator then has to ignore.

Failure modes

  • Query drift: a rewrite answers a different question, and its documents crowd out relevant ones. Always include the original question as one of the queries.
  • Answering the rewrite: the generator sees a rewritten query and answers that instead. Give it the original question only.
  • Redundant rewrites: the three queries are near-paraphrases and return the same list, so you pay for nothing. Ask for different angles, not different wording.
  • Lost depth: fusing many shallow lists favours documents that are vaguely relevant everywhere over one that is precisely relevant once. Keep the per-query depth modest and consider a reranker after fusion.

Build it with your AI

RAG-Fusion

Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.

Implement the Multi-Query RAG pattern in this codebase, using the LLM client and language we already use.

## Goal

Answer a question from a document index by searching with several model-written rewordings of it in parallel, fusing the ranked results with Reciprocal Rank Fusion, and answering from the fused top documents.

## How it works

1. Call the rewriter model with the Query rewriter prompt, filling in the question. Parse its reply into a list of N queries (default 3), drop any identical to the original, and add the original question as one more query.
2. Run every query against the retriever in parallel, keeping each ranked list of document IDs (default top 10 per query).
3. Fuse the lists with Reciprocal Rank Fusion: each document scores the sum of 1 / (60 + rank) over every list it appears in, with ranks starting at 1.
4. Sort by fused score, drop duplicates, and keep the top k documents (default 5).
5. Call the generator model with the Grounded answer prompt, passing the original question, never a rewrite, and the k fused documents, and return its answer.

## Prompts

### Query rewriter prompt

```markdown
You write search queries for a document index.

<question>{{question}}</question>

Write {{n}} different search queries that together cover what someone
would need to read to answer the question. Vary the wording and the angle:
use the terms a document on the subject would use, not the question's own.
Return one query per line, with no numbering and no commentary.
```

### Grounded answer prompt

```markdown
Answer the question using only the documents below. They were retrieved
with several rewordings of the question, so some may cover only part of it.

<question>{{question}}</question>
<documents>{{documents}}</documents>

Cite the document ID after each claim, like [D7]. If a part of the
question is not covered by the documents, say which part.
```

## Stop rule

Stop after one fan-out, one fusion and one generation. If every retrieval returns nothing, say the index has no answer instead of generating.

## Requirements

- Make the number of rewrites, the per-query depth, k and the RRF constant (default 60) configurable.
- Run the retrievals concurrently with a per-query timeout; a failed or timed-out query drops out of the fusion instead of failing the request.
- If the rewriter's reply cannot be parsed, fall back to searching with the original question alone.
- Log each query with its ranked IDs and the fused scores, so a surprising answer can be traced to the query that pulled its document in.
- Add a test with a stub LLM returning 3 queries and a fake retriever keyed by query: D7 ranks 2, 1 and 3 in their lists, and the original question gets an empty list. Assert 4 retrieval calls and D7 first with score exactly 1/62 + 1/61 + 1/63.

Sub-questions

Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.

Implement the Multi-Query RAG pattern (Sub-questions variant) in this codebase, using the LLM client and language we already use.

## Goal

Answer a multi-part question by splitting it into independent sub-questions, answering each from its own retrieval, and combining the sub-answers into one final answer.

## How it works

1. Call the decomposer model with the Decomposer prompt, filling in the question. Parse its reply into a list of 2 to 4 independent sub-questions.
2. For each sub-question, in parallel, retrieve the top k documents (default 4).
3. For each sub-question, call the answer model with the Sub-answer prompt, passing that sub-question and only its own documents.
4. Call the combiner model with the Combiner prompt, passing the original question and every sub-question with its sub-answer.
5. Return the combined answer, with the sub-answers attached so a reader can check each part.

## Prompts

### Decomposer prompt

```markdown
Break the question into the smallest set of independent sub-questions
that together answer it.

<question>{{question}}</question>

Each sub-question must make sense on its own and must not depend on the
answer to another one. Write 2 to 4 sub-questions, one per line, with no
numbering and no commentary.
```

### Sub-answer prompt

```markdown
Answer the sub-question using only the documents below.

<sub_question>{{sub_question}}</sub_question>
<documents>{{documents}}</documents>

Give a short, factual answer and cite the document ID after each claim.
If the documents do not answer it, reply exactly: not found
```

### Combiner prompt

```markdown
Answer the original question from the sub-answers below.

<question>{{question}}</question>
<sub_answers>{{sub_questions_and_answers}}</sub_answers>

Keep every citation. If a sub-answer is "not found", say which part of
the question could not be answered.
```

## Stop rule

Stop once the combiner has answered. A sub-question with no documents gets the sub-answer 'not found', and the combiner must say which part it could not answer.

## Requirements

- Cap the sub-questions (default 4); if the decomposer returns more, keep the first four and log the rest.
- Run the retrieve-and-answer branches concurrently, each with its own timeout.
- Give each sub-answer call only its own documents, so one branch's sources cannot leak into another's answer.
- Reject sub-questions that depend on another's answer (that needs a loop, as in agentic RAG). If the decomposer's reply cannot be parsed or yields fewer than 2 sub-questions, answer the original question with a single retrieval.
- Add a test with a stub LLM that splits a question into 3 sub-questions; assert 3 retrievals, 3 sub-answer calls each holding only its own documents, and one combiner call holding all 3 sub-answers.

Step-back query

Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.

Implement the Multi-Query RAG pattern (Step-back query variant) in this codebase, using the LLM client and language we already use.

## Goal

Answer a specific question by retrieving for it and for a more general step-back question in parallel, then answering from both sets of documents.

## How it works

1. Call the step-back model with the Step-back prompt, filling in the question. It returns one more general question about the principles or background behind it.
2. Retrieve the top k documents (default 4) for the original question and, in parallel, for the step-back question.
3. Call the generator model with the Answer with background prompt, passing the original question, the step-back question and the two document sets, labelled separately.
4. Return the answer.

## Prompts

### Step-back prompt

```markdown
You are an expert at stepping back from a specific question to the
general principle or background it depends on.

<question>{{question}}</question>

Write one more general question whose answer would give the background
needed to answer the original. Return only that question.
```

### Answer with background prompt

```markdown
Answer the question. Use the background documents for principles and
the specific documents for facts.

<question>{{question}}</question>
<step_back_question>{{step_back_question}}</step_back_question>
<background_documents>{{background_docs}}</background_documents>
<specific_documents>{{specific_docs}}</specific_documents>

Cite the document ID after each claim.
```

## Stop rule

Stop after one step-back question, two retrievals and one generation. If both retrievals come back empty, say the index has no answer.

## Requirements

- Keep the original-question retrieval even when the step-back retrieval succeeds; the specifics come from it.
- Label the two document sets separately in the generator prompt, so the model knows which documents are background.
- If the step-back call fails, fall back to the original retrieval alone instead of failing the request.
- Log the step-back question next to both result lists.
- Add a test with a stub LLM returning a fixed step-back question and a fake retriever; assert the retriever was called twice, once with each question, and the generator prompt holds the step-back documents inside <background_documents> and the original-question documents inside <specific_documents>.

Prompt templates

RAG-Fusion

Query rewriter prompt
You write search queries for a document index.
<question>{{question}}</question>
Write {{n}} different search queries that together cover what someone
would need to read to answer the question. Vary the wording and the angle:
use the terms a document on the subject would use, not the question's own.
Return one query per line, with no numbering and no commentary.
Grounded answer prompt
Answer the question using only the documents below. They were retrieved
with several rewordings of the question, so some may cover only part of it.
<question>{{question}}</question>
<documents>{{documents}}</documents>
Cite the document ID after each claim, like [D7]. If a part of the
question is not covered by the documents, say which part.

Sub-questions

Decomposer prompt
Break the question into the smallest set of independent sub-questions
that together answer it.
<question>{{question}}</question>
Each sub-question must make sense on its own and must not depend on the
answer to another one. Write 2 to 4 sub-questions, one per line, with no
numbering and no commentary.
Sub-answer prompt
Answer the sub-question using only the documents below.
<sub_question>{{sub_question}}</sub_question>
<documents>{{documents}}</documents>
Give a short, factual answer and cite the document ID after each claim.
If the documents do not answer it, reply exactly: not found
Combiner prompt
Answer the original question from the sub-answers below.
<question>{{question}}</question>
<sub_answers>{{sub_questions_and_answers}}</sub_answers>
Keep every citation. If a sub-answer is "not found", say which part of
the question could not be answered.

Step-back query

Step-back prompt
You are an expert at stepping back from a specific question to the
general principle or background it depends on.
<question>{{question}}</question>
Write one more general question whose answer would give the background
needed to answer the original. Return only that question.
Answer with background prompt
Answer the question. Use the background documents for principles and
the specific documents for facts.
<question>{{question}}</question>
<step_back_question>{{step_back_question}}</step_back_question>
<background_documents>{{background_docs}}</background_documents>
<specific_documents>{{specific_docs}}</specific_documents>
Cite the document ID after each claim.

Related patterns

Composes well with:

Sources

  1. Forget RAG, the Future is RAG-Fusion (Raudaschl, 2023)
  2. rag-fusion reference implementation (Raudaschl)
  3. RAG-Fusion: a New Take on Retrieval-Augmented Generation (Rackauckas, 2024)
  4. Query Rewriting for Retrieval-Augmented Large Language Models (Ma et al., 2023)
  5. Take a Step Back: Evoking Reasoning via Abstraction in Large Language Models (Zheng et al., 2023)
  6. Sub Question Query Engine (LlamaIndex documentation)

From the Agentic Atlas by Joran Kikke · Edit on GitHub · Suggest a correction