Tool Calling: Let the Model Look Up an Order
A language model only knows what was in its training data and in the messages of the request, so on its own it cannot tell a customer where their order is today. Tool calling closes that gap. The application describes functions the model may ask for, and the model replies with a request to run one. The application runs it and sends the result back, so the final answer rests on real data the program looked up.
The model asks, the program runs
Picture a customer who writes "Where is my order 4412?". The model cannot open the order database, but the program that calls the model can. With tool calling, the program sends the question together with a short description of a function, say get_order_status, that looks up an order by its number. The model reads both and, in place of guessing, replies with a structured request: "please run get_order_status with order_id set to 4412".
That reply is only text. The model never runs code, and the API does not run the function either. The program receives the request and decides what to do with it: it may run the function, refuse to, check the arguments first or run something else entirely. Whatever it decides, it sends the outcome back to the model in a second request, and the model writes the answer the customer reads.
- Program
- Model
- get_order_status
- Program to Model: Where is 4412? + tools
- Model to Program: tool call: 4412
- Program to get_order_status: runs the function
- get_order_status to Program: status: shipped
- Program to Model: messages + result
- Model to Program: It has shipped
So one question becomes two calls to the model with the program's own work in between. This split is the point of the design: the code that touches real systems stays in the program, where you control it, and the model contributes what it is good at, which is understanding the question and writing the answer.
True or false?
When the model decides to use a tool, the API runs the function on its servers and returns the result.
False. The API only returns the name of the function and its arguments. Running it is always the job of the application.
The program can decline a tool call the model asks for.
True. A tool call is a request in text. The program decides whether and how to act on it, and can report back that it did not run it.
Describing a tool to the model
The model can only ask for functions it has been told about. In a Chat Completions request, the tools parameter is a list of tool definitions sent next to messages. A function tool has "type": "function" and, nested under function, three parts:
- name: what the model writes when it asks for the function. Letters, digits, underscores and dashes, at most 64 characters.
- description: plain words the model reads to decide when to call the function and how. It is the most important part to write well.
- parameters: the arguments, described as a JSON Schema object, the same schema language used to describe the shape of structured output.
tools = [
{
"type": "function",
"function": {
"name": "get_order_status",
"description": (
"Look up the current status of a customer's order. "
"Use it when the customer asks where an order is."
),
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order number, for example 4412.",
}
},
"required": ["order_id"],
},
},
}
]
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)A good test for a definition is whether a new colleague could use the function correctly from the name, the description and the parameter descriptions alone, because that is all the model gets. Say what the function does, what each argument means and in what format, and when the function should be used.
Three properties of definitions matter in practice:
- They cost tokens. The provider adds the definitions to the prompt the model reads, so they count against the context window and are billed as input tokens on every request that carries them.
- The schema guides, it does not bind. On Chat Completions the optional
strictsetting is off by default, so the model is asked to follow the schema but its arguments are not guaranteed to fit it. - Sending tools does not force a call. When tools are present, the model may answer in plain text if it sees no need for one, and the code has to handle both outcomes.
Not every endpoint accepts tools, and even with one provider, support can differ from model to model. A provider may offer tool calling on some of its models only through its newer API. A local server supports it only for models trained for it, and some local model libraries mark those models with a tools label. So check the documentation of the endpoint and try the exact model before you build on it.
True or false?
The description of a tool is a comment for developers, and the model ignores it.
False. The model reads the description to decide when and how to call the function. A vague description leads to wrong or missing calls.
A long list of tool definitions makes every request that carries it more expensive.
True. Definitions are added to what the model reads, so they are counted and billed as input tokens each time they are sent.
The reply that asks for a call
When the model decides to use a tool, its reply is still an assistant message, but it carries something new. Its content is usually empty (null), and its tool_calls field holds one entry per function the model wants run. Each entry has:
- an id, which names this particular call so its result can be matched to it later;
- a function name, one of the names from the definitions;
- the arguments, written by the model as JSON text.
The finish_reason of the choice is tool_calls, which tells the code that the model stopped because it wants a tool run, not because its answer is finished (stop) or cut off (length).
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_order_status",
"arguments": "{\"order_id\": \"4412\"}"
}
}
]
},
"finish_reason": "tool_calls"
}Look closely at arguments: it is a string that contains JSON, not a dictionary. In the openai package it arrives as call.function.arguments, a str, and the code turns it into a dictionary with json.loads.
The model generated that string, so it is not always valid JSON, and it may hold parameters the schema never defined. Treat it like any other model output and check it before trusting it.
import json
choice = response.choices[0]
message = choice.message
if choice.finish_reason == "tool_calls":
call = message.tool_calls[0]
name = call.function.name # "get_order_status"
args = json.loads(call.function.arguments)
print(call.id, name, args["order_id"])
else:
print(message.content) # the model answered in textTrue or false?
Printing message.content on a reply that asks for a tool shows the arguments of the call.
False. The content of such a reply is typically null, so Python prints None. The request sits in message.tool_calls.
A finish_reason of tool_calls means the model stopped to ask for a function, and it can only answer once a later request brings the result.
True. The model stopped to ask for a tool. It writes its answer only after a later request brings the result. Nothing waits in between: the next request must send the whole conversation again.
Sending the result back
The program now runs the function with the parsed arguments and gets a result, for example that order 4412 has shipped. To hand it to the model, it adds two messages to the conversation, in this order:
- the assistant message that asked, with its
tool_callsexactly as they came back; - a tool message: role
tool, the result as text incontent, and intool_call_idthe id of the call it answers.
The order matters because a tool message is only valid as the answer to a call in an earlier assistant message. The id is the link between them: it tells the model which request this result belongs to, which becomes essential as soon as a reply asks for more than one call.
| # | role | What it carries |
|---|---|---|
| 1 | system | the standing instructions of the application |
| 2 | user | Where is my order 4412? |
| 3 | assistant | tool_calls: id call_abc123, get_order_status, {"order_id": "4412"} |
| 4 | tool | tool_call_id call_abc123, content {"status": "shipped"} |
The content of a tool message is text. The format inside it is up to you: plain words, an error code or, most often, the result serialized with json.dumps. The model reads that string the way it reads any other message.
result = get_order_status(args["order_id"]) # your own function
messages.append({
"role": "assistant",
"content": message.content,
"tool_calls": [
{
"id": call.id,
"type": "function",
"function": {
"name": call.function.name,
"arguments": call.function.arguments,
},
}
],
})
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})True or false?
It is enough to send the tool message alone, because the model remembers that it asked for the call.
False. Each request is stateless. The assistant message with the matching tool_calls has to come before the tool message in the same list.
The second request
The model keeps no memory between calls: it sees only the messages of the current request. So the program sends the whole conversation again, now four messages long, and the model reads the question, its own request for the tool and the result together. From that it writes the answer the customer sees, and this time the reply is ordinary text with a finish_reason of stop.
second = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)
final = second.choices[0]
if final.finish_reason == "stop":
print(final.message.content)The answer is only as good as the result it was given. If the tool reports that the order shipped, the model can say so; if the program sends an error, the model can explain that the order was not found. Either way the facts come from the program, and the model turns them into a reply in the voice the system prompt asks for.
Since the same tools travel with the second request, the model is free to ask for another call in place of answering, for example to look up a second order. The reply then has tool_calls again and a finish_reason of tool_calls, so code that only expects text should check the finish reason before it prints the answer.
True or false?
The second request sends the user question, the assistant message with the tool call and the tool message together.
True. The model has no memory of the first request, so everything it needs to write the answer must be in the messages of the second one.
The reply to the second request is always plain text, because the tool result has already been delivered.
False. The model may ask for another tool call instead, so the finish reason still has to be checked.