Session 5· 11· 15 min
First Tool Definition
What you'll learn
- ▸Write a tools[] array with a function schema
- ▸Pass it to chat.completions.create()
- ▸Inspect the model response for tool_calls
The mental model for tool calling
What the model actually does
The model does not EXECUTE your function. It returns a structured REQUEST that says "please call get_mock_weather with city='London'". Your code reads that request, runs the function, and returns the result. This back-and-forth is the tool-calling loop.
Tool calling — the dance
You
send tools + question
Model
returns tool_calls
Your code
runs the function
You
send result back
Model
final natural answer
The code
11_first_tool_definition.py
tools = [{ ①
"type": "function",
"function": {
"name": "get_mock_weather", ②
"description": "Get current weather for a city.", ③
"parameters": { ④
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"],
"additionalProperties": False,
},
"strict": True,
},
}]
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Weather in London?"}],
tools=tools, ⑤
)①tools is a LIST — you can define many in one call.
②The name is what the model will request. Make it snake_case and descriptive.
③The description is how the model decides WHEN to use the tool. Be clear.
④parameters is a JSON Schema for the function arguments — same rules as response_format.
⑤Pass tools= on the API call. The model sees the schemas and may return tool_calls.
$ python 11_first_tool_definition.py
Nothing executes yet
This lesson only DEFINES the tool and inspects what the model returned. Executing it comes in lesson 13.
Knowledge Check
Why does the description field matter so much?
Code Check
What does the parameters field look like?
Recap — what you just learned
- ✓A tool definition is name + description + parameters schema
- ✓description is how the model chooses when to use the tool
- ✓parameters uses the same JSON Schema rules you learned in Phase 1
- ✓strict: true gives guaranteed valid arguments