Function Calling
Having Python functions the agent could use isn't the same as the LLM actually being able to use them, a model only outputs text, it has no way to directly call a Python function in your program. Function calling (also called tool calling) is the bridge: you describe your functions to the model in a structured format, and the model can respond by asking you to call one, with specific arguments, instead of (or as well as) replying in plain text.
Describing a function to the model
Each tool is described with a JSON Schema, its name, what it does, and what arguments it takes:
description matters more than it looks, the model decides whether and when to call a tool based on how clearly it understands what that tool does from this text alone.
Passing tools to the model
response = client.chat.completions.create(
model='openai/gpt-4o-mini',
messages=messages,
tools=tools,
)
message = response.choices[0].message
if message.tool_calls:
for call in message.tool_calls:
print(call.function.name, call.function.arguments)
This needs a real API key, so it's read-only here. Crucially: the model does not run run_python itself, it can't, it has no way to execute code. It just replies with a structured request, "please call run_python with code='print(2 + 2)'", .arguments arrives as a JSON string your own code still has to parse and act on.
Dispatching a tool call yourself
This part needs no API key at all, it's the exact mechanics your own code has to implement, testable right now with a fake tool call standing in for a real model response:
json.loads(tool_call['arguments']) turns the model's JSON-string arguments back into a real Python dict, function(**arguments) then calls the right function by unpacking that dict as keyword arguments, so {'code': '...'} becomes run_python(code='...').
NOTE
available_functions is a lookup table mapping the name the model was told about (in tools) to the actual function object your code can call, keeping these in sync (every tool you describe to the model needs a matching entry here) is what makes dispatch work at all.