Query Rewriting Based on Previous Results
What you'll learn: How to use what the LLM generates to reformulate your query and fetch better context for a more complete answer.
The Core Idea
Sometimes the first retrieval doesn't give you everything you need. The initial generation might reveal gaps, new terms, or follow-up questions that weren't in the original query. Query rewriting based on previous results means looking at what the LLM produced, identifying what's missing or unclear, and reformulating your query to retrieve additional context that addresses those gaps.
Think of it like researching a topic: you read one article, realize it mentions a concept you don't understand, then search specifically for that concept to fill the gap. Your second search is informed by what you learned (and didn't learn) from the first.
How It Works
- Initial retrieval + generation: Retrieve context for the user's query and generate a response
- Analysis: Examine the generated output — did it hedge? Mention missing information? Reference concepts that weren't in the retrieved context?
- Rewrite the query: Craft a new search query targeting the gaps, often more specific or differently phrased
- Second retrieval: Fetch additional documents with the rewritten query
- Enhanced generation: Combine all context and regenerate a more complete answer
Why This Matters
The first pass might give you 70% of the answer. Query rewriting gets you closer to 100% by being adaptive — using what you learned to ask better questions.
Key Takeaway: Query rewriting turns RAG into a multi-step process where each generation informs the next retrieval, creating a self-improving loop that produces more complete and accurate answers.