Skip to main content
Rewrite user queries for better RAG retrieval — HyDE, step-back, sub-queries and more — with the QueryRewriterAgent.
The Query Rewriter Agent transforms user queries to improve retrieval quality in RAG applications by bridging the gap between how users ask questions and how information is stored. Agents: 1 — Specialized agent for query optimization.

Quick Start

1

Simple Usage

Rewrite a short query for better retrieval.
2

With a Strategy

Decompose a complex query into focused sub-queries.

How It Works

Workflow

  1. Receive user query
  2. Optionally gather context via tools
  3. Apply rewriting strategy (BASIC, HyDE, STEP_BACK, SUB_QUERIES, MULTI_QUERY)
  4. Return optimized query/queries

Setup

Run — Python

Run — CLI

Run — agents.yaml

Serve API

Rewriting Strategies

BASIC

Expand abbreviations, fix typos, add context to short queries.

HYDE

Generate hypothetical document for better semantic matching.

STEP_BACK

Generate higher-level concept questions for complex queries.

SUB_QUERIES

Decompose multi-part questions into focused sub-queries.

MULTI_QUERY

Generate multiple paraphrased versions for ensemble retrieval.

CONTEXTUAL

Resolve references using conversation history.

Basic Rewriting

Expands abbreviations, fixes typos, and adds context to short keyword queries.

HyDE (Hypothetical Document Embeddings)

Generates a hypothetical document that would answer the query, improving semantic matching.

Step-Back Prompting

Generates broader, higher-level questions to retrieve background context.

Sub-Query Decomposition

Breaks complex multi-part questions into focused sub-queries.

Multi-Query Generation

Generates multiple paraphrased versions for ensemble retrieval.

Contextual Rewriting

Uses conversation history to resolve pronouns and references.

Auto Strategy Detection

Automatically selects the best strategy based on query characteristics.

Custom Abbreviations

Add domain-specific abbreviations for better expansion.

Response Structure

Using Tools for Context

The Query Rewriter Agent can use tools (e.g., search) to gather context before rewriting. The agent decides when to use tools based on the query.

Custom Tools

CLI Usage

Query rewriting is available via CLI and works with any command.

With Any Prompt

With Deep Research

Custom Tools File

Configuration Options

Integration with RAG


Monitor / Verify

Features Demonstrated

Best Practices

Use BASIC for short keywords, HYDE for semantic gaps, SUB_QUERIES for multi-part questions, and MULTI_QUERY for ensemble retrieval. AUTO chooses for you when in doubt.
For MULTI_QUERY and SUB_QUERIES, run retrieval on every returned query and combine results. Using only the primary query throws away the recall the strategy bought you.
Call add_abbreviations({"K8s": "Kubernetes"}) so the rewriter expands your jargon correctly instead of guessing.
This agent optimises for retrieval. To enrich a prompt for task execution rather than search, use the Prompt Expander Agent.
Expand prompts for task execution instead of search.
Generate the embeddings your RAG retrieval runs on.