Dev tools
Function Calling Explained: How LLMs Use Tools
Function calling lets an LLM ask your code to run a tool, like a weather lookup or a database query. See how it works step by step, with a simple example.
Function calling is the feature that lets a large language model use tools. Instead of only writing text, the model can reply with a structured request like "run get_weather with city = Istanbul." Your code runs that function, sends the result back, and the model uses it to answer. It is how chatbots check live data, how assistants book meetings, and how AI agents take actions in the real world.
This guide explains function calling in plain language, walks through a full function calling example, and covers the rules that make it reliable in real apps.
What Is Function Calling?
On its own, a language model can only produce text. It can't look up today's weather, read your database or send an email. Function calling closes that gap.
You describe a set of functions to the model: their names, what they do, and what inputs they take. When the model decides one of them would help, it doesn't answer in prose. It returns the function name and the arguments in a structured format. Your application runs the function and gives the result back to the model.
The most important point: the model never runs anything itself. It only asks. Your code decides whether to run the function, runs it, and controls what comes back. That makes function calling safe to build on, as long as your code checks every request.
Function calling or tool calling?
They are the same thing. OpenAI popularized the name "function calling" when it launched the feature in 2023. Anthropic calls it "tool use," and many developers now say tool calling because a tool can be more than a single function, such as a search engine or a code runner. The idea is identical across providers.
How Function Calling Works, Step by Step
Every function call follows the same loop.
- You describe the tools. Along with the user's message, your app sends a list of tools. Each one has a name, a short description, and a JSON Schema for its inputs.
- The model decides. It reads the request and the tool list. If a tool would help, it replies with a tool call instead of a final answer.
- Your code runs the tool. You check the arguments, call the real function or API, and capture the result.
- You send the result back. The result goes into the conversation as a tool result message.
- The model answers. With the real data in front of it, the model writes its reply, or asks for another tool if it needs more.
The loop can repeat. An agent that books a meeting might first call a calendar tool to find free slots, then a booking tool to reserve one, then an email tool to send the invite. Our guide on how to build an AI agent shows how this loop becomes the core of an agent.
A Function Calling Example
Let's say you want an assistant that can answer "Do I need an umbrella in Istanbul today?"
Step 1: Describe the tool
You give the model a tool definition. The inputs are described with JSON Schema, a standard way to say which fields exist and what type each one is.
{
"name": "get_weather",
"description": "Get the current weather for a city. Use this when the user asks about weather, temperature or rain.",
"input_schema": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name, for example Istanbul" },
"unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
},
"required": ["city"]
}
}The exact wrapper differs a little between providers. Anthropic calls the schema field input_schema, while OpenAI and Google call it parameters. The content inside is the same JSON Schema.
Step 2: The model asks for the tool
The model sees the question and the tool, and replies with a tool call instead of text:
{ "name": "get_weather", "arguments": { "city": "Istanbul", "unit": "celsius" } }Step 3: Your code runs it
Your application matches the name to a real function, checks the arguments and runs it:
def get_weather(city: str, unit: str = "celsius") -> dict:
# Call a real weather API here; this is sample data.
return {"city": city, "temp": 14, "unit": unit, "rain_chance": 0.7}
TOOLS = {"get_weather": get_weather}
def run_tool_call(name: str, arguments: dict) -> dict:
fn = TOOLS.get(name)
if fn is None:
return {"error": f"unknown tool: {name}"}
try:
return fn(**arguments)
except TypeError as e: # wrong or missing arguments
return {"error": str(e)}Step 4: The model answers
You send the result back as a tool result. The model now has real data and replies in plain language: "It's about 14°C in Istanbul with a high chance of rain, so yes, take an umbrella."
Notice that errors go back to the model too. If a tool fails, returning a clear error message lets the model fix its request or explain the problem, instead of guessing.
Writing Good Tool Definitions
Most function calling problems come from vague tool definitions, not from the model. A few rules help a lot.
- Write descriptions for the model, not for humans. Say what the tool does and when to use it. "Use this when the user asks about weather" is more useful than "Weather endpoint."
- Use clear names.
search_ordersbeatsquery2. - Keep inputs simple. Fewer, well-named fields lead to fewer mistakes. Use
enumwhen only a few values are allowed. - Mark required fields. The model then knows what it must ask the user for.
- Don't offer too many tools at once. A long list of similar tools makes the model pick the wrong one more often. Group or filter tools by task.
Strict mode and structured output
By default, a model usually follows your JSON Schema but can occasionally return a missing field or a wrong type. Some APIs offer a strict mode that forces the arguments to match the schema exactly. Turn it on when it is available, and still validate the arguments in your own code.
Parallel Calls and Tool Choice
Modern models can request several tools in one turn. If a user asks "Compare the weather in Istanbul, Berlin and Dubai," the model can ask for three get_weather calls at once. Your code can run them in parallel and return all three results together, which is faster than one at a time.
Most APIs also let you control tool choice:
- Auto: the model decides whether to call a tool. This is the usual setting.
- Required: the model must call some tool, useful when you always want structured output.
- A specific tool: force one named tool, for example to always extract data in the same shape.
- None: the model must answer in text only.
Function Calling vs MCP
As apps grew to dozens of tools, a new question appeared: function calling vs MCP. They aren't competitors. They work at different layers.
Function calling is the model's ability to ask for a tool. It is built into the model and its API.
MCP (the Model Context Protocol) is a shared standard for packaging and connecting tools. Instead of writing tool definitions and glue code for each app, you build an MCP server once, and any app that supports MCP can use its tools. Under the hood, the app still turns those MCP tools into function calls for the model. Our guide to the Model Context Protocol explains how the pieces fit.
A simple rule of thumb: for a handful of tools inside one app, plain function calling is enough. When you want to reuse tools across apps, teams or AI clients, MCP saves a lot of repeated work.
Safety Rules for Function Calling
Because tool calls lead to real actions, treat every request from the model like input from an untrusted user.
- Validate arguments in your code before running anything.
- Check permissions against the real user, not against what the model claims.
- Ask for confirmation before actions that send money, send messages or delete data.
- Limit what tools can reach. A read-only tool can't be tricked into deleting records.
- Remember prompt injection. Text inside tool results, such as a web page or an email, can contain instructions meant to trick the model into calling other tools.
FAQ
What is function calling in LLMs?
Function calling lets a language model request that your application run a specific function with specific arguments, such as looking up the weather or querying a database. Your code runs the function and returns the result, and the model uses it to answer.
Does the LLM run the function itself?
No. The model only produces a structured request. Your application decides whether to run it, runs it, and sends the result back. This keeps you in control of every action.
What is the difference between function calling and tool calling?
There is no real difference. Function calling is the older name popularized by OpenAI. Tool calling or tool use is the broader term used by Anthropic and many frameworks. Both describe the same mechanism.
Is function calling the same as MCP?
No. Function calling is the model's ability to request a tool. MCP is a standard for packaging tools so they can be shared across many apps and AI clients. MCP tools still reach the model through function calling.
Which models support function calling?
Most current models from major providers support it, including models from OpenAI, Anthropic and Google, and many open models too. Smaller local models can be less reliable at choosing tools and filling arguments, so test them on your own tasks.