Function Calling API Mechanics
What you'll learn: How the model outputs structured function calls that you execute externally and feed back into the conversation.
How It Works
OpenAI-style function calling isn't magic—it's a carefully orchestrated dance between you and the LLM. The model doesn't actually execute functions; instead, it outputs structured JSON specifying which function to call and with what arguments.
Think of it like ordering at a restaurant: the waiter (LLM) doesn't cook your meal—they write down your order in a specific format, hand it to the kitchen (your code), wait for the prepared dish (function result), then describe it back to you.
The Mechanics Flow
- You provide tool schemas to the model alongside your prompt (as covered in Tool Schema Definition)
- Model decides whether to respond with text or a function call
- If calling a function, the model outputs JSON like:
{ "name": "get_weather", "arguments": {"location": "Paris", "unit": "celsius"} } - You parse this output and execute the actual function in your environment
- You return the result as a new message in the conversation (typically with role
"tool"or"function") - Model sees the result and continues reasoning, possibly making another call or generating a final answer
Why This Design?
The model lives in an isolated inference environment with no network access or execution privileges. By outputting instructions rather than executing directly, you maintain control, security, and flexibility—you decide what runs and when.
Key Takeaway: Function calling is a structured protocol where the LLM outputs JSON function call requests, your code executes them in the real world, and you feed results back as conversation messages for the model to continue reasoning.