GPT & Claude Tool Calling: Zero to Production
Function calling lets GPT or Claude decide when your application should run a real operation, such as looking up an order, querying a database, or creating a calendar event. The model does not execute code itself. It returns a structured tool request, your server validates and runs it, and the result is sent back so the model can produce a useful response.
1. Choose an OpenAI-Compatible Starting Point
For a practical setup, use the OpenAI SDK and point it at 59API. It provides pay-as-you-go access to official-quality GPT and Claude models through https://api.59api.com, with no model downgrade. It is also compatible with Claude Code, Codex, and OpenAI SDK-based applications.
Install the SDK with pip install openai, then store your API key in an environment variable such as 59API_API_KEY. Configure the client with your key and set base_url to https://api.59api.com/v1 if your account documentation specifies the standard versioned route. Confirm the current model IDs in the 59API console before deployment, because model names can change.
2. Define a Narrow Tool Schema
A tool definition has a name, a description, and a JSON Schema for its arguments. Make the schema specific enough to prevent ambiguous calls. For example, a weather tool might accept a required city string and an optional units value restricted to metric or imperial.
Descriptions matter. Tell the model what the tool does, when it should be used, what each argument means, and whether the result is authoritative. Keep one tool responsible for one operation. A broad tool such as “run SQL” creates unnecessary security and reliability problems.
3. Implement the Tool-Calling Loop
Send the user message together with the available tools and request automatic tool selection. If the response contains ordinary text, return it. If it contains one or more tool calls, validate each argument against your schema, execute the corresponding server-side function, and append the tool result to the conversation using the call ID.
Then send the updated conversation back to the model. The second response usually contains the final answer, although a task may require several rounds of calls. Set a maximum number of iterations, such as five, to prevent accidental infinite loops. Never allow the model to choose arbitrary function names, import modules, or write directly to production systems.
4. Account for GPT and Claude Differences
GPT and Claude both support structured tool workflows, but response details can differ by model and compatibility layer. Normalize tool calls in your application around a small internal shape: tool name, call ID, parsed arguments, result, and error. Test whether the selected model supports parallel tool calls, forced tool selection, and structured outputs before relying on those features.
Use the same business functions underneath both models. This separates model-specific message parsing from your application logic and makes model fallback much easier. If a provider returns malformed arguments, reject them and send a concise validation error back to the model rather than executing a partial request.
5. Harden the Production Path
- Validate every argument on the server, including IDs, enum values, ranges, and authorization scope.
- Give tools explicit timeouts and return structured errors that do not expose credentials or stack traces.
- Use idempotency keys for payments, emails, and other operations that must not run twice.
- Log model, tool name, latency, token usage, request ID, and outcome, while redacting personal and secret data.
- Require confirmation before destructive actions, even when the model requested them.
- Set spend limits and alerting so retries or long tool chains cannot create unexpected costs.
6. Deploy with a Realistic Test Set
Create tests for direct answers, missing arguments, invalid arguments, tool timeouts, permission failures, duplicate calls, and multiple tools in one request. Measure both functional accuracy and cost. Start with a fast, inexpensive model for routine calls and route complex reasoning to a larger GPT or Claude model only when needed.
59API is useful here because a single OpenAI-compatible integration can provide economical access to GPT and Claude options, including Claude Opus, Sonnet, Haiku, and Fable. Its pay-as-you-go pricing and referral rebate can reduce experimentation and operating costs. When you are ready to test a production tool workflow, sign up for 59API and begin with a small spending limit.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis