Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Prompt template · LLM apps and evals

Query rewriting prompt for RAG search

A good query rewriting prompt gives the model the last few turns and the newest question, then asks for one to three standalone search queries with references resolved. This template keeps product names and error codes exactly as written, uses the documents’ vocabulary, never answers the question, and returns an empty list when no search is needed.

Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.

205 tokens unfilled · 262 with the example below · o200k_base (GPT-4o and later)

Template

The query rewriting prompt template (copy it)

Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.

Prompt (one message, plain text)
You turn user questions into search queries for a {{search_type}} over {{corpus}}.

<context>
The conversation so far:
<history>
{{history}}
</history>
</context>

<constraints>
- Resolve references from the history: replace “it”, “that plan” or “the second one” with what they mean.
- Write one to three queries. Use more than one only if the question has separate parts or could use different terms.
- Use the vocabulary the documents are likely to use, not the user’s slang. Keep product names and error codes exactly as written.
- Don’t answer the question.
- If the question doesn’t need a search (a greeting, a thank-you), return an empty list.
</constraints>

<output_format>
JSON only: {"queries": ["...", "..."]}
</output_format>

<task>
Rewrite the latest user question as standalone search queries that will find the documents needed to answer it.

<latest_question>
{{latest_question}}
</latest_question>
</task>
For an API call: system prompt and user message

With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.

System prompt
You turn user questions into search queries for a {{search_type}} over {{corpus}}.

<constraints>
- Resolve references from the history: replace “it”, “that plan” or “the second one” with what they mean.
- Write one to three queries. Use more than one only if the question has separate parts or could use different terms.
- Use the vocabulary the documents are likely to use, not the user’s slang. Keep product names and error codes exactly as written.
- Don’t answer the question.
- If the question doesn’t need a search (a greeting, a thank-you), return an empty list.
</constraints>

<output_format>
JSON only: {"queries": ["...", "..."]}
</output_format>
User message
<context>
The conversation so far:
<history>
{{history}}
</history>
</context>

<task>
Rewrite the latest user question as standalone search queries that will find the documents needed to answer it.

<latest_question>
{{latest_question}}
</latest_question>
</task>

Variables

What do the variables mean?

{{search_type}}
The retrieval method, such as “keyword search”, “vector search” or “hybrid search”. Keyword search needs exact terms; vector search tolerates paraphrase.
{{corpus}}
What is being searched, such as “our product help centre” or “the engineering wiki”.
{{history}}
The last few turns of the conversation. Three to six turns is usually enough context.
{{latest_question}}
The user’s newest message.

Use

When should I use this query rewriting prompt?

Use it in a chat-based RAG system before retrieval. Follow-up questions such as “what about on the free plan?” mean nothing to a search index on their own; rewriting them into standalone queries is one of the simplest ways to improve what retrieval returns.

For single-turn search boxes, it still helps with vague or multi-part questions, but test whether it beats sending the raw question. Every extra model call adds latency and cost, so measure the retrieval improvement on your own data.

Structure

Why is the prompt structured this way?

The search type and corpus are in the role because they change what a good query is. Keyword search needs the exact terms the documents use, while vector search works better with a natural sentence. Telling the model which it is writing for avoids queries that suit the wrong engine.

Resolving references is the main job, and the constraint gives concrete examples of what to resolve. Keeping product names and error codes exactly as written matters because those are often the only terms that match precisely.

Returning a list, possibly empty, makes the output easy to wire up: run each query, merge the results. The empty case saves a pointless retrieval for messages like “thanks”, and the “don’t answer” rule stops the model from skipping ahead.

Example

Example: a filled-in query rewriting prompt

{{search_type}}
hybrid keyword and vector search
{{corpus}}
a hosting company’s help centre
{{history}}
User: How do I add a custom domain?
Assistant: Go to Sites, then Domains, then Add domain, and follow the DNS steps shown.
User: Is SSL included?
Assistant: Yes. A certificate is issued automatically once the DNS records are verified.
{{latest_question}}
and does that work on the free plan?

What to look for. Taken alone, “does that work on the free plan” is unsearchable. Good output resolves “that” to custom domains and SSL, such as custom domain free plan and SSL certificate free plan, keeping “free plan” in the help centre’s wording.

The complete filled-in prompt (262 tokens)
Filled-in prompt
You turn user questions into search queries for a hybrid keyword and vector search over a hosting company’s help centre.

<context>
The conversation so far:
<history>
User: How do I add a custom domain?
Assistant: Go to Sites, then Domains, then Add domain, and follow the DNS steps shown.
User: Is SSL included?
Assistant: Yes. A certificate is issued automatically once the DNS records are verified.
</history>
</context>

<constraints>
- Resolve references from the history: replace “it”, “that plan” or “the second one” with what they mean.
- Write one to three queries. Use more than one only if the question has separate parts or could use different terms.
- Use the vocabulary the documents are likely to use, not the user’s slang. Keep product names and error codes exactly as written.
- Don’t answer the question.
- If the question doesn’t need a search (a greeting, a thank-you), return an empty list.
</constraints>

<output_format>
JSON only: {"queries": ["...", "..."]}
</output_format>

<task>
Rewrite the latest user question as standalone search queries that will find the documents needed to answer it.

<latest_question>
and does that work on the free plan?
</latest_question>
</task>

Models

Does it work the same in ChatGPT, Claude and Gemini?

Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this query rewriting prompt.

  • All models

    A small, fast model is usually enough for rewriting. It runs on every turn, so its latency adds directly to the time before the answer starts.

  • OpenAI GPT

    Use Structured Outputs with an array of strings for queries. OpenAI notes that a refusal may not follow the schema, so check the refusal field before parsing.

  • Gemini

    Google recommends few-shot examples for Gemini. Two examples of a follow-up question and its rewritten queries, taken from your own logs, teach the resolution pattern quickly.

FAQ

Questions people ask

What is a standalone question?

A search query that makes sense with no conversation around it. “And does that work on the free plan?” means nothing to an index on its own; “custom domain free plan” does. Turning follow-ups into standalone queries before retrieval is one of the simplest ways to improve what a chat-based RAG system finds.

Multi-query or HyDE: which rewrite should I use?

This template does multi-query: up to three queries when a question has separate parts or could use different terms. HyDE (hypothetical document embeddings) works the other way: the model writes a likely answer and you search with its embedding, which only suits vector search. This prompt forbids answering, so HyDE needs a separate prompt.

When should I skip query rewriting?

When the question already stands alone, and for greetings or thanks, where the template returns an empty list. Every rewrite is an extra model call before retrieval, so it adds latency and cost on every turn. Test whether it beats sending the raw question on your own data, and use a small, fast model for it.

Related

Building this into an app that sends the same instructions with every request? Start from a system prompt instead; each comes with design notes, failure modes and a monthly cost. In the system prompt library: Search query rewriter for RAG.

Also useful with this prompt: Text chunker.

Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. In the glossary: rag. Browse all 29 prompt templates.

Keep exploring