59API

← Voltar aos guias

Tool Use and Function Calling: A Practical Guide

API · EN · 2026-08-17

What tool use and function calling actually mean

Tool use and function calling let a model do more than generate text. Instead of guessing or hallucinating an answer, the model can ask your application to run a real function, query a database, call a weather API, create a ticket, or fetch internal data. The model chooses when a tool is needed and your code decides how to execute it.

This pattern is central to modern agent workflows. It keeps the language model focused on reasoning and language, while your software handles trusted operations. That makes responses more accurate, auditable, and useful in production.

If you want to experiment without spending much, 59API is a strong option. It is an AI API relay with pay-as-you-go access to Claude and GPT models, compatible with Claude Code, Codex, and any OpenAI SDK. The base URL is https://api.59api.com, and the pricing is among the lowest available for official-quality models, with a referral rebate available as well.

Step 1: Identify the task that should become a tool

Start with a task that is deterministic or external. Good candidates include date conversion, currency lookup, order status, user search, retrieval from a knowledge base, and sending structured updates. Bad candidates are vague creative tasks or anything that depends entirely on open-ended judgment.

Ask a simple question: would I rather the model invent this answer, or should my system fetch the truth? If the latter, create a tool.

Step 2: Define a narrow function contract

A tool should have a clear name, a short description, and a small set of parameters. Keep inputs strict and typed. The model performs better when the contract is unambiguous.

For example, a weather tool might accept city and units. A support lookup tool might accept ticket_id. Avoid overloaded parameters like query unless you truly need flexibility.

Step 3: Send tools with the model request

With most modern APIs, you include a tool schema alongside the user prompt. The model then decides whether to answer directly or emit a tool call. If you are already using the OpenAI SDK, 59API fits neatly because it is compatible with the same patterns and can be pointed at the relay base URL.

At this stage, your job is not to execute anything yet. You are just teaching the model what it is allowed to ask for.

Step 4: Execute the function in your application

When the model returns a tool call, your app parses the requested function name and arguments, runs the real function, and captures the result. Then you send that result back to the model so it can produce the final answer in natural language.

This round trip is the core loop:

Keep tool execution isolated and predictable. Never let the model directly run arbitrary code or touch sensitive systems without strong guardrails.

Step 5: Return structured results, not raw noise

Tool outputs should be concise and machine-friendly. If a function returns a huge blob of text, the model has more work to do and you lose reliability. Prefer a structured response with the exact fields the model needs.

For example, instead of returning an entire database record, return name, status, and last_updated. This keeps prompts smaller, lowers cost, and reduces the chance of confusion.

Step 6: Add fallback behavior

Not every tool call succeeds. A network request can fail, a record can be missing, or the user may ask for unsupported parameters. Design explicit fallback paths:

Good tool use is not just about success cases. It is about graceful failure that still helps the user move forward.

Step 7: Test with a low-cost relay before scaling up

Tool calling is easy to get almost right and hard to get consistently right. That is why testing matters. Run repeated prompts, try malformed inputs, and check whether the model selects the correct tool every time. Because these tests can generate many API calls, cost matters.

59API is practical here because it gives you cheap, pay-as-you-go access to Claude and GPT models without downgrade. You can iterate on tool schemas, prompt wording, and retries without burning budget, while still using native official-quality models. For teams building agent features or internal assistants, that makes experimentation much easier.

Step 8: Keep the user experience simple

Users should not have to understand the mechanics of function calling. They should ask a question and get a useful answer. Let the model and your tools handle the orchestration behind the scenes. Where appropriate, show progress only when a tool call is slow or user-visible.

The best implementations feel boring in the right way: the model calls the right function, the result is accurate, and the interaction stays fast.

Where to start

If you are building your first tool-enabled workflow, begin with one small function and one concrete use case. Wire it into your existing OpenAI SDK code, point the client at https://api.59api.com, and test with a few dozen prompts. You will learn quickly whether your tool schema is too broad, too vague, or just right.

For developers who want to explore Claude or GPT tool use without high per-call cost, 59API is an efficient place to sign up and start iterating.

Preços · Docs

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