Lesson 5 of 7 · A First Agent
Giving a Model Tools
Give a language model tools: a name, description and JSON input schema for each, passed to the API, and safe execution that checks names and arguments first.
- 6 min read
- Beginner
- Reviewed
- Describe a tool with a name, a description and an input schema
- Pass tool definitions to an LLM API
- Run tool calls safely: check the tool name and arguments first
- Write tool descriptions that help the model choose well
Telling the model what it can do
A model can only use tools it has been told about. For each tool you send three things:
- A name, such as
get_weather. - A description: what it does, when to use it, what it returns.
- An input schema: the arguments it takes, their types, and which are required.
Defining and running tools
This program defines one tool, connects it to a Python function, and runs four tool calls the way an agent would, including three that go wrong:
import json
# ---- 1. Describe each tool for the model: name, description, input schema ----
TOOL_DEFINITIONS = [
{
"name": "get_weather",
"description": "Get today's weather for a city. Returns the temperature in °C and a short description.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name, for example 'Lisbon'"}},
"required": ["city"],
},
},
]
# ---- 2. The real functions behind the tools ----
FAKE_WEATHER = {"lisbon": (24, "sunny"), "oslo": (3, "light snow")}
def get_weather(city):
temp, sky = FAKE_WEATHER.get(city.lower(), (None, None))
if temp is None:
return {"error": f"Unknown city '{city}'. Try a major city name."}
return {"city": city, "temp_c": temp, "sky": sky}
FUNCTIONS = {"get_weather": get_weather}
# ---- 3. Run a tool call safely: check the name and the arguments first ----
def run_tool_call(call):
definition = next((t for t in TOOL_DEFINITIONS if t["name"] == call["name"]), None)
if definition is None:
return {"error": f"No tool called {call['name']}."}
missing = [r for r in definition["input_schema"]["required"] if r not in call["input"]]
if missing:
return {"error": f"Missing argument(s): {', '.join(missing)}."}
return FUNCTIONS[call["name"]](**call["input"])
# Tool calls, as a model would produce them
for call in [
{"name": "get_weather", "input": {"city": "Lisbon"}},
{"name": "get_weather", "input": {"city": "Atlantis"}},
{"name": "get_weather", "input": {}},
{"name": "book_flight", "input": {"to": "Oslo"}},
]:
print(json.dumps(call), "->", run_tool_call(call))The good call returns the weather. The other three come back as clear error messages instead of crashing: an unknown city, a missing argument, and a tool that doesn't exist. Models sometimes produce all three; your code should expect them.
Sending tools to the API
The definition above is in the format Anthropic's API uses. With the SDK on your own computer, you pass the list with every request:
response = client.messages.create(
model="MODEL_NAME",
max_tokens=1000,
tools=TOOL_DEFINITIONS,
messages=[{"role": "user", "content": "Do I need a coat in Oslo today?"}],
)
for block in response.content:
if block.type == "tool_use":
print(block.name, block.input) # e.g. get_weather {'city': 'Oslo'}Other providers use the same idea with slightly different field names. OpenAI's Responses API, for example, takes {"type": "function", "name": ..., "description": ..., "parameters": ...}, where parameters holds the same JSON Schema.
Writing good descriptions
The description is the model's only guide to when and how to use a tool. Compare:
| Weak | Strong |
|---|---|
weather: gets weather | get_weather: Get today's weather for a city. Returns the temperature in °C and a short description. Use for questions about current conditions, not forecasts. |
city: string | city: City name, for example 'Lisbon'. Use the English name. |
Common mistakes
Write the input schema for a currency conversion tool, so a model knows exactly which arguments to send.
# Describe a tool for: convert_currency(amount, from_currency, to_currency)
# amount is a number; the two currencies are strings like "EUR"; all three are required.
definition = {
"name": "convert_currency",
"description": "Convert an amount of money from one currency to another.",
"input_schema": {
"type": "object",
"properties": {},
"required": [],
},
}
print(definition["name"])
for name, prop in sorted(definition["input_schema"]["properties"].items()):
print(f"{name}: {prop['type']}")
print("required:", ", ".join(definition["input_schema"]["required"]))
What tells a model when to use a tool and what arguments to send?
Summary
- Each tool is described by a name, a description and a JSON Schema for its inputs.
- Tool definitions are sent with every request; the model replies with tool calls.
- Run tool calls safely: check the name and arguments, and return clear errors.
- Good descriptions (including formats and examples) are the cheapest way to make tools work well.
Next: put it all together and build a minimal agent.
Finished reading? Tick it off to track your progress.