What Is Function Calling?
OpenAI's function calling (now called tool use in the API) lets the model output structured JSON matching a schema you define — instead of free-form text. This makes calling real code from LLM outputs trivial without fragile regex parsing.
Defining Tools
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}]
The Complete Agentic Loop
import openai, json
def run_agent(user_message):
messages = [{"role": "user", "content": user_message}]
while True:
resp = openai.chat.completions.create(
model="gpt-4o", messages=messages,
tools=tools, tool_choice="auto"
)
msg = resp.choices[0].message
messages.append(msg)
if msg.tool_calls:
for tc in msg.tool_calls:
result = dispatch_tool(tc.function.name, json.loads(tc.function.arguments))
messages.append({"role":"tool","tool_call_id":tc.id,"content":json.dumps(result)})
else:
return msg.content # Final answer
Parallel Tool Calls
GPT-4o can call multiple tools in a single turn — drastically reducing latency for tasks that need several pieces of information simultaneously:
Parallel tool calling can reduce agent latency by 50–70% for tasks requiring multiple independent lookups.
Structured Outputs with Strict Mode
Setting "strict": true in your tool definition guarantees the model's output exactly matches your schema — no extra fields, no missing required properties. Essential for production agents where downstream code depends on the output shape.