Lesson 1 of 9 · How It Works
What Is Tool Calling?
Tool calling (function calling) lets an LLM use your code: describe functions, the model replies with a call, your program runs it and returns the result.
- 5 min read
- Intermediate
- Reviewed
- Explain what tool calling is and why it exists
- Describe the parts of a tool call: name, arguments and call ID
- Read a tool call's arguments and return a result in Python
- Tell tool calling apart from structured outputs
Letting a model use your code
Language models are good at understanding requests and writing text, but they can't check today's exchange rate, read your database or book a table. Tool calling connects them to code that can.
The model never runs anything. It decides which tool would help and what to pass it; your code does the rest.
What a tool call looks like
Different providers wrap it differently, but every tool call has three parts:
| Part | Example | Why it matters |
|---|---|---|
| Name | get_exchange_rate | Which function to run |
| Arguments | {"base": "EUR", "target": "JPY"} | What to pass it, as JSON |
| Call ID | call_8f2a | Matches the result to the request |
Here is a tool call in the format of OpenAI's Responses API, where the arguments arrive as a JSON string, and the result your code sends back:
import json
# A model doesn't call functions. It replies with structured text describing a call.
# This is what a tool call looks like in OpenAI's Responses API (arguments arrive as a JSON string):
model_output = {
"type": "function_call",
"call_id": "call_8f2a",
"name": "get_exchange_rate",
"arguments": "{\"base\": \"EUR\", \"target\": \"JPY\"}",
}
# Your code reads it, runs the real function, and sends the result back.
def get_exchange_rate(base, target):
rates = {("EUR", "JPY"): 171.4, ("EUR", "USD"): 1.10} # a real app would call a rates API
return {"base": base, "target": target, "rate": rates[(base, target)]}
FUNCTIONS = {"get_exchange_rate": get_exchange_rate}
args = json.loads(model_output["arguments"]) # text -> Python dictionary
result = FUNCTIONS[model_output["name"]](**args)
reply_to_model = {
"type": "function_call_output",
"call_id": model_output["call_id"], # which call this answers
"output": json.dumps(result),
}
print("Model asked for:", model_output["name"], args)
print("We send back: ", reply_to_model)json.loads turns the argument text into a dictionary, FUNCTIONS[name] finds the right Python function, and the reply carries the same call_id so the model knows which request it answers. In Anthropic's Messages API the same exchange uses tool_use and tool_result blocks, with arguments already parsed into an object, as the next lessons show.
What it's used for
- Fresh or private data: prices, weather, orders, documents, database rows.
- Exact work: calculations, date arithmetic, running code.
- Actions: sending a message, creating a ticket, booking a slot.
- Agents: tool calling in a loop is how agents act on the world (see Introduction to AI Agents).
Tool calling vs structured outputs
Use structured outputs when you want data back (“extract the invoice number and total”). Use tool calling when the model needs something done or looked up before it can answer. Under the hood they use the same skill: producing JSON that matches a schema.
Common mistakes
Read a tool call's arguments, which arrive as JSON text, the way your code must before running the tool.
import json
call = {"name": "book_table", "arguments": "{\"restaurant\": \"Nonna's\", \"people\": 4, \"time\": \"19:30\"}"}
# The arguments arrive as a JSON string. Turn them into a dictionary,
# then print each argument on its own line, like:
# restaurant = Nonna's
When a model “calls a function”, what actually happens?
Summary
- Tool calling lets a model ask your code to run functions, by returning a structured name, arguments and call ID.
- Your program parses the arguments, runs the function and returns the result with the matching ID.
- It gives models fresh data, exact computation and the ability to act.
- Structured outputs return data as the answer; tool calling requests an action first.
Next: describing a tool precisely with JSON Schema.
Finished reading? Tick it off to track your progress.