Function Calling Explained: How LLMs Use Tools Reliably

Function calling lets a language model request your code, not run it. Learn the five-step loop, strict mode, structured outputs, and production pitfalls.

By Galactic News Editorial · Published 2026-11-16T22:44:14+00:00 · Updated 2026-11-16T22:44:14+00:00

Function calling explained: how LLMs use tools reliably through structured requests

Function Calling Explained: How LLMs Use Tools Reliably

Function calling lets a language model use your code. You describe a function with a JSON schema. The model decides when to call it and fills in the arguments. Your code runs the function. The result goes back to the model. That loop turns a chatbot into a system that can check data and take action.

The key fact surprises most developers. The model never runs your function. It returns a structured request to call it. Your application executes the function and returns the result. The model then writes its final answer using that real data.

Function calling explained: the five-step loop

OpenAI describes the tool-calling flow in five steps. First, you send a request to the model with the tools it may call. Second, the model returns a tool call. Third, your code executes the function with the model's arguments. Fourth, you send the result back to the model. Fifth, the model returns a final response, or asks for more tool calls.

A weather example makes it concrete. The user asks, "What is the weather in Paris?" Your request includes a get_weather tool that takes a location. The model returns a tool call with location set to Paris. Your code calls the weather API and gets 25 degrees Celsius. You send that result back. The model answers, "The weather in Paris today is 25C."

The loop repeats as needed. A complex request may need several rounds. The model can call one tool, read the result, then call another. Your code controls the loop. Set a maximum number of rounds, usually five to ten, so a confused model cannot loop forever.

How to define a function the model can use

A function definition has four parts: a name, a description, a parameter schema, and a strict flag. The description matters most. The model reads it to decide when and how to call the function. Write it like documentation for a new team member. State the purpose, the expected input, and what the output means.

Here is a minimal definition from the OpenAI docs:

{
  "type": "function",
  "name": "get_weather",
  "description": "Retrieves current weather for a given location.",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {"type": "string", "description": "City and country, e.g. Bogota, Colombia"},
      "units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
    },
    "required": ["location", "units"],
    "additionalProperties": false
  },
  "strict": true
}

Three design rules keep definitions reliable. Use enums to prevent invalid states. A units field with two allowed values cannot receive a third. Do not make the model fill in arguments you already know. If the order ID is in your session, leave it out of the schema and pass it in your code. Combine functions that always run in sequence. If you always call mark_location after query_location, put the marking logic inside the query function.

Keep the tool list small. OpenAI suggests fewer than 20 functions available at the start of a turn. More tools mean more chances the model picks the wrong one. For large tool sets, newer models support tool search. The model loads rarely used tools only when it needs them.

Strict mode and structured outputs

Regular function calling guarantees valid JSON. It does not guarantee the JSON matches your schema exactly. Strict mode changes that. With strict: true, the model is constrained to produce arguments that match the schema. That removes the most common production failure: a missing required field or a wrong type.

Strict mode has rules of its own. Every object in the schema needs additionalProperties: false. All fields in the schema must be listed as required. Some JSON Schema keywords are not supported. Check the Structured Outputs guide before relying on an unusual keyword.

Structured outputs solve a different problem from function calling. Function calling is for triggering an action or fetching data. Structured outputs force the model's final answer to match a schema. Use function calling when the model needs to request an action. Use structured outputs when you need reliably shaped data extracted from a response, with no function to run.

ApproachBest forWhere the result lands
Function callingTriggering actions, fetching live datatool_calls in the response
Structured outputsExtracting shaped data from a replymessage.content as JSON
Plain JSON promptQuick prototypes onlyUnreliable; needs manual parsing

Tool use in Claude: the same pattern, different names

Anthropic calls the same capability tool use. A tool definition has a name, a description, and an input_schema written as JSON Schema. Claude returns a tool_use content block with a name, an input object, and a unique ID. Your code executes the tool and returns the result in a tool_result block with the same ID.

The loop is identical. Call the Messages API with tools and the conversation. If the stop reason is tool_use, execute every requested tool. Append the results and repeat. Claude can request several tools in a single response, so run independent calls in parallel. You can also force a specific tool. Forcing one tool is a clean way to get structured JSON output for extraction tasks.

Both providers agree on the hard rules. Validate every tool input before execution. Never trust user-provided data inside a tool call without sanitization. Cap the loop. Log each tool call and its result. These rules are what separate a demo from a production system.

Common failures and how to prevent them

Four failures recur in production. The first is a hallucinated argument. The model invents a value that looks plausible. Prevent it with enums, tight descriptions, and server-side validation of every argument.

The second is a wrong tool choice. The model picks a similar tool with the wrong side effects. Prevent it with distinct names and descriptions that state when not to use the tool. Fewer tools help more than cleverer prompts.

The third is a missing result. The tool returns an error or empty data, and the model guesses instead of reporting the gap. Prevent it by returning clear error messages in the tool result. Tell the model what failed so it can retry or tell the user.

The fourth is an infinite loop. The model keeps calling tools without reaching an answer. Prevent it with a hard step cap and a fallback message. After the cap, return a summary of what was tried and ask the user for guidance.

Bottom line

Function calling is a simple contract. You describe tools with JSON Schema. The model requests calls. Your code executes them. Strict mode makes the arguments reliable. Structured outputs handle shaped answers. Validate inputs, cap the loop, and log everything. That is the whole pattern, and it is enough to build agents that act on real data.