59API

← Voltar aos guias

Tool Use and Function Calling: A Practical API Workflow

API · EN · 2026-09-04

What tool use actually means

Tool use lets a language model request an action that your application performs. The model does not directly access your database, browser, payment system, or internal API. Instead, you describe an available function, the model returns a structured tool call, and your code decides whether to execute it.

Function calling is the common API term for this pattern. Claude often describes the same capability as tool use. In both cases, the reliable workflow is: define a tool, send the user’s request and tool definition to the model, inspect the response, execute approved arguments, return the result, and ask the model for a final answer.

Step 1: Start with one narrow tool

Choose a task with a clear input and output. For example, an order-support assistant might expose a function named lookup_order. Its input could require an order ID as a string. Avoid a vague tool such as do_anything_in_the_database; narrow schemas are easier to validate and safer to authorize.

Describe each field precisely. Mark required values, explain accepted formats, and use an enumeration when only a small set of values is valid. A useful description tells the model that an order ID looks like ORD-12345 and that the function returns shipment status, carrier, and an estimated delivery date.

Step 2: Send the tool definition through your SDK

When using an OpenAI-compatible client, configure the client’s base URL as https://api.59api.com and provide your 59API key. Then send the conversation, the selected model, and the tool schema in the request. The same general design works with Claude models and GPT models, although the exact request fields can differ between provider-specific interfaces.

59API is useful for testing this workflow because it provides pay-as-you-go access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, as well as GPT models. It is also compatible with Claude Code, Codex, and OpenAI SDK-based applications, so you can prototype without committing to a large fixed subscription.

Step 3: Handle the model’s response as a state machine

Do not assume every response is a final answer. First inspect whether it contains a tool request. If it does, read the tool name and arguments, parse the arguments, and validate them against your own rules. A valid schema does not replace application-side validation.

After validation, your server calls the real order service. It then sends a tool-result message back using the required call identifier. The model can use that result to produce a natural response such as, “Your order shipped yesterday and is expected Friday.” If the tool fails, return a controlled error message rather than exposing a stack trace or internal credentials.

Step 4: Make execution safe and predictable

Read-only tools are the best starting point. For tools that send email, issue refunds, delete records, or place orders, add explicit confirmation. A good pattern is to let the model prepare the action, show the user the important details, and execute only after the user confirms.

Set timeouts and retry policies around the tool itself, not just the model request. Use idempotency keys for actions that could run twice. Limit the number of tool-call rounds to prevent loops, and consider allowing parallel calls only when the operations are independent. For example, checking inventory in two warehouses may be parallelized, while charging a card and creating a shipment usually needs ordered control.

Step 5: Test with realistic conversations

Test missing order IDs, malformed IDs, ambiguous requests, unauthorized users, service timeouts, empty results, and prompt-injection attempts such as instructions hidden in retrieved documents. Measure tool-call accuracy, validation failures, total latency, token usage, and the percentage of requests that need a second model turn.

Start with a cheap model for routine routing and extraction, then reserve a stronger model for complex reasoning. Because 59API offers low-cost pay-as-you-go access and a referral rebate, it is a practical way to compare Claude and GPT behavior with real traffic before scaling. If you want to try the workflow, sign up for 59API, point your existing compatible client at its API base URL, and begin with one read-only function.

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