Function Calling in Practice: Getting the Model to Take Action

A model that can only talk is not enough. Agents that actually get work done rely on function calling: the AI outputs a structured instruction, and your code goes out to query a database, send a message, or call an API. Get this right and the model upgrades from a chatbot into an execution engine.

What actually happens during a function call

A complete call runs in four steps. First you tell the model which tools are available, described as a JSON Schema that includes each function’s name, its purpose, and the type of every parameter. The model reads that, and when it needs help it returns a JSON block naming the function to call and the arguments. Your backend executes that function and gets a real result. Finally you feed the result back to the model, which decides whether to make another call or answer directly. Throughout the whole process the model never touches external systems — it only makes decisions, which keeps the security boundary clean. The core value of this mechanism is control. The model produces instructions inside a sandbox; the hands that actually do things belong to code you wrote and can test. Compared with letting the model directly emit commands to run, function calling has a much smaller error surface and is far easier to validate and log. It is the standard interface for wiring a large model into an existing system.

The tool descriptions decide success or failure

The model fills in parameters entirely from the descriptions you give it. A description needs three things: what the function does, what each parameter means, and when it should be used. A common mistake is naming functions vaguely — something like process_data, and the model has no idea when to call it. Switch to verb-plus-object names like fetch_order_by_id and accuracy improves immediately. It also helps to include one or two example values for the parameters; the model fills them in far more reliably than guessing from thin air. Another trap is too many tools. Hand the model thirty or forty functions at once and it gets decision fatigue, with call accuracy dropping noticeably. A practical rule is to expose only the 8 to 12 most relevant tools per task and load the rest on demand. You wouldn’t dump the whole toolbox on the table either — you pass over the few wrenches you need.

Feed the result back and keep looping

Many newcomers take the first tool result and show it straight to the user, skipping the step of feeding it back into the model. The right move is to send the result back as a fresh round of messages so the model can judge: if it has enough information it summarizes and answers; if not, it makes another call. Multi-step tasks work like booking a flight — first check flights, then enter passengers, then pay, and each step depends on the real return of the previous one. When feeding back, include a role marker like “this is the return from the previous step” so the model doesn’t mistake tool output for a new user question. On long chains you can also attach a running progress summary each round, letting the model know how far along it is and cutting out wasted repeat calls to the same function.

Safety rails are not optional

Function calling hands real capability to the model, and that brings risk. Give every tool a permission whitelist — for example, an email tool may only send to internal domains. Add timeouts to external requests so a stuck API can’t drag down the whole chain. Add a second confirmation for operations that change data. An agent with no guardrails going live is like handing the keys to your company system to an intern who makes mistakes. One more layer worth adding is a call budget: cap how many tool calls a single session may make and how much it may spend in total. Once the threshold is crossed, force-stop and hand off to a human. That way, even if the model loops forever or gets tricked, the damage stays inside the budget. Safety is not a single switch; it is four layers stacked — whitelist, timeout, confirmation, and budget.

Debugging starts with logs

Nine times out of ten, a function-calling problem can be found in the logs. Record, for every call, what function the model requested, what parameters it filled in, what the code returned, and how the model ultimately decided. When you review afterwards, you can see at a glance whether the description was unclear or the code errored. An agent without logs leaves you guessing blindly when it fails. Go one step further and collect failing cases into a regression test set: each entry records the input and the expected call, and every time you change a tool description you run it to confirm accuracy hasn’t dropped. Once a team gets big enough, this automated validation beats staring at screens, and it is the baseline guarantee that lets function calling ship reliably.

Balancing performance and cost

Tool calls are not free. Each one means a model inference plus a backend request, and the longer the chain the higher the cost. When independent pieces of information can be fetched in parallel, don’t serialize them — querying weather and exchange rates at the same time finishes twice as fast as one after the other. Another saving is caching the tool schemas and unchanged intermediate results within a session, so you don’t re-serialize dozens of function definitions every round. Keeping the exposed tools for a single task around a dozen protects both accuracy and cost. Treat call count and spend as core metrics to watch, right alongside accuracy.

Key TakeawaysDeclare ToolsDescribe inputs with JSON SchemaModel DecidesWhether to call, and with what argsExecute & ReturnCode runs, result fed backLoopModel plans again from the result

Figure: the core loop of function calling

Stage Who does it Key note
Tool declaration Developer Schema describes parameters and purpose
Call decision Model Outputs function name and argument JSON
Actual execution Your code Add timeouts, permissions, whitelist
Feed results back Developer + model Re-decide: continue or answer
Budget fallback Platform Cap calls and spend to prevent runaway
Popular Tags
Scroll to Top