Function Calling
Function calling (also called tool use) is a capability of modern LLMs that allows them to generate structured JSON function calls instead of plain text. This bridges the gap between natural language understanding and programmatic action.
When you define a set of functions with their parameters and descriptions, the LLM can decide when to call a function, select the appropriate one, and generate the correct arguments. The application then executes the function and returns the result to the model.
For example, given a "get_weather" function, a user asking "What's the weather in Toronto?" would trigger the model to call get_weather(city="Toronto") rather than generating a text response. The actual weather data is fetched and returned to the model for a grounded response.
Function calling is the foundation of AI agents and tool-using AI systems. It's supported by OpenAI, Anthropic (Claude), Google (Gemini), and other providers, each with slightly different API formats.
How it works
The application sends the model a list of tools, each with a name, a description of when to use it and a JSON Schema for its parameters. The model reads the user's request and the descriptions and decides whether to answer directly or request a call.
The model never runs the function itself. In Anthropic's API, a call to a client tool comes back with stop_reason "tool_use" and a tool_use block holding the tool name and arguments; the application runs the operation and sends the output back in a tool_result block. OpenAI's guide describes the same five-step round trip: request with tools, receive a tool call, execute it, send the output back, receive the final answer or more calls.
By default argument generation is best effort, so arguments can be malformed or invented. Both providers offer a strict mode (strict: true) that makes calls match the declared schema. OpenAI's strict mode requires additionalProperties to be false and every property to be listed as required, with optional fields expressed by allowing null.
Tool definitions cost tokens. Anthropic's documentation notes that tool names, descriptions and schemas count as input tokens, and the API adds a tool-use system prompt whose size depends on the model, so long tool lists make every request more expensive.
Example
A tool definition in the shape Anthropic's documentation uses, and the call the model returns for "What's the weather in San Francisco?".
{
"name": "get_weather",
"description": "Get the current weather for a given location.",
"input_schema": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "City and state, e.g. San Francisco, CA" }
},
"required": ["location"]
}
}{
"type": "tool_use",
"id": "toolu_example_01",
"name": "get_weather",
"input": { "location": "San Francisco, CA" }
} The application calls a real weather service with that location and returns the result as a tool_result with the matching id. The model then writes its answer from the returned data instead of guessing.
Common confusion
Is function calling the same as tool use?
Yes, the two names describe the same capability; Anthropic's documentation says tool use is "also called function calling". Some providers also offer server-side tools, such as web search, that they run themselves, but the request-call-result pattern is the same.
What happens if the user does not give a required argument?
The model may ask for it or may guess. Anthropic's documentation shows a model filling in a location the user never gave when asked "What's the weather?". Validating arguments in application code, and saying in the prompt to ask rather than guess, reduces this.
Related tools & demos
- Agent Engineering Patterns How tool calls fit into planning and control flow.
- AI Agent Cost Simulator Model the token cost of tool-heavy requests.
- JSON Formatter Check and tidy tool schemas and call payloads.
- AI Agents Systems built from repeated tool calls.
Further reading
- Tool use with Claude Claude Developer Platform docs
- Function calling OpenAI API docs
Want to learn more?
Explore more developer terms or read in-depth articles on the blog.
Browse all terms