QueryRewriterAgent.
Quick Start
How It Works
Workflow
- Receive user query
- Optionally gather context via tools
- Apply rewriting strategy (BASIC, HyDE, STEP_BACK, SUB_QUERIES, MULTI_QUERY)
- 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
| Feature | Implementation |
|---|---|
| Workflow | Single-step query optimization |
| Observability | --verbose flag |
| Tools | Optional search tools for context |
| Structured Output | RewriteResult with metadata |
Best Practices
Pick the strategy for the query shape
Pick the strategy for the query shape
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.Retrieve with all queries, then merge
Retrieve with all queries, then merge
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.Register domain abbreviations
Register domain abbreviations
Call
add_abbreviations({"K8s": "Kubernetes"}) so the rewriter expands your jargon correctly instead of guessing.Use Prompt Expander for task prompts, not search
Use Prompt Expander for task prompts, not search
This agent optimises for retrieval. To enrich a prompt for task execution rather than search, use the Prompt Expander Agent.
Related
Expand prompts for task execution instead of search.
Generate the embeddings your RAG retrieval runs on.

