OpenAI Function Calling Complete Guide: Complete Developer Guide 2026
Master OpenAI Function Calling Complete Guide with practical examples and production patterns
OpenAI Function Calling: Complete Developer Guide (2026)
Function calling (now usually called tool calling) lets a model decide to invoke functions you define and return structured arguments for them. You describe your tools as JSON Schemas; the model, when appropriate, replies with a tool call (name + JSON arguments) instead of free text; your code runs the function and feeds the result back. This is the foundation of agents, RAG retrieval, and any app where the LLM must take actions reliably.
The core loop
python
pip install openai
from openai import OpenAI
client = OpenAI()tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]
resp = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)
call = resp.choices[0].message.tool_calls[0]
import json
args = json.loads(call.function.arguments) # {"city": "Tokyo"}
result = get_weather(**args) # your real function
feed the result back so the model can answer in natural language
messages.append(resp.choices[0].message)
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
final = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)
print(final.choices[0].message.content)
Production patterns that matter
tool_choice to force, forbid, or auto-select tools. Force a specific tool when you need guaranteed structured output.tool_calls and append one tool message per call id.Function calling vs structured outputs
If you only want a typed object back (not to *run* anything), use structured outputs / response schemas rather than a tool round-trip. If the model must choose actions and you execute them, that's tool calling. Anthropic's Claude offers the same capability through its tool-use API — the concepts transfer directly.
How it powers agents
Every agent framework is, underneath, this loop wrapped in control flow. To see it scaled up, compare OpenAI Assistants vs LangGraph and CrewAI vs AutoGen.
FAQ
Does the model actually run my function? No — it only returns the name and arguments. Your code executes it and returns the result.
Can it call multiple tools? Yes, in parallel; handle each tool_call_id separately.
How do I guarantee valid JSON? Use strict/structured-output mode and validate with Pydantic before executing.
Is this OpenAI-only? No — Claude and others expose equivalent tool-use APIs with the same loop.
Summary
Function calling turns an LLM from a text generator into a controller that can take actions. Define tools as JSON Schemas, run the call loop, validate every argument, and feed results back. Master this and agents, RAG, and tool-augmented apps all become variations on one pattern.
*Last updated: June 2026. Verify the current API shape in the OpenAI docs.*
Also available in 中文.