Tools & Function Calling

Function Calling

A model feature where the LLM returns a structured request to call a named function with JSON arguments. Your code runs the function and sends the result back.

Last updated: October 17, 2026

What Is Function Calling?

Function calling lets a model ask your application to run code. You describe each function with a name, a description and a JSON Schema for its parameters. When the model decides a function is needed, it does not run anything itself. It returns the function name and a JSON arguments object, your code executes the call, and you send the result back so the model can continue or write its final answer. OpenAI and Google call this function calling, and Anthropic calls the same pattern tool use.

Two controls matter in production. Schema enforcement: OpenAI strict mode makes calls follow your schema instead of best effort, though Chat Completions stays non strict by default, and Anthropic offers strict: true on a tool definition. Tool choice: OpenAI tool_choice accepts auto, required, one forced function or an allowed_tools subset, and Gemini offers auto, any, none and validated modes. A model can also request several calls in one turn, which you can switch off.

Code Example

python
import json


def get_order_status(order_id: str) -> dict:
    return {"order_id": order_id, "status": "shipped"}


TOOLS = {"get_order_status": get_order_status}

# Shape of a function call item from the OpenAI Responses API (mocked)
call = {
    "type": "function_call",
    "call_id": "call_123",
    "name": "get_order_status",
    "arguments": '{"order_id": "A1001"}',
}

result = TOOLS[call["name"]](**json.loads(call["arguments"]))
print({"type": "function_call_output", "call_id": call["call_id"], "output": json.dumps(result)})

The dispatch step of a function calling loop. The call is mocked. In a real loop it comes from the model response, and the output goes back in the next request.

When Should You Use Function Calling?

Use function calling whenever the model must read live data or cause a side effect, such as an order lookup or a database query. Keep descriptions specific, because the model picks tools from them. Strict mode enforces the shape of the arguments, not that the values are allowed, so check permissions in your own code before any side effect. Skip it when the steps are fixed and known. Plain code is simpler.

Common Questions

Does the model execute the function itself?

No. For functions you define, the model only returns the function name and arguments. Your application runs the code and sends the output back, matched to the original call by its id (call_id in the OpenAI Responses API).

Sources

Related Terms

Building with Function Calling?

I've shipped this pattern in real production systems. If you want a second pair of eyes on your architecture, that's what I do.