Sub topic 13
Letting models call tools and APIs
This chapter assumes you can write basic Python, read JSON, and call an HTTP API. It also assumes you have seen how to get structured output from a model and validate it. Tool use builds directly on that.
What tool use is
A language model only produces text. It cannot check the weather, query a database, or send an email. Tool use (also called function calling) closes that gap. You describe a set of functions to the model. When the model decides one is needed, it replies with a structured request to call it. Your program runs the function and sends the result back.
The model never executes anything. It proposes a call as data, and your code decides whether to run it.
The request-and-response loop
Each tool definition usually has a name, a plain-language description, and a JSON Schema for its arguments. The model chooses a tool based mostly on the description, so write descriptions the way you would explain the function to a new colleague.
Running the call safely
The model's output is untrusted input. Treat it as you would a form field submitted by a stranger. Look the name up in an allow-list, and never use eval or build a function name from model text.
import json
def get_weather(city):
return {"city": city, "temp_c": 18}
TOOLS = {"get_weather": get_weather}
def run_tool_call(call):
fn = TOOLS[call["name"]]
return json.dumps(fn(**call["arguments"]))
print(run_tool_call({"name": "get_weather", "arguments": {"city": "Oslo"}}))This prints {"city": "Oslo", "temp_c": 18}. The dictionary TOOLS is the allow-list. If the model asks for a name that is not in it, the lookup raises a KeyError instead of running something unexpected.
Common errors
- Unknown tool or bad arguments. The model may invent a tool name or omit a required argument. Catch the error and return it to the model as text so it can correct itself.
- Vague descriptions. Two tools with overlapping descriptions lead to the wrong one being chosen. Make each tool do one thing.
- Too many tools. A long list makes selection less reliable and uses up context. Offer only the tools a task needs.
- Unvalidated arguments. Check types and ranges before acting, as you did for structured output.
- Side effects without a check. A tool that sends money or deletes data should require confirmation or limited permissions. Give each tool the least access it needs.
Read tools and write tools
Separate tools that only read from tools that change something. Read tools, such as search or lookup, are low risk and can run automatically. Write tools deserve a confirmation step, logging, and narrow permissions. Doing this limits the damage when the model chooses badly or when untrusted text in a document tricks it, a risk covered in the safety chapter.
When one call is not enough, the program can repeat the loop above until the model answers without requesting a tool. That pattern is the basis of agents, covered in the next chapter.
Exercise
The model returns a tool call named "deleteuser", but your TOOLS dictionary only contains "getweather". What should your code do, and why?
Show answer
It should not run anything. The lookup should fail, and the code should catch the error and either report it back to the model as text or stop. The model's output is untrusted, so only functions in your allow-list may run.