Sub topic 4
Tool Calling in One Page
This chapter assumes you know basic Python, what a prompt and completion are, and how a classic retrieve-then-generate pipeline works. It covers the one mechanism the rest of the tutorial builds on: tool calling.
The idea
A language model produces text. It cannot run a search or query a database. Tool calling (also called function calling) lets it ask your program to do that work.
You describe each tool to the model with a name, a description, and a JSON Schema for its parameters. The model may reply with a request to call a tool, with arguments. Your code runs the function and sends the result back as a new message. The model then reads the result and either answers or asks for another call.
The model never executes anything. It only proposes a call. Your code decides whether to run it.
The exchange
A tool definition
This is a retrieval tool. The exact field names differ between model providers, but the parts are the same.
{
"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"]
}
}The model sees only this text when it decides what to do, so each part has a job:
| Part | What it does |
|---|---|
| name | Identifies the tool in the model's call |
| description | Tells the model when to call the tool and when not to |
| parameters | Constrains the shape of the arguments |
| required | Lists the arguments the model must supply |
What a call looks like
The model's reply contains an identifier, a tool name, and arguments. Using the shape from the agent loop later in this tutorial, it looks like this:
reply = {
"content": "",
"tool_calls": [
{"id": "call_1", "name": "search_docs", "arguments": {"query": "rate limit for API keys", "top_k": 4}}
],
}Your code looks up the function by name, calls it with the arguments, and appends the result as a message that carries the same id. The id lets the model match each result to the call that produced it.
Common errors
- Vague descriptions. If the description does not say when to use the tool, the model guesses. It may search on every greeting or never search at all.
- Trusting arguments. Arguments are model output. Clamp ranges, check types, and handle unknown tool names.
- Raw output. Return small, readable results such as ids and text. Do not return vectors or huge documents.
- Errors that vanish. Send failures back as a result, for example
{"error": "unknown tool"}, so the model can recover.
Exercise
A model replies with a tool call for search_docs, but the arguments include "top_k": 500, and your tool allows at most 8. Whose job is it to enforce the limit, and why?
Show answer
Your code's. The model only proposes arguments, and its output can be wrong or influenced by untrusted text. The tool function should clamp or reject values outside the allowed range, for example with max(1, min(int(top_k), 8)).