Function Calling Best Practices for Reliable LLM Apps
Function calling lets a model act through your code, so a bad call can change data or charge a card. These practices keep calls valid, safe and testable.
On this page
Short answer
Function calling best practices are the habits that keep a language model from making bad tool calls: clear schemas, strict validation in code, safe defaults, limited retries and tests that run before every release. They matter because a bad call can change data, charge a card or send a message, while a bad text answer is only embarrassing.
What are function calling best practices?
Function calling best practices are the habits that keep a language model from making bad tool calls: clear schemas, strict validation, safe defaults and tests that run before every release. Function calling lets a model ask your code to do things, such as look up an order or book a slot, instead of answering from memory. For the basic mechanism, see what function calling is.
The stakes are higher than in plain text generation, because a bad call can change data, charge a card or send a message. A model that invents a fact in a chat reply is embarrassing; a model that invents a function argument can book the wrong flight. The practices below exist to stop that.
They fall into four groups: design the schema so the model can use it, validate every call before it runs, handle failure gracefully, and test and monitor the tools as if they were part of your code - because they are.
How do you design schemas a model can use?
Design the schema for the model, not for your database. The model sees names, descriptions and types, and it fills in arguments from the conversation. If a field name is unclear, the model will guess, and the guess will sometimes be wrong.
| Rule | What it means | Why it matters |
|---|---|---|
| Names say what the field is | Use "order_number", not "id" | Stops the model picking the wrong identifier |
| Descriptions state the format | Say "ISO date, YYYY-MM-DD" | Stops the model inventing formats |
| Required fields only | Mark optional fields optional | Fewer missing-argument errors |
| Enums over free text | Status must be "open", "shipped" or "cancelled" | The model cannot invent a new status |
| Keep the tool list short | Fewer than ten tools per call | The model picks the right tool more often |
Keep each tool narrow. A tool called "manage_order" that can cancel, refund, change or rebook is four tools hiding in one, and the model will struggle to choose the right arguments. Split it into four tools with clear names.
How do you validate a call before running it?
Validate every call in code before it touches a system. Check that the function name exists, that every required argument is present, that each argument has the right type, and that the values are in range. Reject anything that fails, and tell the model what was wrong so it can try again.
Validation is also where business rules live. A refund tool should check that the order exists, that it is refundable, and that the amount matches the order total before it runs. These checks belong in your code, not in the prompt, because the model cannot be relied on to enforce them. The build steps are covered in how to implement function calling.
Log the rejected calls. A spike in rejected calls usually means a schema problem or a prompt change, and the log is the fastest way to find it.
How do you handle failures and retries?
Treat every tool call as something that can fail. The API can time out, the database can be slow, or the model can pass an argument that passes validation but still makes no sense. Your code should catch the failure, return a short error the model can use, and let the model decide what to do next.
Set a limit on retries and on total steps. A model that retries the same failing call five times wastes time and money. Two retries with a short pause is enough for most transient failures. After that, hand the task to a person or stop with a clear message.
Never retry a call that changes data without checking whether the first attempt went through. A payment or booking retried blindly can run twice. Check the state first, then decide.
How do you test function calls?
Test function calls the way you test any other code: with fixed cases and expected results. A prompt that works today can break tomorrow when the model changes, so the tests are what keep the system honest.
- Write test prompts with expected calls. For each case, write down the exact function and arguments you expect.
- Run them on every change. Model, prompt or schema changes all trigger a test run.
- Check the arguments, not just the name. A call to the right function with the wrong order number still fails.
- Include the awkward cases. Missing data, ambiguous requests and two tools that look alike.
- Record the results. Keep a history so a regression is visible instead of anecdotal.
The awkward cases are where most failures hide. A request that could match two tools, or a missing order number, is exactly where the model guesses. If your tests only cover the clean path, production will find the rest. The wider architecture is covered in tool use in LLMs.
Example (hypothetical): an order-status assistant that stops failing
Example (hypothetical): a shop's assistant answers "where is my order?" by calling a lookup tool. In the first week, 120 of 400 calls fail: 60 because the model passes a tracking number where the tool expects an order number, 40 because customers give a name instead of a number, and 20 because the order is from before the system started.
The team renames the argument from "id" to "order_number", adds a description that says "the 8-digit order number from the confirmation email, not the tracking number", and adds a search tool that finds an order by name and postcode. Failed calls fall from 120 to 18 out of 400, and the remaining 18 are mostly orders older than the system, which the assistant now explains instead of failing silently.
The fix cost one afternoon. The lesson is that most function calling failures are schema and naming problems, not model problems, and they show up in the logs before they show up in complaints.
How do you version and monitor your tools?
Version your tools like an API. When a schema changes, give it a new version and keep the old one working until callers have moved. A model that learned the old schema from a cached prompt will keep calling it, and a hard cutover breaks it overnight.
Monitor three numbers: the share of calls that pass validation, the share that succeed, and the share that need a retry. A sudden move in any of them after a prompt or model change is a warning. Log the full call - function name, arguments, result - so you can replay a failure. The security side of this is covered in AI security.
Review the logs weekly at first. The patterns you find - a tool that is never chosen, an argument that is always wrong - are usually the next schema fix.
What are the common mistakes?
The most common function calling mistakes are trusting the model to validate, giving tools broad permissions, and shipping without tests.
- Trusting the prompt to enforce rules. Business rules belong in code, where they cannot be talked around.
- Broad tool permissions. A lookup tool that can also refund is a risk, not a convenience.
- One tool doing many jobs. A combined tool makes the model guess which job you mean.
- No tests on awkward cases. Missing data and ambiguous requests are the usual failures.
- Blind retries on actions that change data. A retried payment can run twice.
- No logging. Without the call log, a failure cannot be replayed or explained.
The fix is to treat the model as an untrusted caller. Validate in code, scope permissions to the task, and test the awkward cases before users find them.
Key takeaways
- Design schemas for the model: clear names, stated formats, enums instead of free text
- Validate every call in code before it runs, including business rules like refund limits
- Limit retries to two and never retry a data-changing call without checking state first
- Test the awkward cases - missing data, ambiguous requests, similar tools - not just the clean path
- Version tools like an API and monitor validation, success and retry rates weekly
Frequently asked questions
What is function calling in an LLM app?
Function calling lets a language model ask your code to run a tool, such as looking up an order or booking a slot, instead of answering from memory. The model picks a function from a schema you define and fills in its arguments, and your code runs the call and returns the result for the model to use.
Why do function calls fail?
Most failures are schema problems, not model problems: an unclear argument name, a missing description of the expected format, or two tools that look alike. Other common causes are missing data from the user, a tool that times out, and a prompt change that alters how the model fills arguments.
Should the model or the code validate a function call?
The code should validate. Check the function name, required arguments, types, ranges and business rules in code before the call runs, and return a short error the model can use when validation fails. A prompt can ask for correct values, but it cannot enforce them.
How many tools should a model see at once?
Keep the list short, ideally fewer than ten tools per call, and keep each tool narrow. A long list makes the model pick the wrong tool, and a broad tool that does many jobs makes it guess which job you mean. Split broad tools into named, single-purpose ones.
How do you test function calling?
Write test prompts with the exact function and arguments you expect, run them on every model, prompt or schema change, and check the arguments as well as the function name. Include awkward cases such as missing data and ambiguous requests, and record results so regressions are visible.
Is function calling safe for payments and bookings?
It can be, with controls: validate in code, scope the tool's permissions to the task, check state before retrying a data-changing call, and require a human approval step for high-value actions. Log every call so failures can be replayed and explained.