AAI Agentic Handbook
Retrieval & Search

Query rewriting

Mid levelReviewed Oct 2026 · fast-movingIn 4 interview questionsIn 2 posts

Turning the user's raw message into one or more better search queries: resolving pronouns, expanding jargon, stripping chit-chat.

In plain English

Users type messy questions, often in the middle of a conversation: 'what about for contractors?' means nothing to a search engine on its own. Query rewriting uses rules or an Large Language Model (LLM) to turn that into a complete, searchable query — 'parental leave policy for contractors' — before retrieval runs. It can also expand abbreviations or produce a few variants of the query.

Analogy

A good reference librarian who listens to your rambling request and writes down the precise thing to look up.

Common pitfall

Letting the rewriter 'improve' precise queries. An LLM will happily rewrite ORA-01555 into 'Oracle database snapshot error', losing the exact token BM25 needed. Skip rewriting for identifier-like queries or always fuse with the original.

How it actually works

Three common rewrites. Contextualisation (standalone question from chat history) is the highest-value one: in multi-turn chat, 20–40% of follow-ups contain pronouns or ellipsis that break retrieval. Expansion (synonyms, acronyms, domain terms) helps BM25 most. Multi-query generates 3–5 paraphrases, retrieves for each and fuses with Reciprocal Rank Fusion (RRF) (often called RAG-Fusion) — +2–6 recall points at 3–5× retrieval cost. Use a small fast model (Haiku-class, GPT-4o-mini-class, or a fine-tuned 1–3B model) with Structured output / decoding to emit JSON {queries: [...], filters: {...}}, and pass the original query as one of the legs so a bad rewrite can't remove a good match. Ma et al. (2023) trained a small rewriter with RL from downstream answer quality. Latency: 150–500 ms for a small hosted model, < 50 ms for a self-hosted 1B — usually the largest added cost in the retrieval path.

What I'd ship on day one

Standalone-question rewrite for every turn after the first, using a small model with JSON output and a 2-second timeout falling back to the raw query. Original query always fused as one leg. Multi-query (3 variants) only for intents where it measurably helps.

Worked example

Internal IT helpdesk chat. Turn 1: 'How do I get VPN access on my new laptop?' Turn 2: 'and on my phone?' Raw retrieval on turn 2 returns phone-reimbursement policy. Rewriter output: {"queries": ["VPN access setup on mobile phone", "GlobalProtect iOS Android enrollment"]} → top hit is the mobile VPN enrollment page. Cost: one 300-token call to a small model, ~200 ms.

In the interview

Recommend it conditionally: "In chat, a standalone-question rewrite is mandatory; multi-query fan-out only when I measure a recall gain worth 3× the retrieval cost." Always keep the original query as a leg. Expect the follow-up: how do you evaluate a rewriter? Compare retrieval recall@20 on the golden set with raw vs rewritten queries, segmented by first-turn vs follow-up.

User
Rewriter
Search
LLM
1
'and on my phone?'User → Rewriter
2
VPN on mobile + rawRewriter → Search
3
retrieve each, RRFSearch → Search
4
top 6 chunksSearch → LLM
5
answer with citationsLLM → User
The rewrite runs before retrieval; the original query stays as a fused leg.

Numbers worth knowing

Follow-ups needing contextualisation
20–40% of turnsmulti-turn assistant traffic (typical sampled logs)
Added latency
150–500 ms hosted, < 50 ms self-hosted 1B~300 input / 60 output tokens
Multi-query recall gain
+2–6 recall@20 points3–5 variants fused with RRF; costs 3–5× retrieval

How to decide

WhenDo this
Multi-turn chatAlways produce a standalone query.
Query looks like an identifier, code or exact titleSkip rewriting; search verbatim.
Recall on paraphrase-heavy queries is low and latency budget allowsMulti-query with RRF, original included.

Cost maths

Per query: ~300 input + 60 output tokens. Assume a small model at $0.25/M input and $1.25/M output (illustrative Oct 2026): 300 × 0.25e-6 + 60 × 1.25e-6 = $0.00015 → $150 per million queries. With Prompt caching of the fixed instructions (~250 tokens, reads at 0.1×) input cost drops ~70%.

Operating it

Time to scale up when…
  • Rewriter p95 > 400 ms — self-host a fine-tuned 1–3B rewriter
  • Rewrite changes identifier tokens in > 1% of queries — add a verbatim-token guard
  • Follow-up recall still < first-turn recall by > 10 points — add history summarisation
What to monitor
  • Recall@20 raw vs rewritten on the golden set, by turn position — alert if rewritten < raw
  • Rewriter timeout / fallback rate — alert above 2%
  • Identifier tokens dropped by rewrite — alert above 0.5% of queries

When it goes wrong

What users saw

Searches for specific legal citations ('§ 1983 claims') started returning general civil-rights articles.

How it was caught

Exact-citation slice of the golden set dropped from 0.9 to 0.55 recall@10 after the rewriter launch.

Root cause

The rewriter normalised '§ 1983' into 'federal civil rights lawsuits', and the original query was not kept as a leg.

Fix

Added the original query as a fused leg, a regex guard that preserves citations and codes verbatim, and the citation slice as a release gate.

Sources

  1. Query Rewriting for Retrieval-Augmented Large Language Models — Ma et al., 2023paper
  2. Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods — Cormack et al., 2009paper

Check yourself

Three questions. Every option is explained once you answer.

1ConceptYour query rewriter produces a standalone query, but you still fuse the user's original query as one retrieval leg. Why?
2ScenarioSingle-turn search box. A user types 'ORA-01555 after 19c upgrade'. Your LLM rewriter is enabled for all traffic. Per the page's decision table, what should happen?
3CalculationA rewrite call uses ~300 input and ~60 output tokens on a small model priced at $0.25/M input and $1.25/M output. What does rewriting cost per million queries, before caching?