Papa Programmer
Lesson 11 of 20

How an agent uses tools

8 min read

Optional

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.

  1. The program sends the goal and the list of tools.
  2. The model replies with a tool call.
  3. The program checks the call.
  4. The program runs the tool.
  5. The program sends the result back, and the model decides the next step.
  1. The program sendsThe goal and the list of tools go to the model.
  2. The model asksIt replies with a tool call, a request for one tool with its inputs.
  3. The program checksIt can refuse, limit or log the call here.
  4. The program runsThe real function runs and returns a result.
  5. The result goes backThe model reads it and decides the next step.
One tool call, from request to result

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.

python
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.

The model asks, you actA tool call is a request written as text. Only your program runs the function.
Validate every callCheck the name, the arguments and the permission before running anything.
Return clear errorsA plain failure message lets the model correct its next call.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools