Skip to main content

Pattern · Retrieval and RAG

Query rewriting

Turn what the user typed into a better search query: standalone, expanded, or split into parts.

Say it in the interview

Because users ask follow-ups, I would add a query-rewrite step that turns the last message and history into a standalone search query, and HyDE only for very short queries. I would log both versions in the trace so we can see when the rewrite caused a miss.

When to use it

  • Follow-up questions that depend on chat history ("and for seniors?")
  • Short or vague queries
  • Questions that need several searches

How it works

  1. User message + chat history → Rewrite into a standalone query
  2. Rewrite into a standalone query → short query → HyDE: draft an answer, embed it
  3. Rewrite into a standalone query → Search
  4. HyDE: draft an answer, embed it → Search

Trade-offs

  • Big gains on conversational search
  • One extra model call per question
  • A bad rewrite can drift from what the user meant

Pitfalls interviewers probe

  • Rewriting every query, even clear ones
  • Not logging the rewritten query, so failures cannot be debugged

Practise it on these questions

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.