What function calling is
Function calling (also called the tools API) is a feature that lets a language model interact with your code and external systems in a reliable, structured way. You describe the functions available — their names, what they do, and their parameters — and the model, when appropriate, responds not with prose but with a structured JSON request to call one of them, complete with the arguments it chose. This turns an LLM from a text generator into something that can drive real actions like searching a database, fetching live data, or sending a message.
The request-execute-respond loop
It is important to understand that the model never runs your function itself. The flow is a loop you orchestrate:
- You send the user’s message plus a list of function definitions.
- The model decides whether a function is needed. If so, it returns the function name and a JSON object of arguments.
- Your code executes that function and gets a real result.
- You send the result back to the model.
- The model uses the result to produce its final answer.
This separation matters for safety and control: the model only proposes an action, and your application decides whether and how to carry it out. It is also what grounds the answer in real data rather than guesswork.
Defining a function
You describe each function with a JSON schema — typically a name, a short
description of when to use it, and a list of typed parameters. For example, a
weather tool might declare a single required location string and an optional
unit enum of celsius or fahrenheit. The description text genuinely matters:
the model reads it to decide when the function is appropriate, so vague
descriptions lead to the model calling tools at the wrong times.
Function calling vs structured output
Function calling is built on the same underlying capability as structured output: forcing the model to emit valid, schema-conforming JSON. The difference is purpose. Structured output is about getting clean machine-readable data back from any request, while function calling specifically uses that to request a tool invocation and then continue the conversation once the tool has run. In practice, modern APIs let the model return multiple tool calls at once and even run them in parallel.
Patterns for reliable tool use
A few habits make function-calling agents far more robust:
- Write precise descriptions so the model knows exactly when each tool applies.
- Validate the arguments the model returns before executing — never trust them blindly, especially for anything destructive.
- Handle errors as messages — when a function fails, return the error back to the model so it can retry or apologise gracefully.
- Keep tools focused — many small, well-named functions beat one giant do-everything tool.
Done well, function calling is the backbone of dependable AI agents: it gives the model a structured, auditable way to act in the real world while your code stays firmly in command of what actually executes.