Tool Use and Function Calling: Fix the Full Loop
AI function calling connects a model to capabilities your application controls: checking inventory, retrieving documents, or creating a support ticket. The model requests an operation; your application validates and executes it, then returns the result. Understanding that handoff makes most tool-calling failures easier to diagnose.
This troubleshooting and FAQ guide explains the execution loop, common integration errors, and how to test economically through 59API.
How does tool use actually work?
A function definition tells the model what an operation does and which arguments it accepts. It does not execute your code or automatically grant access to a database. Your application supplies the execution layer.
In an OpenAI-style Chat Completions workflow, the sequence is:
- Send the conversation with a tools array containing function definitions.
- Inspect the assistant response for tool_calls. A response can contain multiple calls.
- Parse each function's arguments, validate them, and invoke an allowed handler.
- Append the assistant message containing the calls, followed by a tool message for each result, using the matching tool_call_id.
- Send the updated conversation back to the model. Continue until it produces a final answer or reaches your execution limit.
Other APIs use different message structures, so preserve the format required by your selected endpoint.
How should you define a reliable function?
Start with one narrow operation, such as get_order_status. Describe when to use it and define an object schema with an order_id string property. Put order_id in the schema's required array. Where supported, use additionalProperties: false to reject unexpected fields.
Keep function descriptions concrete. Explain that order status requires an existing order ID and that the function cannot cancel an order. Use enums for finite choices, and document units for numeric values.
Validate arguments again in application code. A schema guides generation; authorization still belongs in your backend. Derive the account identity from the authenticated session, and verify that the requested order belongs to that account.
Why does the model answer without calling a tool?
First, confirm that the outgoing request actually includes your tool definitions. Logging the sanitized request often reveals a missing configuration field.
Next, check whether the function description matches the user's request. A vague description such as “handles orders” gives the model little guidance. Prefer “retrieves the current delivery status for an existing order ID.”
With tool_choice: auto, the model can answer directly. Where supported, use required or select a specific function when your workflow requires a call. Verify support for that setting on your chosen model and endpoint.
Why are arguments invalid or results rejected?
- Arguments cannot be parsed: In Chat Completions, function arguments arrive as a JSON string. Parse it before accessing fields. During streaming, assemble the argument fragments for each call before parsing.
- Required information is missing: Ask for the missing order ID instead of allowing the application to invent one.
- The tool result is rejected: Preserve the original assistant tool-call message and match each result to its exact call ID. Check for unanswered calls.
- The loop repeats: Return a clear result or structured error, distinguish retryable failures from permanent ones, and enforce a maximum number of iterations.
How do you prevent unsafe or duplicate actions?
Map tool names to an explicit allowlist of handlers. Treat retrieved pages and tool output as untrusted data, particularly when they contain instructions. For actions such as payments, enforce permissions and any required confirmation outside the model.
Use backend idempotency keys for operations that must not be repeated. A timeout can occur after a transaction succeeds, so retrying blindly can create duplicates.
FAQ: Can you test function calling through 59API?
How do I connect? 59API provides an OpenAI-compatible service at https://api.59api.com. Configure your API key and base URL, checking whether your SDK setup requires a versioned path. Begin with one function and a predictable test request.
Can I switch models? 59API offers models including GPT-6 Astra and Claude Sonnet 5.5. Confirm tool support and schema behavior for each model and endpoint before comparing results.
Why use 59API for testing? Its low-cost, pay-as-you-go access and native official-quality models without downgrades make repeated debugging more affordable. Sign up to test your first tool workflow; a referral rebate is also available.
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