Reliable LLM tool calling is less about asking a model to use a function and more about designing a controlled execution loop around it. This guide explains how to define function schemas, validate arguments, enforce permissions, handle sequential and parallel calls, format results, and recover from failures. It also provides a practical tracking routine for revisiting tool integrations as models, SDKs, schemas, and production behaviour change.
Overview
LLM tool calling, also called function calling or AI tool use, allows a language model to request an operation from your application. The model does not normally execute the operation itself. Instead, it selects a declared tool, proposes arguments, and returns a structured request. Your application then decides whether the request is valid and permitted, runs the underlying function, and sends the result back to the model.
A dependable implementation treats the model as an intent and argument generator, not as an authority. The application remains responsible for authentication, authorisation, input validation, business rules, side effects, and error handling. This distinction matters for tools that send messages, change records, create purchases, access private data, or trigger external systems.
A useful control flow is:
- Describe a small set of tools with precise schemas.
- Send the user request and tool definitions to the model.
- Inspect the model response for tool calls.
- Validate each tool name and argument against application rules.
- Ask for confirmation when the action is consequential or ambiguous.
- Execute approved calls with timeouts and observability.
- Return a bounded, clearly labelled result to the model.
- Generate the final user-facing response or a safe recovery message.
This pattern complements structured output prompting and output guardrails. For related validation patterns, see LLM Output Guardrails: Validation Patterns for Safer Production AI Responses and Structured Output Prompting: JSON Schema Examples for Reliable LLM Responses.
What to track
1. Tool schema quality
Every tool should have one clear purpose, a stable name, and a description that explains when it should and should not be used. Keep parameters narrow. A tool called search_orders is easier to govern than a general-purpose manage_customer_data function with many unrelated modes.
Use explicit types, required fields, sensible limits, and enumerated values where possible. For example:
{
"name": "search_orders",
"description": "Find orders belonging to the authenticated customer using an order ID or email address.",
"parameters": {
"type": "object",
"properties": {
"order_id": { "type": "string", "description": "A known order reference" },
"status": { "type": "string", "enum": ["open", "shipped", "cancelled"] },
"limit": { "type": "integer", "minimum": 1, "maximum": 20 }
},
"required": ["limit"]
}
}The exact wrapper and field names vary between model APIs and SDKs, so treat this as a design template rather than a provider-specific request. Track schema changes in version control, including descriptions. A wording change can alter model selection even when the formal type definition stays the same.
2. Argument and permission failures
Record whether a call failed because the model selected the wrong tool, omitted a required value, produced an invalid type, requested an unsupported value, or attempted an operation outside the user’s permissions. Keep these categories separate. A schema problem needs a different fix from an authorisation failure.
Never rely on the model to enforce access control. Resolve the authenticated user, tenant, resource ownership, and allowed operation in application code. A valid-looking argument is not evidence that the caller may use it.
3. Operational behaviour
Monitor execution time, timeout rate, retry count, tool-call frequency, downstream errors, and the size of tool results returned to the model. Also track duplicate calls. Repeated writes can be particularly damaging if a retry occurs after the external service completed the first request but the response was lost.
For side-effecting functions, use idempotency keys or an equivalent application-level safeguard where the underlying system supports it. Log a correlation ID, tool name, schema version, validation outcome, and execution status, while excluding secrets and unnecessary personal data.
4. Result quality
A technically successful tool call can still produce a poor answer. Check whether the result contains the fields the model needs, whether it is stale or ambiguous, and whether its size encourages the model to overlook important details. Return compact, purpose-built results rather than dumping an entire database record or raw service response.
Cadence and checkpoints
A monthly or quarterly review is a practical baseline for active integrations. Review more frequently after launching a new tool, changing a schema, migrating models, or investigating an incident. The goal is not to inspect every conversation manually; it is to compare a repeatable set of signals.
At each review
- Sample successful and failed calls from each important tool.
- Compare validation failures with downstream service failures.
- Check whether tool descriptions still match current business rules.
- Review calls that required retries, clarification, or human confirmation.
- Run a fixed evaluation set containing normal, ambiguous, adversarial, and edge-case requests.
- Confirm that permissions are checked independently of model-generated arguments.
- Inspect result sizes and redactable fields sent back into the model context.
- Record schema, prompt, model, SDK, and application versions for each test run.
Maintain a small regression suite rather than relying only on production traffic. Include cases such as a missing identifier, conflicting identifiers, an unknown enum value, a request for another user’s record, a duplicate write, a tool timeout, and a downstream response containing unexpected fields. The LLM Evaluation Metrics and Test Cases guide can help organise this into a repeatable testing process.
Before a release
Test the complete loop, not just the model response. Verify that invalid calls are rejected, denied actions do not reach the external system, timeouts produce bounded recovery, and the final response does not claim an action succeeded when execution was incomplete. If a model or provider changes, rerun the same fixtures before comparing any apparent quality improvement.
How to interpret changes
When tool-call errors increase, first identify which layer changed. A higher rate of malformed arguments may indicate a schema or prompt issue. More permission denials may reflect a new user flow or better enforcement rather than model degradation. Longer execution times may come from the downstream service, extra sequential calls, or oversized results.
Compare like with like. Separate read-only tools from side-effecting tools, simple lookups from multi-step workflows, and first attempts from retries. A single aggregate success rate can hide a serious failure in a low-volume but high-impact operation.
Use these diagnostic questions:
- Did tool selection change? Review descriptions, available tools, and ambiguous names.
- Did argument shape change? Compare the emitted call with the current schema and normalise types only where safe.
- Did execution change? Inspect permissions, service responses, timeouts, and idempotency handling.
- Did result interpretation change? Check result labels, truncation, ordering, and instructions about uncertainty.
- Did the surrounding context change? Long conversations, retrieved documents, or injected text can influence tool selection.
Do not automatically solve errors by adding more instructions to the system prompt. A clearer schema, a smaller tool set, stricter validation, or a better confirmation step is often more durable. For prompt patterns specifically designed for tool use, see Function-Calling Prompt Templates: Reliable Tool Use for LLM Applications.
Sequential and parallel calls
Sequential calls are appropriate when the second operation depends on the first, such as retrieving an account before checking its permissions. Parallel calls can reduce waiting time when operations are independent, such as querying separate read-only sources. Before enabling parallel execution, confirm that calls have no ordering dependency, do not compete for mutable state, and can be safely retried independently.
For either pattern, set a maximum number of tool turns. A loop that allows unlimited model calls can become expensive, slow, or difficult to diagnose. Cost and token controls are covered in How to Reduce LLM API Costs: Token Optimization Techniques and a Review Checklist.
When to revisit
Revisit this implementation monthly or quarterly, and immediately after any event that can alter tool behaviour. The most important triggers are a model migration, SDK or API terminology change, function-schema revision, new permission rule, external service change, incident, unusual rise in retries, or addition of a side-effecting tool.
Keep a short change record with the date, affected tools, expected behaviour, test results, observed production impact, and rollback plan. This makes the article’s checklist useful during incident review rather than only during initial development. Recheck provider documentation whenever you depend on a particular schema feature, parallel-call behaviour, argument mode, or response format; support and naming conventions can change.
A practical next step
- Choose one high-value tool and write down its intended inputs, outputs, permissions, and failure states.
- Reduce its schema to the smallest useful surface.
- Add application-side validation, authorisation, timeout, and idempotency handling.
- Create at least ten regression requests, including ambiguous and denied cases.
- Measure selection, validation, execution, retry, latency, and final-answer outcomes separately.
- Schedule the next review and store the current schema and test results with the code.
Reliable function calling is an ongoing engineering practice. Treat schemas as interfaces, model output as untrusted input, and tool execution as a controlled application workflow. That approach makes AI tool use easier to test, safer to operate, and simpler to update when the surrounding model stack changes.