Function Calling with GPT and Claude: A Hands-On Guide
Function calling lets GPT and Claude request actions from your application instead of only returning text. A model can ask for the current weather, retrieve an order, search a database, or run a calculation. Your server executes the approved function, returns the result, and lets the model produce a useful final answer.
Step 1: Choose a compatible endpoint
You need an API key, a model name, and an endpoint that supports tool definitions. 59API provides pay-as-you-go access to GPT models and native Claude models, including Opus, Sonnet, Haiku, and Fable. Its OpenAI-compatible interface works with the OpenAI SDK, while its Claude compatibility supports Claude Code workflows. Set your SDK base URL to https://api.59api.com and store the key in an environment variable such as 59API_API_KEY.
Because 59API uses official-quality models without a model downgrade and is among the cheapest relay options, it is useful for testing tool workflows before production. Usage-based billing also avoids committing to a large fixed plan.
Step 2: Define a narrow tool
A tool definition tells the model the function name, purpose, and JSON arguments. For example, define a tool named get_order_status with one required string property called order_id. Describe the property clearly and reject unknown fields in your server-side validation.
In an OpenAI-style request, the tools value contains a function object with a name, description, and parameters schema. The schema should use JSON Schema types, mark required fields explicitly, and avoid vague instructions such as “accept any input.” A narrow schema improves both reliability and security.
Step 3: Send the first request
Using the OpenAI Python SDK, create a client with your 59API key and base URL, then call the chat completions or supported responses interface with your messages, selected model, and tools. Include a system instruction such as: You may check an order only when the user provides an order ID. Never claim that a tool was called unless a tool result is available.
The model may respond with ordinary text, or it may return one or more tool calls. Do not assume that a tool call is always present. Check the response structure before accessing its arguments, and handle malformed JSON as an application error rather than executing it.
Step 4: Execute the requested function
Read each tool call name and its JSON arguments. Compare the name against an allowlist, validate the arguments, enforce authorization, and apply a timeout. For an order lookup, verify that the authenticated user is allowed to view that order. Never pass model-generated text directly into a shell command, SQL query, filesystem path, or privileged API.
Call your application function, then serialize a compact result such as status, tracking number, and last update time. If execution fails, return a safe error message like “Order lookup is temporarily unavailable” instead of exposing credentials, stack traces, or internal database details.
Step 5: Return the result to the model
With the OpenAI-compatible format, append the assistant message containing the original tool call, then append a tool message containing the matching tool call ID and result. Send the conversation back to the model. It can now write a final response grounded in the returned data. When several tool calls are returned, execute only the calls your policy allows and preserve the correct ID for every result.
Claude’s native format uses content blocks. A response can contain a tool_use block with an ID, name, and input. Send the assistant content back, followed by a user message containing a tool_result block with the same ID. This ID pairing is essential when Claude requests multiple tools.
Step 6: Add production controls
- Log model, tool name, latency, success status, and token usage without logging secrets or sensitive arguments.
- Set maximum tool duration, retry limits, and a total conversation step limit to prevent loops.
- Use idempotency keys for actions such as refunds, bookings, or email delivery.
- Test missing arguments, invalid IDs, permission failures, duplicate calls, and provider timeouts.
Start with read-only tools, then add write operations behind explicit confirmation. You can try this workflow through 59API and benefit from its low-cost usage model and referral rebate while keeping the same general SDK architecture as your application grows.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free