Sub topic 8
Rewriting and decomposing queries
This chapter assumes you know the basics of Python, embeddings and vector search, and the agent loop from the previous chapters. Here you will see how the model changes what it searches for, so that retrieval works even when the user's words are a poor query.
Why the user's words are a weak query
Classic RAG embeds the question exactly as typed. That fails in two common ways:
- Context-dependent wording. After "How do I set up SSO?", a follow-up such as "What about the limits?" has no meaning on its own. Embedded alone, it matches nothing useful.
- Compound questions. "Compare the free and pro plan rate limits" needs facts from two places. One query vector lands between them and tends to retrieve neither well.
In agentic RAG the model writes the query itself, so it can fix both problems before searching.
Query rewriting
Rewriting turns the user's message into a short, self-contained search query. The query parameter description in the tool schema does most of the work. In the schema from the earlier chapter it says: "A short, self-contained search query. Do not rely on earlier messages for meaning." The model reads this each time it fills in the arguments.
| User message | Weak query | Rewritten query |
|---|---|---|
| "What about the limits?" (after an SSO question) | `what about the limits` | `SSO configuration limits` |
| "It keeps failing, why?" | `it keeps failing why` | `SSO login error causes` |
Rewriting also covers vocabulary. If the first query uses the user's words and the results are poor, the model can retry with the documentation's terms. This is why the schema description says to call the tool again with a different query.
Query decomposition
Decomposition splits a compound question into sub-queries. The model issues one call per part and combines the results at the end.
Many providers let the model return several tool calls in one reply. The loop you built earlier already handles this, because it iterates over every item in tool_calls. Independent sub-queries can run in the same step.
Some questions are dependent: the second query needs the first result. For "Which plan includes the feature that was added in version 3?", the model must first find the feature name, then search for its plan. These calls happen in separate loop steps, because the model needs to observe the first result before writing the next query.
Prompting for it
You can steer this behavior in the system prompt. Add a rule to the loop's system message:
SYSTEM_PROMPT = (
"Answer from the documentation. Search when you need facts, and cite passage ids.\n"
"Write each search query so it makes sense on its own.\n"
"If a question asks about several things, search for each one separately.\n"
"If results do not answer the question, rephrase the query using different terms."
)Pass this string as the system message in run_agent.
Common errors
- Over-decomposing. Splitting a simple question into five searches adds cost and latency. The step budget from the stopping chapter limits this, but the prompt should ask for splitting only when the question has separate parts.
- Repeating the same query. A model that retries with an identical string gets identical results. Log queries and watch for repeats.
- Drifting from the intent. A rewrite can add terms the user never meant. Keep queries close to the original meaning, and log both the user's text and each query for debugging.
- Hidden dependence on history. A query like "the second option" fails. The parameter description must say the query is self-contained.
Exercise
A user asks, "Does the pro plan support SSO, and how many seats does it include?" Write the search queries a model should issue, and say whether they can run in the same loop step.
Show answer
Two queries: "pro plan SSO support" and "pro plan included seats". They are independent, because neither needs the other's result, so the model can return both tool calls in one step.
Exercise
A user asks, "Who wrote the document that introduced the new billing model?" Why must this be answered in more than one loop step?
Show answer
The second query depends on the first result. The model must first find which document introduced the billing model, then search for that document's author, so it needs to observe the first result before writing the second query.