Function calling

Function calling is a feature of AI model APIs in which the model, given descriptions of functions, replies with a structured request naming a function and its arguments, which the developer's code then runs.

A language model cannot run code or reach other systems by itself. Function calling gives it a way to ask. The developer describes a set of functions, each with a name, a plain description and its parameters written as a JSON Schema. When a request needs one, the model replies not with prose but with a structured call naming the function and the arguments to pass. The developer's application runs the function, sends the result back, and the model uses it to write its answer or to make another call. OpenAI's documentation sets this out as a five-step loop, and notes that function calling is also known as tool calling.

OpenAI introduced the feature on June 13, 2023 for gpt-4-0613 and gpt-3.5-turbo-0613, suggesting uses such as chatbots that consult external tools, turning plain requests into API calls or database queries, and pulling structured data out of text. In November 2023 it let models call several functions in one turn. Anthropic offers the same capability as tool use, and distinguishes client tools, which run in the developer's application, from server tools such as web search, which Anthropic runs itself.

Anthropic's documentation uses a weather example. The developer defines a get_weather function that takes a location. Asked about the weather in San Francisco, the model returns a call to get_weather with the location set to San Francisco, CA. The application looks up the forecast, sends back 15 degrees Celsius and partly cloudy, and the model writes a normal sentence from that result.

Early implementations were best-effort, and calls did not always match the schema. Both OpenAI and Anthropic now offer a strict setting that forces calls to follow the schema exactly. That does not make the values right: Anthropic notes that some models may fill in a reasonable guess, such as a city, when the user has not supplied one.

The model only proposes a call; the developer's code decides whether to carry it out. OpenAI warned in 2023 that untrusted data returned by a tool can instruct the model to do things the user did not intend, and recommended asking the user to confirm actions with real-world effects such as sending email or making purchases. Function calling is the basic building block of AI agents and of standards such as the Model Context Protocol.

Common questions

What is function calling in LLMs?

It is a way for a language model to request that an external function be run. The developer describes available functions, the model returns a structured call with arguments when one is needed, and the developer's code runs it and returns the result. This lets a model fetch live data or take actions.

Is function calling the same as tool use?

Essentially yes. OpenAI says function calling is also known as tool calling, and Anthropic calls the same capability tool use. Some providers also run certain tools, such as web search, on their own servers, which the developer does not have to execute.

Does the AI model run the function itself?

No, not for functions the developer defines. The model outputs the name of the function and the arguments; the developer's application decides whether to run it and sends back the result. This gives the developer a point to validate inputs and require user approval.

What is the difference between function calling and JSON mode?

JSON mode makes a model's whole reply valid JSON, with no link to any action. Function calling has the model choose a function and supply arguments, which the application then runs. Both produce structured output, but function calling is designed for taking actions and fetching data.

Finding more

Search for "function calling", "tool use API" or "strict tool schema" together with the provider you use. Provider documentation is the most reliable source, since details such as parallel calls and strict modes differ between APIs and change over time. For security, look for guidance on prompt injection through tool outputs from security researchers and standards groups.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.