Tool Use and Function Calling
Tool use, also called function calling, lets a model ask your code to run a function and give it the result. You describe each tool (its name, purpose and inputs); the model decides when it needs one and replies with a request; your code runs the function and sends back the result. It is used to give models current, private or exact data, such as a fee table, a calendar or an order status, and to let them take actions.
The model never runs your code itself. It only asks, and your code stays in charge of what actually happens.
The Tool Use Round Trip
Describing a Tool
| Field | Description |
|---|---|
| name | It is used to identify the tool, such as look_up_fee |
| description | It is used to tell the model what the tool does and when to use it |
| parameters | It is used to define the inputs as a JSON schema |
The helper wraps each tool in the {"type": "function", "function": ...} envelope the chat completions format expects.
Example
from model_api import ModelClient, tool_result
FEES = {(10, "lab"): 45, (10, "field trip"): 30, (11, "lab"): 50, (11, "field trip"): 35}
def look_up_fee(grade, item):
amount = FEES.get((grade, item.lower()))
return f"${amount}" if amount is not None else "No fee found for that grade and item."
tools = [{
"name": "look_up_fee",
"description": "Look up a school fee by grade (9-12) and item, such as lab or field trip.",
"parameters": {
"type": "object",
"properties": {"grade": {"type": "integer"}, "item": {"type": "string"}},
"required": ["grade", "item"],
"additionalProperties": False,
},
}]
client = ModelClient()
messages = [{"role": "user", "content": "How much is the grade 11 lab fee?"}]
while True:
reply = client.chat(messages, tools=tools, max_tokens=1000)
messages.append(reply.message) # the model's turn, including its tool requests
if reply.finish_reason != "tool_calls":
break
for call in reply.tool_calls:
print("Model asked for:", call.name, call.arguments)
messages.append(tool_result(call, look_up_fee(**call.arguments)))
print(reply.text)Example output. The model's request and answer were produced by an AI model on November 16, 2026, to show the shape; this code wasn't run against a live API. The loop ran end to end against a local test server, and look_up_fee really returned $50.
Model asked for: look_up_fee {'grade': 11, 'item': 'lab'}
The grade 11 lab fee is $50.Explanation: The first call includes the tool description. The model replies with finish_reason tool_calls and a tool call holding its inputs. The code adds the model's turn to messages, runs look_up_fee(grade=11, item="lab"), and sends the result back with tool_result(), which carries the same call id. On the second call the model has the real fee, writes the answer, and finish_reason is no longer tool_calls, so the loop ends.
Writing Good Tool Descriptions
The model chooses tools only from their names, descriptions and schemas, so these deserve as much care as the code.
Common Mistakes
Letting the model's inputs reach a database or payment unchecked. Validate them like any user input.
Losing the call id. Each result must carry the id of the request it answers.
Forgetting the model's turn. Add reply.message to the conversation before the tool results, or the model can't match results to its requests.
Interview Corner
Does the model run the function itself?
No. The model returns a request with the tool name and inputs; your code runs the function and sends the result back. This keeps your code in control of every action.
When would you use tool use instead of putting data in the prompt?
When the data is large, changes often, is private per user, or the model must take an action, such as booking a meeting. The model fetches only what it needs, when it needs it.
Practice
Exercise: a second tool. Add a tool next_exam_date(subject) backed by a small dictionary. Ask, "When is the next math exam, and how much is the grade 10 lab fee?" Print every tool request; how many calls does the model make?
Check your understanding:
- What three things describe a tool?
- What does
finish_reasontool_callsmean? - Why must the result carry the call id?
Key Takeaways
Quick reference
| Need | Code |
|---|---|
| Offer tools | client.chat(messages, tools=tools) |
| Model wants a tool | reply.finish_reason == "tool_calls" |
| The request | call.name, call.arguments, call.id for each call in reply.tool_calls |
| Send a result | messages.append(tool_result(call, output)) |
Was this tutorial helpful?