Course contentsShow
← Machine Learning and Deep Learning
Lesson 2073 of 3,53844. AI Agents: FoundationsFree lesson

Function Calling API Mechanics

How OpenAI-style function calling works: model outputs function calls, you execute them, return results to model.

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

  1. You provide tool schemas to the model alongside your prompt (as covered in Tool Schema Definition)
  2. Model decides whether to respond with text or a function call
  3. If calling a function, the model outputs JSON like:
    {
      "name": "get_weather",
      "arguments": {"location": "Paris", "unit": "celsius"}
    }
    
  4. You parse this output and execute the actual function in your environment
  5. You return the result as a new message in the conversation (typically with role "tool" or "function")
  6. 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.