Sub topic 7
Letting the Model Skip Retrieval
This chapter assumes you know the agent loop from the previous chapter, the search_docs tool schema, and basic Python. It covers how the model decides whether to search, and how to tune and check that decision.
Why skipping matters
Classic RAG retrieves on every message. A user who types "Thanks!" still triggers an embedding call, a vector search, and four unrelated chunks pasted into the prompt. That wastes tokens and time. It can also mislead the model, because it tends to use whatever context it is given.
In an agent loop, retrieval is one tool the model may call. When a reply has no tool_calls, the loop treats it as the final answer. So skipping needs no new code. The model skips by not calling the tool.
Where the decision comes from
The model has no separate "retrieve or not" switch. Two pieces of text steer it:
- The tool description. In the schema you saw earlier, "Use it for questions about product behavior, configuration or limits. Do not use it for greetings or general knowledge" sets both sides of the boundary.
- The system prompt. "Search when you need facts" tells the model when searching is appropriate.
Write both so the boundary is concrete. "Search when helpful" is too vague to guide the model.
Checking the decision
Count how often the model searches on questions where you know the right answer. Wrap the tool so every call is recorded. This sketch reuses run_agent, call_model and search_docs from earlier chapters.
searches = []
def logged_search(query, top_k=4):
searches.append(query)
return search_docs(query, top_k)
cases = [
("Thanks, that helped!", False),
("What is the capital of France?", False),
("What is the maximum upload size on the Pro plan?", True),
]
for question, should_search in cases:
searches.clear()
answer = run_agent(question, call_model, {"search_docs": logged_search}, tool_specs)
did_search = len(searches) > 0
status = "ok" if did_search == should_search else "MISMATCH"
print(f"{status}: {question!r} searched={did_search}")Run a few dozen such cases, not three. The mismatches show which way the model errs.
Two failure modes
| Failure | Symptom | Usual fix |
|---|---|---|
| Under-retrieval | Confident answers about product limits from memory | State in the prompt that product-specific facts always require a search |
| Over-retrieval | Searches on greetings or general knowledge | Add "do not use for..." examples to the tool description |
Under-retrieval is the more dangerous one. The answer reads fine and nothing signals that it is ungrounded.
Follow-up questions add a second problem. "And what about the free plan?" has no meaning without the earlier turns. The schema tells the model to write self-contained queries for this reason. Include a few follow-up cases in your test set.
Exercise
Your agent answers "What is the rate limit for the export API?" directly, without searching, and gives a plausible but wrong number. Name the failure mode and give two changes that reduce it.
Show answer
This is under-retrieval. First, change the system prompt or tool description to say that any question about this product's limits, configuration or behavior must be answered from a search. Second, add this question to your test cases with an expected search, so a regression shows up in the mismatch count. Requiring citations of passage ids also helps, because an answer with no citation is easy to flag.