Sub topic 14
Cost, Latency and Query Drift
This chapter assumes you have read the earlier chapters and can call an LLM, run vector or BM25 search, and read Python. You have seen the rewriting techniques. Each one adds cost and can also make results worse. This chapter shows how to measure both problems and limit them.
What each technique adds
Every rewriting step adds at least one LLM call before retrieval. Calls that depend on earlier output must run one after another, so their delays add up. Calls that do not depend on each other can run in parallel.
| Technique | Extra LLM calls | Extra searches | Can run in parallel? |
|---|---|---|---|
| Conversational rewrite | 1 | 0 | No, it must come first |
| Multi-query expansion | 1 | n | Searches, yes |
| HyDE | 1 | 0 | No, it must come first |
| Decomposition | 1 | one per sub-question | Searches, yes |
| Iterative loop | up to max_steps | up to max_steps | No, each step depends on the last |
The iterative loop is the most expensive. Each step retrieves, then asks the model whether the evidence is enough. Passages accumulate, so the prompt grows with every step.
Estimating the budget
Estimate the worst case before you ship. Take the number of LLM calls, the tokens per call, and the delay per call.
def worst_case(steps: int, llm_seconds: float, search_seconds: float,
prompt_tokens: int, growth_per_step: int) -> tuple[float, int]:
latency = steps * (llm_seconds + search_seconds)
tokens = sum(prompt_tokens + i * growth_per_step for i in range(steps))
return latency, tokens
latency, tokens = worst_case(3, 0.8, 0.2, 500, 400)
print(f"{latency:.1f} s, {tokens} prompt tokens")This prints 3.0 s, 2100 prompt tokens. Three steps cost three seconds before generation starts, and the prompt grows each time.
Query drift
Query drift happens when a rewritten query no longer asks what the user asked. The retriever then finds documents for the wrong question. Common causes:
- The model drops a constraint, such as a product version or a date.
- A multi-query variant broadens the topic until it matches unrelated documents.
- A HyDE passage invents a detail, and the search matches that invention.
- In an iterative loop, each new query is built from the last one, so small errors compound.
Fusion can spread the damage. A drifted query contributes its own ranked list, and any document it ranks highly gains score.
Controls that limit cost and drift
- Cap the steps. Keep
max_stepssmall, often 2 or 3. - Always include the original question. Multi-query search already does this by putting the question first in
queries. The original acts as an anchor in fusion. - Run independent searches in parallel. Variants and sub-questions do not depend on each other.
- Skip rewriting when it is not needed. Route simple, self-contained questions straight to retrieval.
- Cache rewrites. The same question with the same history gives the same rewrite.
- Use a smaller model. Rewriting rarely needs your strongest model.
- Check constraints. Confirm that names, numbers and dates from the question still appear in the rewrite.
import re
def keeps_constraints(original: str, rewrite: str) -> bool:
tokens = re.findall(r"\b[A-Z][\w-]+|\b\d[\w.]*", original)
return all(t.lower() in rewrite.lower() for t in tokens)
print(keeps_constraints("Pricing of Postgres 16?", "What does it cost?"))
print(keeps_constraints("Pricing of Postgres 16?", "Postgres 16 pricing"))The first call prints False and the second prints True. When the check fails, fall back to the original question.
Common errors
- Adding a rewrite step with no measured gain.
- Letting the loop run without a step limit.
- Forgetting that the prompt grows as passages accumulate.
- Fusing lists from drifted queries with the same weight as the original.
Exercise
An iterative loop takes 1.0 s per step (LLM plus search) and runs the full 3 steps. Name two changes that reduce the delay and one that reduces drift.
Show answer
To reduce delay, lower max_steps, or skip the loop for self-contained questions (routing). A cache or a smaller model also helps. To reduce drift, always keep the original question in the search, or check that the rewrite keeps the names, numbers and dates from the original and fall back to the original if it does not.
Exercise
Why can parallel execution speed up multi-query expansion but not the iterative loop?
Show answer
In multi-query expansion the searches for the variants do not depend on each other, so they can run at the same time. In the iterative loop, each query is built from the passages found in the previous step, so the steps must run in order.