Tool Use and Function Calling: A Practical Deep Dive
Tool use, also called function calling, lets a language model request an action from your application instead of pretending it performed one. The model does not directly execute your database query, send an email, or call an external API. It produces a structured tool request; your code validates and executes that request, then returns the result for the model to interpret.
Build the right mental model
A reliable tool-calling workflow has four distinct stages:
- Describe: Send the model a tool name, purpose, and input schema.
- Decide: The model chooses whether a tool is appropriate and generates arguments.
- Execute: Your server validates the arguments and performs the operation.
- Continue: Return a tool result so the model can produce an answer or request another tool.
This separation matters. Treat model output as an untrusted proposal, not as executable code or proof that an action succeeded. The source of truth remains your application and its connected services.
Design schemas that prevent ambiguity
Most tool failures begin with vague definitions. A good schema is narrow, explicit, and easy to reject. For example, a weather tool should require a city or well-defined latitude and longitude, constrain units to an allowed set, and explain whether the location must be resolved before execution.
- Use descriptive property names such as customer_id instead of generic names such as value.
- Mark genuinely required fields as required; do not rely on the model to infer them.
- Use enums for finite choices, including currency, sort order, or account type.
- Set boundaries for numbers, string lengths, dates, and result limits.
- Keep one tool focused on one business capability rather than exposing a giant multi-purpose endpoint.
Descriptions should explain business meaning and important edge cases. State whether an operation is read-only, whether it changes data, and what the model should do when information is missing. If your provider supports strict structured outputs, enable them for arguments that must match the schema exactly, while still performing server-side validation.
Implement the execution loop safely
After the model response, inspect the finish reason or equivalent response field and collect every requested tool call. Parse each argument payload with a real JSON parser. Then validate it against the same schema used by your application. Never construct SQL, shell commands, or authorization decisions directly from raw arguments.
Execute independent read-only calls in parallel when latency matters, but preserve the association between each tool-call identifier and its result. For writes, use idempotency keys. A timeout or network retry must not accidentally create two refunds, orders, or messages. Return compact, typed tool results: include the relevant data, an error category, and a recoverable explanation rather than an enormous raw stack trace.
Finally, append the tool result to the conversation using the provider's required tool-result format and call the model again. Set a maximum number of tool rounds, such as five, to prevent recursive loops. If the limit is reached, return a transparent fallback and log the conversation for investigation.
Secure tools like production endpoints
Tool descriptions are not security controls. Apply the user's identity, permissions, tenant boundaries, and rate limits in your server, independently of what the model requests. Separate read tools from write tools, require confirmation for irreversible actions, and redact secrets and personal data before sending results back to the model.
Log the tool name, validated arguments, latency, outcome, token usage, and correlation ID. Avoid logging credentials or full sensitive payloads. These records make it possible to distinguish schema errors, model selection errors, provider failures, and bugs in your own executor.
Control cost and provider behavior
Tool definitions consume input tokens on every request, so keep descriptions concise and only expose tools relevant to the current task. Cache stable reference data, truncate oversized tool results, and choose a smaller model for classification or simple lookups while reserving stronger models for multi-step reasoning.
For developers using Claude Code, Codex, or an OpenAI SDK, 59API offers a practical low-cost relay with pay-as-you-go access to native official-quality Claude Opus, Sonnet, Haiku, and Fable models, as well as GPT models. Point your client at https://api.59api.com as the API base URL, then keep your existing SDK integration while comparing model cost and latency. Its referral rebate can further reduce effective spend. If you want inexpensive experimentation without committing to a subscription, signing up for 59API is a sensible next step.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite