Function Calling (Tool Use)
Function calling, or tool use, lets a language model request an external function with JSON arguments that the application runs and returns.
Function calling, also called tool calling or tool use, is the ability of a large language model (LLM) to answer with a request to run an external function instead of plain text. The application describes each available tool with a name, a purpose, and a JSON Schema for its arguments. When a request needs live data or an action, the model replies with the tool's name and JSON arguments, the application runs the function, and the model writes its answer from the result. The model never executes anything itself. Meta's Toolformer showed in February 2023 that a model could teach itself to insert API calls into its own text, and OpenAI added function calling to its API on June 13, 2023, with models fine-tuned to detect when a function is needed and to format arguments that match its signature. Every major model provider now supports it, and open models learn it through their instruction tuning and chat templates.
How Function Calling Works#
A tool call is a round trip between the model and the application, repeated until the model has what it needs:
The code below plays both sides of one round trip in the format of OpenAI's function calling guide, with the model's reply written out so it runs without an API key:
import json
from jsonschema import validate
# The tool definition sent with the request: a name, a purpose, and a JSON Schema for the arguments
tool = {
"type": "function",
"name": "get_order_status",
"description": "Look up the shipping status of an order by its number.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string", "pattern": "^[0-9]+$"}},
"required": ["order_id"],
},
}
# The model's output is a function call whose arguments arrive as a JSON string
call = {"type": "function_call", "call_id": "call_1", "name": "get_order_status", "arguments": '{"order_id": "4417"}'}
args = json.loads(call["arguments"])
validate(args, tool["parameters"]) # Raises before anything runs if the arguments break the schema
# The application runs its own function and returns the result under the same call_id
orders = {"4417": "Shipped, arriving Thursday"}
print({"type": "function_call_output", "call_id": call["call_id"], "output": orders[args["order_id"]]})It prints {'type': 'function_call_output', 'call_id': 'call_1', 'output': 'Shipped, arriving Thursday'}, the item the application appends to the conversation before calling the model again. Setting "strict": true on a tool makes the API apply constrained decoding, so the arguments the model generates always match the schema.
Choosing and Chaining Tools#
Models can emit several independent calls in one turn, such as checking two orders at once, and an application can force a specific tool, require some tool, or forbid tools for a turn. Chaining calls across turns, where each result decides the next call, is the loop that turns a model into an AI agent running inside an agent harness. The Berkeley Function Calling Leaderboard evaluates these skills: picking the right function, filling its arguments, making parallel and multi-turn calls, and declining when no tool fits. Because tool results return to the model as input, a web page or document fetched by a tool can carry a prompt injection, so applications give tools the least privilege they need and ask a person before purchases, deletions, or sent messages.
Vision models make natural tools because their output is already structured. In Ultralytics YOLO, Results.summary() returns each object a YOLO26 model finds as a dictionary of class name, class index, confidence, and box coordinates, a list that json.dumps turns directly into a tool result, so a chat model or vision-language model (VLM) can count and locate objects in a photo by calling predict mode.
Function Calling vs. Related Terms#
- Computer use agents: Operate software through screenshots, clicks, and keystrokes, which covers applications without an API. A function call reaches the same result in one structured request wherever an API exists.
- Structured outputs: Constrains a model's final reply to a schema, such as extracting fields from an invoice. Function calling uses the same schemas to request an action and expects a result back.
- Retrieval-augmented generation (RAG): Adds retrieved documents to the prompt. A search tool can implement RAG, and agentic RAG lets the model decide when to search and what to search for.
- Model Context Protocol (MCP): An open protocol, introduced by Anthropic in November 2024, through which applications discover tools served by external programs. The model still invokes an MCP tool through function calling.










