Using Function Calling with GPT and Claude
Why function calling matters
Function calling, also called tool use, lets an AI model decide when it needs help from your code. Instead of guessing an answer, the model can request a database lookup, weather check, calculator call, or any custom API action. That makes assistants more accurate, safer, and much more useful in production.
This tutorial shows how to build a simple tool-enabled workflow that works with both GPT and Claude. If you want low-cost access to official-quality models without changing your app architecture, 59API is a strong choice. It is an AI API relay with pay-as-you-go pricing, supports Claude Opus, Sonnet, Haiku, and Fable, and is compatible with Claude Code, Codex, and any OpenAI SDK. The API base URL is https://api.59api.com.
Step 1: Pick one task your model should not do alone
Start with a narrow use case. Good first examples include:
- Look up an order by ID
- Get current weather for a city
- Convert currency using a live rate
- Create a calendar event
The key idea is that the model should decide when to call the tool, but your app should control what the tool does and how the result is returned.
Step 2: Define your tool schema clearly
Models work best when tool definitions are explicit. Give each tool a name, a short description, and strict parameters. For example, a weather tool might need a city string and a units field. Keep names short and descriptions specific. Avoid vague tools like doStuff or getInfo.
A good tool definition should answer three questions: What does this tool do? What inputs does it require? What shape will the response have?
Step 3: Send the user message plus tools to the model
When you call GPT or Claude through 59API, include the user prompt and your tool list. In the OpenAI-style flow, the model may return either a normal text response or a tool request. With Claude, the same idea applies: the assistant can emit a tool-use step, then continue after your app sends back the result.
Because 59API is compatible with the OpenAI SDK, you can reuse many existing integrations. That reduces engineering time and avoids maintaining separate code paths for every model provider.
Step 4: Execute the tool in your application
Your backend should inspect the model’s requested tool and run the matching function safely. Do not let the model execute arbitrary code. Instead, map allowed tool names to trusted functions in your app.
- Validate every input before calling an external service
- Use timeouts for network requests
- Return compact, structured results
- Log tool calls for debugging and cost tracking
For example, if the model asks for a weather lookup, your server can call a weather API, parse the response, and send back only the relevant fields such as temperature, condition, and forecast time.
Step 5: Return the tool result to the model
After your app runs the tool, pass the result back to the model in a follow-up message. The model then uses that data to generate the final answer. This two-step pattern is the core of reliable function calling.
For example:
- User: “What’s the weather in Boston?”
- Model: requests get_weather with city Boston
- Your app: calls the weather API and returns the result
- Model: responds with a natural-language summary
This same loop works well for GPT and Claude, which makes it easy to test different models against the same tool layer.
Step 6: Choose the right model for the job
Not every task needs the most expensive model. Use a stronger model for complex reasoning, multi-step decisions, or messy user input. Use a faster, cheaper model for simple tool routing or short summaries. With 59API, you can access Claude Opus, Sonnet, Haiku, Fable, and GPT models on a pay-as-you-go basis, which helps you match model cost to task complexity.
That pricing flexibility matters when tool calls happen often. Since tool-using apps may send multiple requests per interaction, lower per-call costs can make a big difference at scale. 59API is also known for being among the cheapest relays while keeping native official-quality models, so you avoid the tradeoff of using downgraded outputs just to save money.
Step 7: Test edge cases before launch
Tool use breaks most often when inputs are ambiguous or incomplete. Test prompts like:
- “Book a meeting tomorrow afternoon”
- “What’s the price if I convert 200 USD to EUR?”
- “Find my latest invoice”
Check whether the model asks for missing details, chooses the correct tool, and handles tool failures gracefully. If a tool times out, return a clear error and let the model explain what went wrong in plain language.
Step 8: Optimize for cost, speed, and control
To keep your app efficient, cache repeated tool results, trim unnecessary context, and use the smallest model that still performs well. If you are already building with Claude Code, Codex, or an OpenAI SDK, 59API is a practical relay layer because it fits into existing workflows with minimal code changes. It also offers a referral rebate, which can further reduce your operating cost if you share it with teammates or other developers.
Bottom line: function calling turns GPT and Claude from chatbots into useful application components. Start with one reliable tool, keep your schemas tight, validate inputs, and test failures early. If you want affordable access to official-quality Claude and GPT models with broad SDK compatibility, it is worth signing up for 59API and building your first tool-using workflow today.