AI Engineering Tutorial · Tools and Agents · Lesson 12 of 25
Tool Calling
Let the model ask your code to run functions (look up an order, search docs) and use the results in its answer.
How tool calling works
- You describe tools to the model: name, description and a JSON schema for the inputs.
- The model replies with a tool call (
stop_reason: tool_use) naming a tool and its arguments. - Your code runs the function and sends the result back as a tool result.
- The model uses the result to answer, or calls another tool.
The model never runs code itself; your application decides what actually happens.
tools = [{
"name": "get_order_status",
"description": "Get the shipping status of an order by its 5-digit order number.",
"input_schema": {"type": "object", "properties": {"order_id": {"type": "string", "pattern": "^[0-9]{5}$"}},
"required": ["order_id"]},
}]
ORDERS = {"10042": "shipped, arrives Friday", "10043": "processing"}
def get_order_status(order_id):
return ORDERS.get(order_id, "no such order")
# what the model sends back when it wants a tool (stubbed)
model_reply = {"stop_reason": "tool_use",
"content": [{"type": "tool_use", "id": "toolu_1", "name": "get_order_status", "input": {"order_id": "10042"}}]}
results = []
for block in model_reply["content"]:
if block["type"] == "tool_use":
output = {"get_order_status": get_order_status}[block["name"]](**block["input"])
results.append({"type": "tool_result", "tool_use_id": block["id"], "content": output})
print(results)
Output
[{'type': 'tool_result', 'tool_use_id': 'toolu_1', 'content': 'shipped, arrives Friday'}]See Tool use overview.
Test yourself
Who executes the function when a model 'calls a tool'?
The model provider's serversYour application codeThe user's browser automaticallyNobody; it is simulated
Show answer
Answer: Your application code
The model only requests the call; your code runs it and returns the result.