Skip to content
AgenticAIWiki
  1. Learn
  2. Part 4
  3. 23. Advanced RAG
  4. 23.6

Lesson 23.6 · 6 of 7 · 5 min read · Reviewed 24 Nov 2026

HyDE and multi-query retrieval

By the end, you can

  • Explain how HyDE searches with a hypothetical answer.
  • Use multi-query retrieval and merge its results.
  • Decide when either technique is worth its extra calls.

Lesson 23.4 listed query expansionQuery rewriting: Reformulating the user’s question before retrieval, for example expanding it or splitting it into sub-questions. techniques. This lesson looks closely at two of them: searching with a hypothetical answer (HyDE) and multi-query retrieval.

The gap they close

Questions and documents are written differently. A question is short and casual: “pto carryover?”. The handbook passage is long and formal: “Annual leave you have not used by 31 December may be carried into the next year, up to five days.” Their embeddingsEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together. can sit further apart than you’d hope, even though one answers the other.

HyDE: search with a draft answer

HyDE stands for Hypothetical Document Embeddings. The steps:

  1. Ask a model: “Write a short passage that would answer this question.”
  2. Embed that draft, not the question.
  3. Search the index with the draft’s vector.
  4. Answer the original question from the real chunksChunking: Splitting documents into smaller passages before embedding and indexing them. you retrieved.
question: "pto carryover?"
draft:    "Employees may carry a limited number of unused annual
           leave days into the following year, subject to approval."
search with the draft --> finds the real handbook passage

The draft may get details wrong (it says “subject to approval”; the real policy says “up to five days”). That doesn’t matter, because it is only used to find the right neighbourhood. It must never reach the user as an answer.

Some systems combine the draft with the original question, embedding both or appending one to the other. The Query2doc paper applies a similar idea to both keyword and dense search, appending a generated passage to the query.

Multi-query: several phrasings, one merged list

Instead of one draft, ask the model for three to five different phrasings:

"pto carryover?"
  -> "carry over unused annual leave"
  -> "holiday days roll over to next year"
  -> "maximum leave carried forward"

Search with each, then merge the ranked lists. Reciprocal rank fusion (23.7) is the usual way to merge: a chunk found by several phrasings rises to the top.

Which to use

Situation Try
Short questions, long formal documents HyDE
Vocabulary varies (jargon, synonyms, acronyms) Multi-query
The question names an exact code or id Neither; keyword search (23.2)
Topic the model knows nothing about Multi-query; a HyDE draft may be far off
Tight latency budget Measure first; both add a model call

Cost

Each technique adds one model call before the search, and multi-query adds several searches. Small, fast models usually write good enough drafts and phrasings. Cache rewrites for repeated questions.

Common mistakes

  • Showing the draft. It reads like an answer, so it is tempting. It is unchecked text.
  • No evaluationEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria.. Run your retrieval test set (chapter 26) with and without the technique, and compare recall@kHit rate@k: The share of test questions with at least one relevant chunk in the top k results.. Keep it only if it helps your questions.
  • Too many phrasings. Past four or five, extra queries mostly repeat each other and add cost.
  • Losing filters. Every expanded query must carry the same tenant and date filters as the original (22.7).

Key points

  • HyDE asks a model to draft a plausible answer, embeds the draft, and searches with it, because answers look more like documents than questions do.
  • Multi-query retrieval writes several phrasings of the question, searches each, and merges the lists.
  • The draft is only a search key: never show it to the user or treat it as fact.
  • Both add a model call and extra searches; keep them only if your test set shows better recall.
Hands-on labRuns in the Playground

Search with a draft answer

Compare searching with a short question, with a drafted hypothetical answer (HyDE), and with several phrasings merged by RRF.

You will have: A script that prints the target passage's rank for the question, the HyDE draft and the multi-query merge.

Steps

  1. Read the handbook chunks and the short question 'pto carryover?'.
  2. Rank the chunks using the question alone and note where the leave-carryover passage lands.
  3. Search with the drafted answer (written here as a model might write it) and note the rank.
  4. Search with three phrasings, merge them with RRF, and note the rank.
  5. Answer from the retrieved passage, never from the draft.

Done when

  • The question alone does not rank the carryover passage first.
  • The HyDE draft ranks it first.
  • The multi-query merge ranks it first.

Open in the Playground No API key needed.

Starter code (Python)
import math, re
from collections import Counter

STOP = {"a", "an", "the", "to", "of", "and", "or", "is", "are", "be", "in", "on", "for", "if", "it", "by", "you", "your", "may", "can", "do", "i", "what", "has", "have", "not", "into", "up", "this", "that", "with", "from", "over"}

def words(text):
    out = []
    for w in re.findall(r"[a-z0-9]+", text.lower()):
        if w in STOP:
            continue
        out.append(w[:-1] if w.endswith("s") and len(w) > 3 else w)
    return out

def embed(text):
    """A toy embedding: content-word counts. Real systems use an embedding model."""
    return Counter(words(text))

def cosine(a, b):
    dot = sum(a[k] * b[k] for k in a)
    na = math.sqrt(sum(v * v for v in a.values())); nb = math.sqrt(sum(v * v for v in b.values()))
    return dot / (na * nb) if na and nb else 0.0

def rank_of(target, query_vec, docs):
    order = sorted(docs, key=lambda d: -cosine(query_vec, embed(docs[d])))
    return order.index(target) + 1

HANDBOOK = {
    "leave-carry": "Annual leave you have not used by 31 December may be carried into the next year, up to five days.",
    "leave-book": "Book annual leave through the staff portal at least two weeks ahead.",
    "pto-form": "PTO request form: managers approve requests in the portal.",
    "sick": "Sick leave is paid from the first day; tell your manager before 10am.",
    "carry-bags": "Carry-on bags for business travel must meet airline size rules.",
}
QUESTION = "pto carryover?"
DRAFT = "Unused annual leave days may be carried into the next year, up to a limit."  # as if from a small model
PHRASINGS = ["carry over unused annual leave", "leave days roll into next year", "maximum leave carried forward"]

def search(query):
    qv = embed(query)
    return sorted(HANDBOOK, key=lambda d: (-cosine(qv, embed(HANDBOOK[d])), d))

def rrf(lists, k=60):
    # TODO: sum 1 / (k + rank) for each doc across lists
    return lists[0]

print("question rank:", search(QUESTION).index("leave-carry") + 1)
# TODO: HyDE: search(DRAFT); multi-query: rrf([search(p) for p in PHRASINGS])
Show a solution
import math, re
from collections import Counter

STOP = {"a", "an", "the", "to", "of", "and", "or", "is", "are", "be", "in", "on", "for", "if", "it", "by", "you", "your", "may", "can", "do", "i", "what", "has", "have", "not", "into", "up", "this", "that", "with", "from", "over"}

def words(text):
    out = []
    for w in re.findall(r"[a-z0-9]+", text.lower()):
        if w in STOP:
            continue
        out.append(w[:-1] if w.endswith("s") and len(w) > 3 else w)
    return out

def embed(text):
    """A toy embedding: content-word counts. Real systems use an embedding model."""
    return Counter(words(text))

def cosine(a, b):
    dot = sum(a[k] * b[k] for k in a)
    na = math.sqrt(sum(v * v for v in a.values())); nb = math.sqrt(sum(v * v for v in b.values()))
    return dot / (na * nb) if na and nb else 0.0

def rank_of(target, query_vec, docs):
    order = sorted(docs, key=lambda d: -cosine(query_vec, embed(docs[d])))
    return order.index(target) + 1

HANDBOOK = {
    "leave-carry": "Annual leave you have not used by 31 December may be carried into the next year, up to five days.",
    "leave-book": "Book annual leave through the staff portal at least two weeks ahead.",
    "pto-form": "PTO request form: managers approve requests in the portal.",
    "sick": "Sick leave is paid from the first day; tell your manager before 10am.",
    "carry-bags": "Carry-on bags for business travel must meet airline size rules.",
}
QUESTION = "pto carryover?"
DRAFT = "Unused annual leave days may be carried into the next year, up to a limit."  # as if from a small model
PHRASINGS = ["carry over unused annual leave", "leave days roll into next year", "maximum leave carried forward"]

def search(query):
    qv = embed(query)
    return sorted(HANDBOOK, key=lambda d: (-cosine(qv, embed(HANDBOOK[d])), d))

def rrf(lists, k=60):
    s = {}
    for ranked in lists:
        for rank, doc in enumerate(ranked, start=1):
            s[doc] = s.get(doc, 0) + 1 / (k + rank)
    return sorted(s, key=lambda d: (-s[d], d))

print("question rank:", search(QUESTION).index("leave-carry") + 1)
print("HyDE rank:", search(DRAFT).index("leave-carry") + 1)
print("multi-query rank:", rrf([search(p) for p in PHRASINGS]).index("leave-carry") + 1)
print("answer from:", HANDBOOK[search(DRAFT)[0]])
Quick checkUsers type short questions like "pto carryover?" and the handbook says "annual leave you have not used may be carried into the next year". Which technique targets this gap most directly?
How sure are you?
Pick an answer and how sure you are