Function calling (also called tool use) lets a large language model reply with a structured request such as get_weather({"location": "Paris, France"}) instead of text, so your code can run that function and send the result back. The model never executes anything itself: you describe the functions with JSON Schema, the model picks one and fills in the arguments, and your application does the work.
That loop is what turns a chatbot into something that can look up live data, query a database or take actions. The examples below use the OpenAI Responses API and the Anthropic Messages API, as documented in October 2026. The shape is the same in both; only the field names differ.
How function calling works, step by step
OpenAI's guide describes the flow in five steps, and Anthropic's works the same way:
- You send a request that includes the user's message and a list of tools the model may call.
- The model returns a tool call: the tool's name and arguments as JSON.
- Your code runs the matching function with those arguments.
- You send a second request containing the conversation so far plus the function's output.
- The model returns a final answer, or asks for more tool calls.
Steps 2 to 4 can repeat many times in one task. An "agent" is mostly this loop running until the model stops asking for tools.
How to define a function the model can call
A tool definition has a name, a description and a JSON Schema for the arguments. In the Claude API:
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
OpenAI uses the same idea with different keys: "type": "function", name, description, parameters (the schema) and an optional strict flag.
The description matters more than anything else. Anthropic's docs call detailed descriptions "by far the most important factor in tool performance" and suggest at least 3 to 4 sentences per tool: what it does, when to use it and when not to, what each parameter means, and what the tool does not return. Claude tool names must match ^[a-zA-Z0-9_-]{1,128}$.
What the model sends back
In the Claude API, a tool call arrives with stop_reason set to "tool_use" and a tool_use block in the content. This is the example from Anthropic's docs:
{
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{ "type": "text", "text": "I'll check the current weather in San Francisco for you." },
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "San Francisco, CA", "unit": "celsius" }
}
]
}
In the OpenAI Responses API, the output contains an item of type function_call. Note that arguments is a JSON-encoded string that you must parse, not an object:
{
"id": "fc_12345xyz",
"call_id": "call_12345xyz",
"type": "function_call",
"name": "get_weather",
"arguments": "{\"location\":\"Paris, France\"}"
}
How to send the result back
Run your function, then add its output to the conversation, linked to the call's ID. In the Claude API, the result goes in a user message as a tool_result block:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "15 degrees"
}
]
}
Two formatting rules cause most errors here. The result must come immediately after the assistant message that made the call, and in that user message the tool_result blocks must come first, before any text. Breaking either rule returns a 400 error such as tool_use ids were found without tool_result blocks immediately after.
In the OpenAI API you append an item of type function_call_output with the same call_id. The output is normally a string; the format inside is up to you (JSON, plain text, an error code). A function with nothing to return should still return a string such as a success message.
Forcing or blocking tool calls with tool_choice
| Behaviour | OpenAI tool_choice | Claude tool_choice |
|---|---|---|
| Model decides (default) | "auto" | {"type": "auto"} |
| Must call some tool | "required" | {"type": "any"} |
| Must call one specific tool | {"type": "function", "name": "get_weather"} | {"type": "tool", "name": "get_weather"} |
| No tool calls | "none" | {"type": "none"} |
Forced tool use is not available everywhere. As of October 2026, Anthropic's docs say any and tool return a 400 error on some models (including Claude Opus 5.5 and Sonnet 5.5) and with manual extended thinking; the recommended alternative there is auto plus strict tool use.
Strict mode: making arguments match the schema
Without strict mode, the model usually follows your schema but is not guaranteed to. Both providers offer "strict": true on a tool definition to guarantee the arguments validate. OpenAI's strict mode has requirements: every object must set "additionalProperties": false, and every property must be listed in required. To make a field optional, allow null as one of its types instead of leaving it out of required.
Handling errors from your function
Functions fail: a network timeout, a bad ID, a missing parameter. Return the error to the model instead of crashing the loop. In the Claude API, set "is_error": true on the tool result:
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "ConnectionError: the weather service API is not available (HTTP 500)",
"is_error": true
}
Make error text actionable. Anthropic's docs suggest messages such as Rate limit exceeded. Retry after 60 seconds. rather than failed, because the model uses the text to decide what to do next. If a call is missing a required parameter, returning Error: Missing required 'location' parameter lets the model retry with it filled in.
Security: treat tool results as untrusted
Tool output often contains text you don't control: web pages, emails, uploaded files, third-party API responses. That text can include instructions aimed at the model (indirect prompt injection). Anthropic's guidance is to keep such content inside tool results rather than the system prompt. On your side, validate every argument before acting on it, exactly as you would validate user input to an API, and require confirmation before tools that delete, pay or send anything.
Function calling vs MCP
Function calling is the mechanism inside one API conversation. Model Context Protocol is a standard for packaging tools as separate servers that any compatible app can discover and call. An MCP-capable app still uses function calling underneath: it lists the servers' tools to the model as functions and forwards each call to the right server.
If the model won't call your tool
Check the description first: vague descriptions are the usual cause. Say plainly when the tool should be used. Anthropic's docs note that a line in the system prompt such as Use the tools to investigate before responding. increases tool use. If the model calls the tool with invented arguments (for example guessing a city the user never named), say in the description that it must ask the user when a required value is missing. Every tool definition is also sent with every request, so trimming unused tools cuts input tokens; larely's token counter estimates how many tokens your tool JSON adds.
Frequently asked questions
- Does the model run the function itself?
- No, not for functions you define. The model only returns the function name and arguments as structured data. Your application runs the code and sends the result back in a follow-up request. Some providers also offer server-side tools, such as Anthropic's web search, which run on the provider's infrastructure instead of in your code.
- What is the difference between function calling and tool use?
- They are names for the same mechanism. OpenAI's docs say function calling and also call it tool calling; Anthropic's docs say tool use and note it is also called function calling. A function is one kind of tool: one whose input is described by a JSON schema.
- Do tool definitions cost tokens?
- Yes. Tool names, descriptions and schemas are sent to the model with every request and are billed as input tokens, as are the tool calls and tool results in the conversation. Anthropic also adds a tool-use system prompt of a few hundred tokens per request, with the exact count listed per model in its docs.
- Can the model call more than one function at once?
- Yes. Both OpenAI and Anthropic support several tool calls in one response. OpenAI lets you set parallel_tool_calls to false to limit it to zero or one call per turn; Anthropic's equivalent is disable_parallel_tool_use inside tool_choice.