A user's question is not always a good search query.
Consider this conversation:
- User: "How does authentication work in this application?"
- User: "What happens if it expires?"
The second question is understandable to a human, but a retriever receiving it independently has very little context.
This is one problem query rewriting attempts to solve.
Instead of sending the original question directly to the retriever, an LLM reformulates it into a more searchable query.
But there is an important engineering question:
Does rewriting actually improve retrieval, or does it simply add another LLM call?
1. Three approaches worth understanding
Original Query Retrieval
Send the user's question directly to the retriever.
Example:
What happens if it expires?
LLM-based Query Rewriting
Use conversation history to generate a standalone question.
Example:
What happens when the application's authentication token expires?
HyDE (Hypothetical Document Embeddings)
Instead of rewriting the question, generate a hypothetical passage that might answer it. Embed that passage and use its vector to retrieve actual documents.
These approaches solve related problems, but they are not interchangeable.
2. What does research tell us?
The paper Query Rewriting for Retrieval-Augmented Large Language Models (EMNLP 2023) introduced the Rewrite-Retrieve-Read framework.
Its central idea is to reformulate a user's query before retrieval, addressing the mismatch between the wording of a question and the knowledge needed to answer it.
Research also shows that rewriting is not guaranteed to improve every query.
A 2025 study, Masking or Mitigating? Deconstructing the Impact of Query Rewriting on Dense Retrieval, investigates how query rewriting affects dense retrieval and the biases that can influence retrieved results.
More recent work, Better Together: Complementary Query Rewriting Under a Strong RAG Baseline (2026), examines rewriting alongside strong retrieval pipelines.
An important takeaway is that different rewriting strategies can retrieve complementary evidence, but combining them does not guarantee improvements across every dataset.
3. Where can query rewriting fail?
I would pay particular attention to these failure cases:
- Lost terminology: The model replaces exact product names, identifiers, or error codes with generic descriptions.
- Incorrect assumptions: The rewriter adds context that the user never provided.
- Over-expansion: A focused question becomes a broad search query.
- Additional latency: The rewriting model introduces another inference step before retrieval.
- Unnecessary rewriting: Already-clear questions are modified without a meaningful reason.
A rewritten query can sound better while retrieving worse documents.
That distinction matters.
4. A practical implementation
A simple implementation should preserve the original question and use conversation history only when needed.
def rewrite_query(question: str, history: str, llm) -> str:
prompt = f"""
Rewrite the question as a standalone
search query using the conversation history.
Preserve:
- Product names
- Error codes
- Dates
- Technical terminology
Do not introduce unsupported information.
Return only the rewritten query.
Conversation:
{history}
Question:
{question}
"""
return llm.invoke(prompt).content.strip()
The next step is to retrieve using both the original and rewritten queries, then merge and rerank the results.
This avoids treating the LLM's rewrite as automatically more accurate than the user's original wording.
5. What should we evaluate?
Before adding query rewriting to a production RAG pipeline, compare it against the original retrieval baseline.
Useful metrics include:
- Recall@K: Are relevant documents present in the top K results?
- MRR: How highly ranked is the first relevant result?
- nDCG@K: Does the ranking reflect the relevance of retrieved documents?
- Latency: How much additional time does rewriting introduce?
- Cost: What is the additional inference cost per query?
Keep the corpus, embedding model, retrieval configuration, and evaluation questions fixed when comparing approaches.
Also, evaluate different query categories separately:
- Short and ambiguous questions
- Conversational follow-up questions
- Questions containing exact identifiers
- Already-specific technical questions
An overall average can hide important regressions.
6. My engineering takeaway
I wouldn't enable query rewriting for every request by default.
A better approach is to introduce it selectively:
- Detect whether the question depends on previous conversation context.
- Rewrite only when the original query needs clarification.
- Preserve the original query as a retrieval candidate.
- Merge results and rerank them.
- Measure whether the additional retrieval quality justifies the latency and cost.
The goal isn't to make every query sound smarter. It's to retrieve the right evidence with the least unnecessary processing.
That's the difference between adding an LLM feature and engineering a better RAG system.
References:
- Query Rewriting for Retrieval-Augmented Large Language Models — EMNLP 2023
- HyDE: Precise Zero-Shot Dense Retrieval without Relevance Labels — 2022
- Masking or Mitigating? Deconstructing the Impact of Query Rewriting on Dense Retrieval
- Better Together: Complementary Query Rewriting Under a Strong RAG Baseline — 2026
Top comments (0)