Function calling is a feature of language model interfaces in which the model, given descriptions of available functions, returns the name of a function and a set of arguments in a structured format, which the application can then run to obtain data or perform an action. A developer might describe a function called get_order_status that takes an order number. When a customer asks where their package is, the model returns a call to that function with the number from the message. The application runs it against its database and gives the result back to the model, which writes a natural reply. It is the foundation of tool calling and of most AI agents.
Background
Before the feature existed, developers who wanted a model to trigger software had to ask for a particular text format and parse it with code, which often failed when the model added commentary or changed the format. In June 2023, OpenAI released function calling for its chat models, trained to detect when a function should be called and to produce valid JSON arguments. Other providers added similar abilities, and the terms were later broadened to tool use. The approach builds on research on models that learn to call external interfaces and on structured output methods.
The request and response flow
- Describe the functions: each has a name, a description and a parameter schema. For example, get_order_status takes an order_number string.
- Send with the conversation: the function list accompanies the messages in the request.
- Model responds: either with normal text or with a function call that names the function and gives arguments as JSON.
- Validate and run: the application checks the arguments, runs the function in its own environment and captures the output.
- Return the result: the output is sent back to the model as a message tied to the call.
- Final answer or another call: the model uses the result to answer, or requests more calls.
The model never runs code. It only proposes a call, so the application controls what actually happens.
Function definitions
A good definition is clear to both a model and a human reader.
- Name: short, descriptive and unique, such as search_orders.
- Description: says what the function does, when to use it and any limits.
- Parameters: a JSON Schema listing each argument with a type, description and constraints such as allowed values, minimum and maximum.
- Required fields: marks which arguments must be supplied.
- Examples: sample values clarify formats, such as dates in year-month-day form.
Precise definitions reduce errors and often matter more than any change to the prompt.
Controlling when functions are called
Developers can usually choose whether the model may decide freely, must call some function, must call a specific one or may not call any. Systems also support several calls in one response, which can run in parallel when they are independent. Limiting the set of functions offered in a given context improves accuracy and security.
Strict schemas and validation
Even capable models can produce missing fields, wrong types or invented arguments. Some interfaces offer a strict mode that uses constrained decoding to guarantee that the arguments match the schema. Even then, the application must validate meaning: a syntactically valid order number may not exist, and a date may be in the past. Typical checks include looking up identifiers, enforcing permissions for the current user and rejecting values outside safe ranges.
Error handling
When a function fails, the error should be returned to the model in clear words so it can correct itself, for example order number not found, please check the digits. Applications set limits on the number of retries and fall back to asking the user. Functions that change state should be idempotent or protected by confirmation, so a repeated call does not charge a card twice.
Use cases
- Data lookup: orders, accounts, inventory, weather or stock prices.
- Actions: creating tickets, sending messages, booking and updating records.
- Calculations: exact arithmetic, unit conversion and data analysis.
- Extraction: defining a function whose arguments match the fields wanted, so the model fills them from text, see knowledge extraction.
- Natural-language interfaces: turning requests into database queries or application programming interface calls.
- Agent loops: repeated calls to carry out multi-step tasks in an agent workflow.
Risks
- Incorrect calls: wrong function choice or arguments.
- Privilege misuse: functions with broad access can cause harm if misdirected.
- Injection: text in user input or in returned data may try to push the model into harmful calls, see prompt injection.
- Data exposure: arguments and results may reveal sensitive information.
- Cost and loops: repeated calls can run up charges.
- Hallucinated functions: the model may name functions that do not exist, so unknown names must be rejected.
Best practices
- Offer the smallest set of functions needed.
- Write detailed descriptions and parameter documentation.
- Validate every call against the schema and business rules.
- Check permissions in code, not in the prompt.
- Require confirmation for sensitive or irreversible actions.
- Log calls, arguments and results.
- Test with unusual and adversarial inputs.
A worked example
A bank builds a balance assistant. It defines one function, get_balance, with a parameter for the account type, either checking or savings. The customer writes: how much do I have in savings? The model returns a call to get_balance with account type savings. The application checks that the customer is signed in, retrieves the balance for that customer only and returns it. The model replies with the amount. If the customer asks to move money, the assistant calls a separate transfer function only after the customer confirms the amount and destination in a secure screen.
Testing and monitoring
Teams test function calling with a library of realistic requests and the calls expected for each, checking the chosen function, the arguments and the behavior after errors. They include cases where no function should be called, ambiguous requests that need a clarifying question and attempts to trick the model into unsafe calls. In production, dashboards track call rates, failures, latency and the share of calls rejected by validation, and alerts flag sudden changes after a model or schema update.
Related concepts
Function calling is one part of a wider set of practices for connecting models to software, including the model context protocol for sharing tools across applications and the design of an AI assistant that uses them in conversation.
FAQs
Is function calling the same as tool calling? They are closely related, and tool calling is the broader modern term.
Can the model run my code? No. It returns a request, and your application decides whether to run it.
Do I need JSON Schema? Most interfaces use it to describe parameters, and it helps the model produce valid arguments.
More in AI Agents and Autonomous Systems
Key facts
- OpenAI introduced function calling for its chat models on June 13, 2023, allowing developers to describe functions that a model could choose to call with JSON arguments.1
- JSON Schema is the standard way to describe a function's parameters so that a model can produce arguments that match it.2
- Several providers now offer a strict mode in which model outputs are constrained to match the supplied schema exactly.3
Related concepts
Quick checkWhat is function calling in AI?Show answer
It is a feature in which a model, given descriptions of available functions, returns the name of a function and structured arguments that the application can run to get data or take an action.
Sources
- OpenAI, Function calling and other API updates, https://openai.com/index/function-calling-and-other-api-updates/
- JSON Schema, Understanding JSON Schema, https://json-schema.org/understanding-json-schema/
- OpenAI, Introducing Structured Outputs in the API, https://openai.com/index/introducing-structured-outputs-in-the-api/
Editorially reviewed by Specialty Digest Editorial TeamLast reviewed November 17, 2026Researched and drafted with AI assistanceReport an issue
