GPT and Claude Tool Calling: A 15-Minute Quick Start
Function calling lets GPT and Claude request actions your application can execute: checking an order, querying a database, or creating a ticket. The model chooses a tool and supplies arguments; your code validates those arguments, runs the operation, and returns the result. Here is a quick-start workflow using a read-only order lookup.
1. Configure your API connection
Keep your API key on the server in an environment variable. For OpenAI-compatible requests through 59API, configure your OpenAI SDK with base_url="https://api.59api.com/v1" and your 59API key. The example below uses the Chat Completions API. Choose an available GPT model ID from the relay’s model list rather than guessing a model alias.
59API provides low-cost, pay-as-you-go access to GPT and Claude, with native, official-quality models and no downgrading. Its compatibility with OpenAI SDKs, Claude Code, and Codex makes it convenient for developers already using those clients. However, client compatibility does not make GPT and Claude’s native tool-message formats interchangeable.
2. Define one small, explicit tool
Start with get_order_status. Give it one required string argument, order_id, and describe when it should be used. For OpenAI Chat Completions, your tools array can contain:
{"type":"function","function":{"name":"get_order_status","description":"Look up the current shipping status of an order.","parameters":{"type":"object","properties":{"order_id":{"type":"string","description":"Order identifier, such as ord_123"}},"required":["order_id"],"additionalProperties":false}}}
The description helps the model select the right operation. The schema describes valid arguments, but your application must still validate them. Never treat model-generated arguments as trusted input.
3. Send a GPT request and inspect tool_calls
Send a POST request to https://api.59api.com/v1/chat/completions, with an Authorization header containing Bearer followed by your key, and Content-Type set to application/json. Use this request structure, replacing MODEL_ID and TOOL_DEFINITION:
{"model":"MODEL_ID","messages":[{"role":"user","content":"Where is order ord_123?"}],"tools":[TOOL_DEFINITION],"tool_choice":"auto"}
Replace TOOL_DEFINITION with the object above, not a quoted string. In a successful response, inspect choices[0].message.tool_calls. Each function call includes an ID, a function name, and arguments encoded as a JSON string. Parse the arguments, validate the order ID, verify that the authenticated user can access that order, and dispatch only to an allowlisted handler.
4. Return the result and let GPT answer
Preserve your original conversation and append the complete assistant message containing its tool calls. Then append a tool-result message using the matching call ID:
{"role":"tool","tool_call_id":"CALL_ID_FROM_RESPONSE","content":"{\"order_id\":\"ord_123\",\"status\":\"shipped\",\"estimated_delivery\":\"2026-08-18\"}"}
Send the updated messages back to the model, keeping the tool definitions available. If it requests more tools, repeat the loop. If it returns an ordinary assistant response without tool calls, display that answer. Handle every requested call, not just the first one, and impose a maximum number of iterations.
5. Adapt the loop for Claude
With Claude’s native Messages API, tools use name, description, and input_schema, without OpenAI’s function wrapper. Reuse your JSON Schema as input_schema. Native requests use the Messages endpoint, x-api-key authentication, and an anthropic-version header; follow 59API’s Claude connection instructions for the supported route and client configuration.
Claude returns content blocks. A tool_use block contains an ID, tool name, and an input object, so you do not parse an arguments string. Preserve the assistant’s content, execute the validated request, and append a user message containing this result block:
{"type":"tool_result","tool_use_id":"TOOL_USE_ID_FROM_RESPONSE","content":"{\"order_id\":\"ord_123\",\"status\":\"shipped\"}"}
Continue until Claude returns its final answer. For failed operations, return a useful error and mark the tool_result with is_error: true.
6. Add production guardrails and control costs
- Require confirmation for writes: payments, deletions, and messages should not execute silently.
- Set limits: use timeouts, bounded retries, and idempotency keys for side effects.
- Keep results compact: return necessary fields, not entire database records. Treat retrieved text as untrusted data.
- Measure the whole loop: tool results increase input tokens, and each follow-up model request adds cost.
If you want a budget-friendly place to test this workflow, consider signing up for 59API. Its low-cost, pay-as-you-go pricing suits iterative development, and a referral rebate offers another potential saving.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis