Skip to content
Shahid Malla

LLM Function Calling Explained, With a Safe Example

The model proposes a function call, your code runs it. A working guide to LLM function calling: tool schemas, validating arguments, when to ask a person first, errors and retries, and how it relates to MCP.

· Published · 12 min read · 9 steps

Function calling, also called tool calling, lets a large language model ask your application to run a function you defined. The model never runs anything itself. It returns a tool name and a JSON object of arguments, your code validates them, runs the work and sends the result back, and the model writes its final answer from that result.

A function call is a structured request from a model, not an action. It names one of the tools you declared and fills in arguments meant to match the JSON schema you published for that tool. Everything after that, including whether the call runs at all, is your application's decision.

That last sentence is the whole security model, and the part most first builds get wrong.

Five-step LLM function call loop: declare tools with JSON schemas, read the proposed call, validate it, confirm anything with side effects, then return the result to the model
One pass of a function call. The model owns step two only. Source: shahidmalla.com.

What does the model do, and what does your code do?

The model chooses a tool and fills in arguments. Your code does everything else. Anthropic's How tool use works page puts it plainly: the model never runs anything on its own, it emits a structured request, and your code runs the operation.

OpenAI's function calling guide gives the round trip in five steps: send a request listing the tools, receive a tool call, run your code with its arguments, send a second request carrying the output, then receive the final answer or more calls. A response can hold several calls, so write the handler as a loop. On the Claude API that loop is keyed on the stop reason: while it is tool_use, run the tools and send the results back.

OpenAI, Anthropic and MCP describe the same three objects under different names:

ObjectOpenAI Responses APIClaude Messages APIMCP server
The tool you declaretype: function with name, description, parametersname, description, input_schemaname, description, inputSchema
The proposed callfunction_call with call_id, name, argumentstool_use with id, name, inputtools/call with name, arguments
Your resultfunction_call_output, same call_idtool_result with content, is_errora result with content, isError

Ollama declares tools in the nested shape OpenAI's Chat Completions API uses. One difference bites people porting code: the OpenAI Responses API returns arguments as a JSON-encoded string you must parse, while the Claude API and Ollama both hand back an object. Anthropic's Handle tool calls page adds a rule: results must come first in the user message and must immediately follow the assistant message that asked for them, or the request returns a 400.

How do you write the JSON schema for a tool?

Write the description for a new colleague, not for a parser. Anthropic calls it "by far the most important factor in tool performance" and asks for at least three or four sentences: what the tool does, when it should not be used, what each parameter means, and what it does not return. OpenAI's version is the intern test, meaning could a person use this function correctly given only what you gave the model?

The schema is ordinary JSON Schema, whose defaults are permissive in two ways that matter here. Leaving a property out is valid unless you list it in required, and extra properties are valid unless additionalProperties is false.

{
  "name": "get_invoice",
  "description": "Read one invoice belonging to the signed-in client, by numeric ID. Returns status, total, currency and due date. It cannot pay, refund or cancel invoices, and cannot read another client's invoices.",
  "input_schema": {
    "type": "object",
    "properties": {
      "invoice_id": {
        "type": "integer",
        "description": "The invoice ID shown in the client area."
      }
    },
    "required": ["invoice_id"],
    "additionalProperties": false
  }
}

Four rules from the vendor docs that changed how I write these:

  • Use enums and object shape to make invalid states impossible. OpenAI's example is a light toggle with separate on and off booleans, which permits a call where both are true. One state parameter with an enum cannot be.
  • Do not ask for arguments you already know. If your code holds the order ID from the previous screen, pass it yourself. Every argument the model fills is one it can fill wrongly.
  • Consolidate and namespace. Anthropic prefers one tool with an action parameter over three near-identical ones, and prefixed names such as billing_list_invoices.
  • Keep the starting set small. OpenAI suggests fewer than 20 functions at the start of a turn, because accuracy falls as the list grows.

Both platforms have a strict mode that makes the shape a guarantee rather than a tendency. OpenAI's requires additionalProperties false and every field in required, with optional fields written as a type union including null. Anthropic offers the same flag. Turn it on, then keep validating anyway.

How do you validate the arguments before anything runs?

Validate in layers, because strict mode only proves the shape is right. An invoice ID can be a valid integer and still belong to a different customer. Schema conformance is not existence, and it is certainly not permission.

LayerWhat it catchesWhat it still misses
Strict schema at the APIMissing fields, wrong types, values outside an enum, invented extra fieldsWhether the record exists or belongs to this user
Your own validator, server sideRanges, formats, unknown IDs, rules the schema cannot expressWhether this account may act on that record
Authorization in your API or databaseA real record the signed-in user may not touchA permitted action the user never asked for
A confirmation stepA permitted action the user did not wantNothing, once the user approves it

OWASP's LLM06:2025 Excessive Agency entry names the rule for the third row: put authorization in the downstream system rather than relying on the model to decide whether an action is allowed. It also asks you to run each tool in the signed-in user's own context, with the minimum scope. A read-only tool that connects to the database as a user holding update and delete rights is the same finding.

When should a call wait for a person?

Whenever it changes something a customer would notice. The Model Context Protocol tools specification, revision 2026-07-28, states that there should always be a human in the loop with the ability to deny tool invocations, and asks applications to show which tools are exposed, to indicate when one is invoked, and to present confirmation prompts. OWASP says the same from the other side: require approval for high-impact actions, in the code that performs them, never in the prompt.

Decision flow for a proposed tool call: read-only calls run and are logged, money or deletion waits for approval, and calls traced to fetched web content are refused
The four questions every proposed call goes through in my builds. Source: shahidmalla.com, following the MCP human-in-the-loop requirement and OWASP LLM06.

In words: if the tool only reads data, run it and log the call. If it moves money or deletes records, show the proposed call and wait for approval. If the request traces back to a web page or an email the model read rather than to a human, refuse and log it. And if this user's account does not permit the action, the answer is a refusal, not a privileged override; if it does permit the action, run it once behind an idempotency key.

Phrase the confirmation in the user's language. "Apply a credit of 40 USD to invoice 10421?" is a question a client can answer. A raw JSON blob is not.

How do you handle tool errors and retries?

Return the error to the model as a result instead of letting an exception kill the request. On the Claude API that is a result block with is_error set to true. On the OpenAI side the output is a string whose format is yours, and functions with no return value are documented as returning something like "success".

{
  "type": "tool_result",
  "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
  "content": "Invoice 10421 belongs to a different client. Ask the user to confirm the number.",
  "is_error": true
}

Anthropic's guidance on wording is the useful part: write instructive errors, not generic ones. "Rate limit exceeded. Retry after 60 seconds." gives the model something to act on; "failed" makes it guess. The same page warns the model may call the tool again, so cap retries and loop iterations. Three failure modes to plan for:

  • A missing argument gets invented. Anthropic documents that when a required parameter is absent, Claude Opus is much more likely to ask for it, while Sonnet might infer a reasonable value instead. Treat any argument the user did not state as a candidate for confirmation.
  • The same write runs twice. The loop can repeat a call after a timeout. Give every write tool an idempotency key and make the second identical call a no-op.
  • Repeated invalid calls. Anthropic's advice is to make the description more detailed, because an invalid call usually means the model lacked the information to get it right.

What does a safe example look like end to end?

Take a billing assistant for a hosting company. It gets two tools, deliberately not symmetrical. get_invoice is the schema above: read-only, one integer argument, scoped by the session so it can only reach the signed-in client's invoices. It runs with no confirmation, and every call is logged.

apply_account_credit takes an invoice ID, an amount and a currency enum. Strict mode guarantees those arrive in the right shape. My code then checks that the invoice belongs to this client, that the amount is under a ceiling held in configuration and not in the prompt, and that the staff account holds the credit permission in the billing system. Only then does the interface show a plain-language confirmation with an Approve button, and the call carries an idempotency key.

The model sees none of that. It sees two descriptions and, afterwards, a result or an instructive error. The policy lives in code a prompt cannot talk its way past. For the parts around this loop, see how to build an AI agent, and AI chatbot versus AI agent for whether you need one.

How does function calling relate to MCP?

MCP is the same mechanism moved out of your application. It defines a server that advertises tools over tools/list, each with a name, a description and an input schema, and runs them through tools/call. That is the contract in the table above with different field names.

The difference is who owns the tool list. Plain function calling defines tools inside one application for one model. An MCP server defines them once and any client can connect, which is what makes the extra moving part worth it when several assistants need the same tools. My guide on what MCP is explains the host, client and server split, and WHMCS MCP servers applies the same rules to one billing system.

One line in the specification is easy to miss: clients must treat tool annotations as untrusted unless they come from a trusted server. A tool description you did not write is input, not instruction, and so is whatever a tool returns.

What does function calling cost, and where does it break?

It costs tokens and round trips. OpenAI documents that function definitions are injected into the system message and billed as input tokens, so every name, description and schema is on the bill for every request. Anthropic's tool use overview adds a tool-use system prompt on top: read on 21 November 2026, its table listed 286 tokens for Claude Opus 5.5, Claude Sonnet 5.5 and Claude Haiku 5.5 at a tool choice of auto or none, and 406 tokens for Claude Haiku 5.5 when a tool is forced.

  • Latency. Every tool call is at least one extra round trip. For a trivial answer the overhead can cost more than the work.
  • Indirect prompt injection. Anthropic warns that tool results often carry content you do not control, such as webpages, inbound email and uploads, and that it should stay inside result blocks rather than system prompts. Instructions arriving through a tool result are data.
  • Forced calls are not universal. Anthropic lists models and settings where forcing a named tool returns an error.
  • It is not a database. Answering from a pile of documents is retrieval, which my guide on RAG chatbots covers.

What should you check before a tool goes live?

  • The description says what the tool does, when not to use it, and what it does not return.
  • Every field the code needs is required, extra properties are rejected, and strict mode is on.
  • Server-side validation runs anyway, with the signed-in user's rights and not a service account.
  • Read and write tools are separate, and only writes trigger a confirmation.
  • Every write tool has an idempotency key and a ceiling held in configuration.
  • Errors are instructive, retries and loop iterations are capped, and every call is logged.

What else do people ask about function calling in LLMs?

Is function calling the same as tool calling?

Yes. Anthropic's docs open by saying tool use is also called function calling, and OpenAI's guide says the reverse. OpenAI draws one narrow distinction inside its own API: a function is the kind of tool defined by a JSON schema, as opposed to custom tools that take free-form text.

Which LLM models support function calling?

OpenAI and Anthropic both document it across their current models, and open-weight models can do it when served through a runner that handles tool calls. The detail is per model, not per vendor: OpenAI documents parallel function calls on supported models from GPT-5 onward, and Anthropic lists models that reject a forced tool choice with a 400 error. Check the vendor's table before you rely on either.

Can you use function calling with Ollama and a local model?

Yes. The Ollama tool calling page passes a tools array to its chat endpoint in the shape OpenAI's Chat Completions API uses, and reads the proposal back from message.tool_calls. Its Python SDK will also build the schema from a plain function's signature and docstring.

Do you need Python or a framework to do function calling?

Neither. It is an HTTP request with a tools array and a loop around the response, and the vendor docs show the same example in Python, TypeScript, Go, Java, Ruby and cURL. A framework helps once you run many tools; for two or three it mostly hides the loop you need to understand.

I build AI agents and chatbots for hosting and billing systems, so if you want this reviewed or built, it falls under AI agent development or MCP server development, with a free scoping call of about 30 minutes and a written quote within one business day. All sales are final, so ask whatever you need before you buy.

Last reviewed 21 November 2026. Disclosure: I sell AI agent, chatbot and MCP server development, and I am founder and CEO of MagizAI, an AI chatbot product, so I am not neutral. I am CTO at ElySpace, a freelance developer for Marmaids LLC, and founder of WHMCSPilot and MagizAI. Field names, schema rules, token figures and quoted guidance come from the OpenAI function calling guide, the Anthropic tool use documentation, the Model Context Protocol tools specification, the Ollama tool calling page and OWASP LLM06:2025 as I read them on 21 November 2026.

Share this article

Shahid Malla

Written by

Shahid Malla

Freelance WHMCS developer and web designer with 13+ years of client work. CTO at ElySpace, freelance developer for Marmaids LLC, and founder of WHMCSPilot and MagizAI.

Got a project like this?

Tell me what you need and I will send a written quote within one business day.