How to Implement Function Calling in Your LLM App
Function calling lets a language model run your tools and use the results in its answer. Learn how to define tools, wire up the loop and handle bad calls.
On this page
Short answer
Function calling lets a language model request a specific action from your code, such as checking an order status, and then use the result in its answer. You define the tools, the model chooses when to call them, and your code runs them and returns the result.
What is function calling?
Function calling is a feature that lets a language model ask your code to run a named tool and return the result, instead of only producing text. The model does not run the tool itself; it returns a structured call, such as get_order_status with an order number, and your application executes it.
Your code then sends the tool result back to the model, and the model writes the final answer using that result. This is how a support assistant can look up a real order, or a booking assistant can check a real flight, instead of guessing from its training data.
The pattern works with most hosted models and with open models that support tool use. It is the same idea behind what function calling is, and the implementation detail is where most projects go wrong: the model call is easy, but the plumbing around it decides whether the feature is safe.
How do you design the tools the model can call?
Design each tool as one small, named action with a clear purpose, such as get_order_status or cancel_booking. A tool should do one thing, take only the inputs it needs, and return a short, structured result that the model can quote.
Keep the tool set small. A model choosing between three clear tools works better than one choosing between thirty overlapping ones. If two tools do almost the same thing, merge them and use a parameter to switch behaviour.
| Design rule | What it means | Why it matters |
|---|---|---|
| One action per tool | get_order_status only reads an order | Clear choices for the model |
| Plain names | Names that state the action | Fewer wrong calls |
| Typed inputs | Order number as a string, dates as dates | Bad arguments are caught early |
| Short results | Return fields, not whole records | Lower token use |
| Read before write | Separate lookup tools from change tools | Safer approval rules |
Write the description for the model, not for a human reader. State when to call the tool, what each parameter means, and what the result contains. A vague description produces vague calls, and vague calls produce wrong answers.
What does a function definition look like?
A function definition is a small schema that tells the model the tool name, a description, and the parameters it accepts. The schema is written in JSON Schema, the same format used to describe data shapes in APIs.
Each parameter needs a name, a type, and a short description. Mark the parameters the tool cannot run without as required, and give the model an example value where the format is not obvious, such as a date in YYYY-MM-DD form.
| Schema field | What it holds |
|---|---|
| name | The tool name, such as get_order_status |
| description | When to call it and what it returns |
| parameters | The inputs, each with a type and description |
| required | Which parameters must be present |
| type | The data type of each parameter |
Keep descriptions short and concrete. A description that says 'use this to check an order' is less useful than one that says 'use this when the user asks where their order is; returns status, tracking number and expected delivery date'.
How do you wire function calling into your app?
You wire function calling into your app as a loop: send the tools with the prompt, run any call the model returns, send the result back, and repeat until the model answers in plain text.
- Define the tools. Write the schemas for each tool and the code that runs them, with validation on every input.
- Send tools with the prompt. Pass the tool definitions with the user message, so the model knows what it can call.
- Parse the model reply. If the reply contains a tool call, extract the tool name and arguments.
- Validate the arguments. Check types, ranges and required fields before running anything.
- Run the tool. Execute the code, with a timeout, and capture the result or the error.
- Return the result. Send the result back to the model and let it write the final answer or call another tool.
Set a limit on loop turns, such as five, so a model that keeps calling tools cannot run forever. Log every call, argument and result, because that log is how you debug a wrong answer later.
For the running costs of this loop, see how much function calling costs to run. Long tool outputs are the usual surprise on the bill, so keep results short.
Example (hypothetical): a travel booking assistant
Example (hypothetical): a travel company adds a booking assistant that can check flights and change seats. It defines two tools: check_flight, which takes a booking reference and returns status and times, and change_seat, which takes a booking reference and a seat code.
On a typical day the assistant handles 240 booking questions. The model calls check_flight 240 times and change_seat 30 times, so 270 tool calls in total. Each call adds about 1.2 seconds of tool time, which is 270 times 1.2, or 324 seconds, just over five minutes spread across the whole day.
Of the 30 seat changes, 4 fail because the seat is already taken. The tool returns a clear error, the model asks the user to pick another seat, and no booking is left half-changed. The team reviews the log each week and finds that 12 of the 240 check_flight calls used a wrong booking reference, so they add a format check that asks the user to confirm the reference before the tool runs.
How do you handle errors and bad tool calls?
You handle errors by validating every call before it runs and by treating every tool result, including failures, as data the model must see. A tool that fails silently is worse than one that fails loudly, because the model will invent an answer to fill the gap.
- Unknown tool name. Reject the call and tell the model the tool does not exist, so it can pick a real one.
- Missing or wrong arguments. Return a short error naming the field, so the model can ask the user for it.
- Tool timeouts. Set a per-tool timeout and return a timeout message instead of hanging the conversation.
- Downstream failures. If the database or API behind a tool fails, return a plain error and offer the user a fallback, such as a human handoff.
- Repeated failures. Stop the loop after two failed calls for the same tool and hand the conversation to a person.
Never let the model retry a write action, such as a refund or a cancellation, without a fresh check. A retry that runs twice can charge twice. Give each write tool an idempotency key, so a repeated call with the same key returns the first result instead of running again.
If you outgrow this hand-rolled loop, agent frameworks add retry, routing and planning around the same mechanism. See how agent frameworks compare with plain function calling before you adopt one.
What are the common mistakes?
The most common function calling mistakes are too many tools, vague descriptions, no validation, and letting the model run write actions without a human check.
- Too many tools. Thirty tools with overlapping purposes confuse the model; five clear ones work better.
- Vague descriptions. A tool described as 'helps with orders' will be called for the wrong reasons.
- No argument validation. Trusting model output as clean input lets bad data reach your database.
- Write actions without approval. Refunds, cancellations and payments need a confirmation step before the tool runs.
- No logging. Without a log of calls and results, a wrong answer cannot be traced back to its cause.
- Long tool outputs. Returning whole records inflates token use and buries the facts the model needs.
Key takeaways
- Function calling lets the model request a named tool and use the result in its answer
- Small, clearly described tools beat a large set of overlapping ones
- Validate every argument and set a timeout before a tool runs
- Limit the loop to a few turns and log every call for debugging
- Write actions need approval and an idempotency key so a retry cannot run twice
Frequently asked questions
Do I need fine-tuning to use function calling?
No. Function calling works with a base model that supports tool use, and the tool definitions are sent with each request. Fine-tuning can help when you need a consistent call format at high volume, but most teams should ship with prompting and tool schemas first, then decide if tuning is worth it.
How many tools should I give the model?
Start with three to eight tools that cover the main actions. Each extra tool adds choice and token use, and overlapping tools increase wrong calls. Add a new tool only when a real user request cannot be handled by the ones you have, and remove tools that are rarely called.
What happens if the model calls a tool with wrong arguments?
Your code should validate the arguments and return a short error naming the problem field, instead of running the tool. The model can then ask the user for the missing detail or correct the value. Never pass model output straight into a database query or a payment call without this check.
Can function calling change data, or only read it?
It can do both, but write tools need extra care. Separate read tools from write tools, add a confirmation step for changes, and use an idempotency key so a retry does not run the action twice. Log every write with the user, the arguments and the result.
How is function calling different from an agent framework?
Function calling is the low-level mechanism: the model returns a structured call and your code runs it. An agent framework adds the loop, memory, planning and tool routing around that mechanism. Plain function calling is enough for one or two tools; a framework helps when many tools and steps have to be coordinated.
Why does my model keep calling the wrong tool?
Usually the tool descriptions overlap or are too vague. Rewrite each description to state exactly when to call the tool and what it returns, rename tools that sound alike, and check the log for the calls that went wrong. If two tools keep getting confused, merge them into one tool with a parameter.