Sub topic 5
Turning retrieval into a tool
This chapter assumes you know basic Python and what a prompt and completion are. It also assumes you know what an embedding is and how the classic retrieve-then-generate pipeline works. You should also know how tool calling works: the model emits a structured request, your code runs it, and you send the result back.
From a fixed step to a function the model calls
In classic RAG, your code calls search(embed(question), k) before every answer. The model never sees the search step. In agentic RAG, you wrap the same search in a function and describe it to the model. The model then decides whether to call it, with what query, and how many times.
The retriever does not change. What changes is who controls the call.
The tool definition
The model sees only the name, the description and the parameter schema. These three things decide how well it uses the tool.
{
"name": "search_docs",
"description": "Search the product documentation. Use it for questions about product behavior, configuration or limits. Do not use it for greetings or general knowledge. Call it again with a different query if the first results do not answer the question.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "A short, self-contained search query. Do not rely on earlier messages for meaning."
},
"top_k": {
"type": "integer",
"description": "Number of passages to return, from 1 to 8."
}
},
"required": ["query"]
}
}Field names for tool definitions differ between providers, so adapt this shape to yours. Three choices matter here:
- The description says when to call the tool and when not to. This is where the decision to skip retrieval comes from.
- It invites a retry. Without that sentence, many models give up after one weak result.
- The
querydescription asks for a self-contained query. The retriever sees only this string, not the conversation, so "what about its limit?" returns noise.
The function behind the schema
The tool function is a thin wrapper. It enforces limits that the schema cannot guarantee, because models sometimes send out-of-range values.
class StubRetriever:
# Replace with your real vector search.
def search(self, query, k):
corpus = [
{"id": "limits-1", "text": "The API allows 100 requests per minute per key."},
{"id": "limits-2", "text": "Batch jobs may run for at most 15 minutes."},
]
return corpus[:k]
my_retriever = StubRetriever()
def search_docs(query, top_k=4):
# The model sees ids and text, never raw vectors.
top_k = max(1, min(int(top_k), 8))
chunks = my_retriever.search(query, top_k)
return [{"id": c["id"], "text": c["text"][:1200]} for c in chunks]
print(search_docs("rate limit", top_k=99))The wrapper does three things:
- It clamps
top_kto 1 through 8, so one call cannot flood the context. - It truncates each passage to 1,200 characters, which caps the cost of a call.
- It returns ids, so the model can cite
[limits-1]in its answer.
Exercise
A user asks, "And what is its maximum size?" The model calls search_docs with the query "its maximum size" and gets irrelevant passages. Which part of the tool definition should you change, and how?
Show answer
Change the description of the query parameter so it requires a self-contained query, for example "Do not rely on earlier messages for meaning". The retriever sees only the query string, so the model must put the missing subject (such as "upload file maximum size") into it.