A language model on its own can only write text. It can’t check today’s weather, look up an order or book a table. Function calling (also called tool use) fixes that: you describe your functions to the model, and it can ask your code to run them. The model never runs anything itself; it writes a request, and your code decides.
One question, step by step#
Here’s exactly what passes between your app, the model and your function when someone asks about the weather. The messages are in the real format of the Claude API; other providers use the same idea with slightly different field names.
Predict first
When the model “calls” get_weather, who actually runs the function?
Four things to notice:
- Your app is in the middle of everything. The model and your function never talk directly.
- The model decides whether to use a tool. For 17 × 23 it just answers, because the weather tool can’t help.
- It can ask for several calls at once when they don’t depend on each other, like two cities. Send all the results back together, in one message.
- Every call is a full round trip. The whole conversation is sent again with the result added, because the API remembers nothing between requests.
The description is a prompt#
The model picks tools and fills in arguments based only on the name, the description and the schema you give it. So write them for a smart reader who has never seen your code: what the tool does, when to use it, what each argument means, with an example. A vague description is the most common reason a model calls the wrong tool, or no tool at all.
Compare "description": "weather" with "description": "Get the current weather for a city. Use for questions about today's conditions, not forecasts.". The second tells the model when not to use it, too.
Never trust the arguments#
Models are good at filling in arguments, but not perfect. They can send a word where you wanted a number, leave out a field, or invent one. Check every call against the schema before you run anything, and if it fails, don’t crash: send the problem back as a tool result marked as an error. The model reads it and usually fixes its own call on the next try.
Predict first
Your weather API says the city “Pariss” doesn’t exist. What should your app do?
In code#
The full exchange from the figure, with the Anthropic Python SDK:
import json
import anthropic
client = anthropic.Anthropic() # reads your ANTHROPIC_API_KEY
def get_weather(city):
# Call a real weather API here. A fixed answer keeps the example short.
return {"city": city, "temp_c": 18, "conditions": "light rain"}
tools = [{
"name": "get_weather",
"description": "Get the current weather for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name, e.g. Paris"}},
"required": ["city"],
},
}]
messages = [{"role": "user", "content": "What's the weather in Paris?"}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=messages)
if response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
output = get_weather(**block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": json.dumps(output)})
messages.append({"role": "user", "content": results})
response = client.messages.create(model="claude-opus-5-5", max_tokens=16000, tools=tools, messages=messages)
print(next(b.text for b in response.content if b.type == "text"))That’s one round of tools. When the model needs several rounds, calling tools, reading results, then calling more, you put this in a loop. That loop is an agent.
What to remember
- You describe tools to the model; it replies with a request (tool name plus arguments) and your code decides whether to run it.
- Every tool call is a round trip: run the function, send the result back with the matching ID, and the model continues.
- Write clear tool descriptions, validate every argument, report errors back to the model, and keep risky actions behind a human check.