EUniversityAgents and tool use· about 60 min· fast-moving, sources checked often· verified 2026-09-20· EN
Tool use
Be able to define tools with a schema, let a model call them, and handle errors and parsing robustly.
Prerequisites
- CPrompting — steering a language modelrequired
- CPython — the basicsrequired
- ELanguage models — training and generationrequired
Intuition
A language model cannot do arithmetic reliably, look up the current weather or read your database. But it can ask to be allowed to: you describe tools (a name, a description, the parameters as a JSON schema), the model answers with a structured call {"name": "weather", "arguments": {"city": "Umeå"}}, your code runs the tool and sends the result back, and the model formulates the answer.
The model proposes; you execute. That means you:
- validate the arguments against the schema (the model can invent fields),
- limit the tool (read-only, a whitelist, a timeout, a budget),
- return errors as text the model can react to, rather than crashing,
- log every call.
A tool that can delete files or send money should require human confirmation.
Code
import json
from pydantic import BaseModel, ValidationError
class WeatherArgs(BaseModel):
city: str
TOOLS = {
"weather": {"schema": WeatherArgs, "fn": lambda a: {"temp_c": get_temp(a.city)},
"description": "The current temperature in a Swedish city"},
}
def tool_spec():
return [{"type": "function", "function": {"name": n, "description": v["description"],
"parameters": v["schema"].model_json_schema()}} for n, v in TOOLS.items()]
def run_call(call):
v = TOOLS.get(call["name"])
if not v:
return {"error": f"unknown tool {call['name']}"}
try:
args = v["schema"].model_validate(json.loads(call["arguments"]))
return v["fn"](args)
except (ValidationError, json.JSONDecodeError) as e:
return {"error": f"invalid arguments: {e}"} # the model gets a chance to correct itself
except Exception as e:
return {"error": f"the tool failed: {type(e).__name__}"}
# the loop: reply = llm(messages, tools=tool_spec()); if reply.tool_calls: run → append(role="tool") → llm again
Mastery means
- Defines tools with a JSON schema and runs the calls the model proposes
- Validates the arguments and handles errors robustly
- Limits what the tools may do
Sign in to do the exercises and build your mastery up.
Sources
- OpenAI — Function calling — documentation, free to read
- Anthropic — Tool use — documentation, free to read