How an agent uses tools
8 min read
The model asks, the program acts
A language model can only produce text. It cannot open a file or call a web service. So how does an agent act?
The answer is a Tool callA request from the model to run one of your functions. The model writes the tool name and its inputs as structured text, and your program checks the request, runs the function and sends back the result.. You tell the model which tools exist, and the model replies with a structured request, such as "call get_weather with the city Berlin". Your program reads that request, runs the real function and returns the result as new text. The model then reads the result and decides what to do.
The round trip has five stops.
- The program sends the goal and the list of tools.
- The model replies with a tool call.
- The program checks the call.
- The program runs the tool.
- The program sends the result back, and the model decides the next step.
- The program sendsThe goal and the list of tools go to the model.
- The model asksIt replies with a tool call, a request for one tool with its inputs.
- The program checksIt can refuse, limit or log the call here.
- The program runsThe real function runs and returns a result.
- The result goes backThe model reads it and decides the next step.
Because your program sits in the middle at stops 3 and 4, it can refuse a call, limit it or log it.
A tool call in code
Most providers use JSON for tool calls. The sketch below shows the program's side of stops 3 to 5.
import json
def get_weather(city):
return {"city": city, "temperature_c": 12}
TOOLS = {"get_weather": get_weather}
model_reply = '{"tool": "get_weather", "arguments": {"city": "Berlin"}}'
call = json.loads(model_reply)
if call["tool"] not in TOOLS:
result = {"error": f"Unknown tool: {call['tool']}"}
else:
result = TOOLS[call["tool"]](**call["arguments"])
print(json.dumps(result))It prints the temperature for Berlin. If the model had named a tool that does not exist, the program would return an error message instead of crashing. That message goes back to the model, which can then correct itself.
Check the call before you run it
Never trust a tool call because it came from your own model. Models produce malformed JSON, invent tool names and pass arguments of the wrong type. Validate each call before you run it.
- Is the tool on the list? Reject names you did not define.
- Are the arguments complete and the right type? A missing city should return a clear error, not a crash.
- Is this call allowed right now? Some actions need approval, which the Lesson on permissions covers.
When a call fails, return the reason in plain words, such as "city is required". The model reads that reply and usually fixes the next call.
Tools are the agent's reach
Everything an agent can do in the world passes through its tools. A short, well-chosen list makes an agent capable and predictable. A long list of vague tools makes it confused and risky. The next Lesson shows how to design them.
Who runs the function?
A model writes: call `get_weather` with city Berlin.
A model only writes text. Your program sits between the request and the real function.
What should the program do?
The model asks for a tool called `delete_everything` that you never defined.
Validate every call. A clear error lets the model correct itself on the next step.
What should an error message say?
A tool call is missing the required `city` argument.
Plain errors tell the model what to fix. Agents with clear messages recover on their own.
Your reading position is saved
0 of 20 complete
Lesson complete
How an agent uses tools
Up next