Sub topic 7
Letting the model skip retrieval
This chapter assumes you can read basic Python and know the search_docs tool and the run_agent loop from the earlier chapters. It covers one decision in the loop: whether the model searches at all on a given turn.
Why classic RAG cannot skip
The classic pipeline from earlier runs embed, search and generate for every message. That has three costs when the message does not need documents.
- Latency and money. Each retrieval adds an embedding call, a search, and thousands of extra prompt tokens.
- Noise. A greeting such as "thanks, that helps" still gets four chunks attached. The model may try to use them and produce an answer about a topic nobody asked about.
- Wrong evidence. Some questions are about the conversation itself ("summarize what you just said") or about general knowledge ("what does HTTP 404 mean?"). The nearest chunks in your index are unrelated to these, but the pipeline passes them in anyway.
When retrieval is a tool, the model can answer directly. Skipping is the case where the model returns a reply with no tool calls on its first step. In run_agent, that is the if not tool_calls: return reply["content"] branch on the first loop iteration.
Three kinds of turns
Sort incoming messages by what they need.
| Turn type | Example | Right behavior |
|---|---|---|
| Needs no documents | "Hi", "Thanks", "Rewrite that in one sentence" | Answer directly |
| Needs general knowledge only | "What is a JSON Schema?" | Answer directly, if your product accepts answers from model memory |
| Needs your documents | "What is the rate limit on the Pro plan?" | Call `search_docs` |
The second row is a policy choice, not a technical one. A support bot for a regulated product may want every factual answer to come from documents. A coding assistant may be happy to explain general concepts from memory. Decide this first, then write it into the prompt.
How the model makes the choice
The model has no separate "should I retrieve" switch. It decides from the text you give it, in two places.
- The tool description. The
search_docsschema says: "Use it for questions about product behavior, configuration or limits. Do not use it for greetings or general knowledge." This sentence is the main control. Name the topics that require search and the ones that do not. - The system prompt. State the policy in plain words. For example: "If the question is about the product, search before answering. If you are unsure whether the documentation covers it, search."
Notice the last sentence. A good policy resolves doubt toward retrieving, because the two errors are not equally costly.
- Needless search costs a few hundred milliseconds and some tokens.
- Needless skip can produce a confident, wrong answer from model memory, with no citation for a reader to check.
Follow-up questions
The hardest skip decisions come in multi-turn chats. Consider this exchange.
- User: "What is the maximum upload size?"
- Assistant: searches and answers "50 MB [doc-12]".
- User: "And for the Pro plan?"
The third message needs retrieval, even though it looks like a short chat reply. A model that treats it as conversation might guess. The query it sends must also stand alone, because "And for the Pro plan?" would match nothing useful. This is why the schema says the query should be "self-contained" and should "not rely on earlier messages for meaning". Query rewriting gets its own chapter, but the skip decision and the query wording are linked: the model must first notice that the follow-up is a factual question.
Forcing or forbidding retrieval
Most providers let you override the model's choice per request. The parameter names differ, so check your provider's documentation. The options usually look like this:
- auto: the model decides. This is the default and the subject of this chapter.
- required: the model must call a tool. Use it on the first step when your product only answers from documents.
- none: the model must not call tools. The
run_agentcode uses an empty tool list for the same purpose when it forces a final answer.
A common hybrid is required on step one and auto afterward. That removes skipping entirely and keeps the loop's ability to stop. Pick it when the cost of a wrong ungrounded answer is high and most traffic is real questions anyway.
Measuring skip behavior
You cannot tune the description without data. Build a small labeled set of questions, mark each one as needing retrieval or not, run the agent, and record whether search_docs was called. The wrapper below counts calls without touching the agent code. It takes your agent as a function, so you can pass in run_agent with a real model.
def skip_report(cases, agent):
# cases: list of (question, should_retrieve) pairs.
# agent(question, tools_by_name) returns the final answer string.
wrong = []
for question, should_retrieve in cases:
queries = []
def search_docs(query, top_k=4):
queries.append(query)
return [{"id": "stub", "text": "stub passage"}]
agent(question, {"search_docs": search_docs})
retrieved = len(queries) > 0
if retrieved != should_retrieve:
kind = "needless search" if retrieved else "needless skip"
wrong.append((question, kind))
return wrong
def fake_agent(question, tools):
# Stand-in for a real model: searches only when the text mentions the product.
if "plan" in question.lower() or "limit" in question.lower():
tools["search_docs"](query=question)
return "answer"
cases = [
("Hi there", False),
("What is the limit on the Pro plan?", True),
("Thanks, that helps", False),
("How do I rotate an API key?", True),
]
for question, kind in skip_report(cases, fake_agent):
print(kind, "->", question)Running this prints one line, needless skip -> How do I rotate an API key?, because the fake agent's keyword rule misses that question. With a real model, the same report shows where your description is too vague.
Look at the two error types separately. A high needless-skip rate means the description or prompt is too permissive, and you should name more topics that require search. A high needless-search rate means retrieval is firing on small talk, and you should add examples of what not to search for. Chapter-level evaluation later in the plan covers building larger sets, but even 30 labeled questions reveal most problems.
Edge cases
- Ambiguous questions. "How does billing work?" may be general or product-specific. With the "when unsure, search" rule the model searches, which is usually correct.
- Stale conversation context. The model may answer a new question from passages retrieved two turns ago. That is fine if the passages cover it, and wrong if they do not. The citation requirement from the grounding chapter exposes this, because the answer would cite an id that does not support the claim.
- Tools with overlapping scope. When there are several sources, a vague description on one of them makes the model search the wrong place or skip. Source routing is covered in a later chapter.
- Prompt drift across model versions. A description that produced good skip behavior on one model can behave differently on another. Rerun your labeled set after any model change.
Exercise
Your documentation assistant answers "What is the maximum payload size?" from memory without calling search_docs. The answer is wrong. Name two changes that reduce this failure, and say which error type you would track to confirm the fix.
Show answer
First, tighten the tool description and system prompt so that questions about product limits, configuration and behavior must be searched, and tell the model to search when unsure. Second, force retrieval on the first step with a "required" tool choice, then switch to "auto" on later steps. Track the needless-skip rate on a labeled set that includes questions about limits. It should fall, while the needless-search rate should stay low enough that greetings are still answered directly.