What is tool calling in LLMs?
Definition
Tool calling, also called function calling, is an LLM API feature in which the model, instead of answering in text, returns a structured request to run a tool you defined: the tool's name and JSON arguments. Your application runs the tool, sends the result back, and the model uses it to continue or to answer.
How does tool calling work?
Anthropic's docs call it a contract: "You specify what operations are available and what shape their inputs and outputs take; Claude determines when and how to call them." The same page adds the key limit: "The model never runs anything on its own" (How tool use works).
OpenAI's function calling guide describes the flow in five steps:
- Your app sends a request with the tools the model could call.
- The model returns a tool call: a tool name and JSON arguments.
- Your app runs the code with those arguments.
- Your app sends a second request with the tool output.
- The model returns a final answer, or more tool calls.
Steps 2 to 4 repeat for as long as the model keeps asking. Anthropic's docs describe the client side as a while loop that runs tools while stop_reason is "tool_use" and exits on any other stop reason.
Is tool calling the same as function calling?
Yes. The names are used for the same mechanism. Anthropic's tool use overview opens with "Tool use (also called function calling)", and OpenAI's guide opens with "Function calling (also known as tool calling)". OpenAI draws one distinction: a function is a specific kind of tool, defined by a JSON schema, next to custom tools that take free text and built-in tools such as web search.
The wire formats differ more than the names. Field names from each provider's docs:
| Step | Claude Messages API | OpenAI Responses API | Gemini Interactions API |
|---|---|---|---|
| Define a tool | tools: name, description, input_schema | tools: type: "function", name, parameters | tools: type: "function", name, parameters |
| Model asks for a call | tool_use block with id, name, input | function_call item with call_id, name, arguments as a JSON string | function_call step with id, name and arguments |
| App returns the result | tool_result block with tool_use_id, content, optional is_error | function_call_output with call_id and output | function_result with name, call_id, result |
The Claude docs point out one design difference: there is no separate tool or function role. Tool calls live in assistant messages and results in user messages.
Tool calling vs MCP vs ReAct
These three terms sit at different layers, and an agent often uses all of them.
| Tool calling | MCP | ReAct | |
|---|---|---|---|
| What it is | An API feature: the model returns a structured call to a tool you defined | A protocol that lets servers expose tools that language models can invoke | A prompting method that interleaves reasoning traces with actions (Yao et al., 2022) |
| Where tools are defined | In each API request | On the MCP server, which lists them in answer to tools/list | In your prompt and parsing code |
| How a call is passed | Structured blocks in the API response | A tools/call request from the MCP client to the server | Text lines your code parses |
They work together. The MCP specification describes its tools as model-controlled: the host offers the server's tools to the model, and a tool call becomes a tools/call request to the server (see our MCP server entry). Claude, OpenAI and Gemini can also connect to remote MCP servers from their own APIs. A ReAct agent runs the same reason, act, observe cycle with actions parsed from text; native tool calling passes the same requests as structured data.
Example: a tool loop with parallel calls and an error result
This loop uses the Claude message format with a stub model, so it runs without an API key. In the first turn the model asks for two tools at once, and one call is missing an argument. We ran it with Node.js 22.9 as node loop.mjs.
// Tool calling loop in the Claude Messages API format, run against a stub model.
// The app owns three jobs: run each requested tool, answer every tool_use id, and cap the loop.
const tools = {
get_order: {
required: ["order_id"],
run: ({ order_id }) => ({ order_id, status: "shipped", carrier: "DHL" }),
},
get_refund_policy: {
required: ["country"],
run: ({ country }) => ({ country, days: 30 }),
},
};
// Stub model: replays scripted turns so the loop runs offline. Swap in client.messages.create().
const turns = [
{ stop_reason: "tool_use", content: [ // two independent calls in one turn (parallel)
{ type: "tool_use", id: "toolu_01", name: "get_order", input: { order_id: "A-1182" } },
{ type: "tool_use", id: "toolu_02", name: "get_refund_policy", input: {} }, // missing country
]},
{ stop_reason: "tool_use", content: [ // the model reads the error and retries
{ type: "tool_use", id: "toolu_03", name: "get_refund_policy", input: { country: "PL" } },
]},
{ stop_reason: "end_turn", content: [
{ type: "text", text: "Order A-1182 has shipped with DHL. You can return it within 30 days." },
]},
];
let t = 0;
const model = async (messages) => turns[t++];
function runTool(block) {
const tool = tools[block.name];
if (!tool) return { content: `Unknown tool ${block.name}`, is_error: true };
const missing = tool.required.filter((k) => !(k in block.input));
if (missing.length) return { content: `Missing ${missing.join(", ")}. Ask the user or pass it.`, is_error: true };
return { content: JSON.stringify(tool.run(block.input)) };
}
const MAX_TURNS = 5; // a model can keep retrying a failing tool: always cap the loop
const messages = [{ role: "user", content: "Where is order A-1182, and can I still return it?" }];
for (let turn = 1; turn <= MAX_TURNS; turn++) {
const response = await model(messages);
messages.push({ role: "assistant", content: response.content });
if (response.stop_reason !== "tool_use") {
console.log(`turn ${turn}: ${response.stop_reason} -> ${response.content[0].text}`);
break;
}
// One tool_result per tool_use id, results first in the next user message, even for failures.
const results = response.content
.filter((b) => b.type === "tool_use")
.map((b) => ({ type: "tool_result", tool_use_id: b.id, ...runTool(b) }));
for (const r of results) console.log(`turn ${turn}: ${r.tool_use_id} ${r.is_error ? "ERROR " : "ok "} ${r.content}`);
messages.push({ role: "user", content: results });
}
console.log(`messages in history: ${messages.length}`);
Output:
turn 1: toolu_01 ok {"order_id":"A-1182","status":"shipped","carrier":"DHL"}
turn 1: toolu_02 ERROR Missing country. Ask the user or pass it.
turn 2: toolu_03 ok {"country":"PL","days":30}
turn 3: end_turn -> Order A-1182 has shipped with DHL. You can return it within 30 days.
messages in history: 6
The failed call still gets a tool_result, with is_error: true. Anthropic's handle tool calls page requires one result for each tool_use block "even if the tool failed", with results first in the next user message. It also recommends instructive errors over a bare "failed", which is why the message says what is missing. After an error, the docs say Claude might call the tool again, "so limit how many retries your loop allows". Here the stub retries once and MAX_TURNS caps the rest.
Which settings control tool calling?
| Setting | Claude | OpenAI | Gemini |
|---|---|---|---|
| Let the model decide | tool_choice auto (default) | tool_choice: "auto" (default) | tool_choice auto (default) |
| Force a call | any or a named tool (Claude Opus 5.5, Sonnet 5.5 and several other current models return a 400 error) | "required" or a named function | any |
| Block calls | none | "none" | none |
| Enforce the schema | strict: true on the tool | strict: true | validated |
| One call at a time | disable_parallel_tool_use: true inside tool_choice | parallel_tool_calls: false | No switch in the function calling guide |
Strict mode is worth turning on. OpenAI says it makes calls "reliably adhere to the function schema, instead of being best effort" and recommends always enabling it. Its requirements: additionalProperties: false on every object and every field marked required, with optional fields typed as nullable.
How many tools should a model get?
Fewer than you think. OpenAI's function calling guide suggests aiming for fewer than 20 functions available at the start of a turn, "though this is just a soft suggestion". Google's Gemini function calling guide advises keeping the active set to 10 to 20 tools at most.
Every definition also costs input on every request. OpenAI notes that function definitions are injected into the system message, count against the context limit and are billed as input tokens. Anthropic adds a tool use system prompt on top of your definitions. For large tool sets, both OpenAI and Anthropic offer tool search, which loads rarely used tools only when needed.
What should you check before shipping tool calls?
- Validate arguments in your code. Strict mode checks shape, not permission. A valid refund amount can still be one this user may not issue.
- Treat tool results as untrusted. Anthropic warns that results from web pages, email or uploads can carry indirect prompt injection, and advises keeping that content inside
tool_resultblocks. - Watch for guessed parameters. The Claude docs say that when a required value is missing, Claude Sonnet "might also infer a reasonable value" instead of asking.
- Cap retries and turns, and log every call with its arguments and result.
- Test tool selection, not only the final answer: the right tool, valid arguments, a sensible order, and recovery from errors.
SDCLabs builds function calling and MCP servers that give agents safe, scoped access to client APIs, databases and third-party systems, as part of our AI agents work. In agent testing we check that the agent calls the right tools with valid arguments, in a sensible order, and handles tool errors without looping.
Sources
- Tool use with Claude, Anthropic (Claude API docs), 2026-11
- How tool use works, Anthropic (Claude API docs), 2026-11
- Handle tool calls, Anthropic (Claude API docs), 2026-11
- Parallel tool use, Anthropic (Claude API docs), 2026-11
- Define tools, Anthropic (Claude API docs), 2026-11
- Function calling, OpenAI API docs, 2026-11
- Function calling with the Gemini API, Google AI Studio (Gemini API docs), 2026-11
- Tools, Model Context Protocol specification (version 2026-07-28), 2026-07-28
- ReAct: Synergizing Reasoning and Acting in Language Models, arXiv (Yao et al.), 2210.03629 v3, 2023-03-10
Frequently asked questions
What is parallel tool calling?
The model asks for several independent tools in one response, for example an order lookup and a policy lookup, and your app runs them before replying with all the results. On the Claude API it is on by default; you turn it off with disable_parallel_tool_use in tool_choice.
What is the difference between tool calling and structured output?
Both return JSON shaped by a schema you provide. A tool call is a request for your code to act, after which the conversation continues with the result; structured output is the final answer itself, shaped for your code to read.
When should you not use tool calling?
Anthropic's docs list three cases: the model can answer from training alone, the exchange is one-shot Q&A with nothing to run, or the extra round trip would take longer than the task itself.
What are server tools?
Tools the model provider runs for you, such as web search or code execution on the Claude API. You enable them in the request and read the results; you never build the tool result yourself.
Services
Read next
Related terms
Mikhail BodnarchukFounder of Codeception and CodeceptJSBoth are open-source testing frameworks: Codeception for PHP, CodeceptJS for JavaScript.LinkedInGitHub