AI Agent Tool Use: Calling APIs Safely With Timeouts, Retries and Fallbacks
AI agent tool use connects a model to your code in a five-step loop. This guide covers OpenAI and Anthropic mechanics, plus timeouts, retries and fallbacks.
By Galactic News Editorial · Published 2026-11-23T10:41:23+00:00 · Updated 2026-11-23T10:41:23+00:00
AI Agent Tool Use: Calling APIs Safely With Timeouts, Retries and Fallbacks
An AI agent that cannot call tools cannot do much. It can chat, but it cannot check an order, book a meeting or update a record. Tool use is the mechanism that connects a model to your code. The model reads a list of tools, picks one, and returns a structured request. Your code runs it and sends the result back. The model never executes anything itself. That split is the whole safety story. This guide covers how the loop works in the OpenAI and Anthropic APIs, and how to make it safe with timeouts, retries and fallbacks.
AI agent tool use API calling patterns: how the loop works
The flow has five steps, as OpenAI's own documentation describes it. First, the application sends a request to the model with the tools it could call. Second, the model returns a tool call. Third, the application executes code with the input from that call. Fourth, the application sends a second request with the tool output. Fifth, the model returns a final response, or more tool calls. The loop repeats until the model answers in plain text or the code stops it. OpenAI calls this function calling. Anthropic calls it tool use. The shape differs slightly. In OpenAI's Responses API, a function call arrives as an item of type function_call with a call_id, a name and arguments as a JSON string. The result goes back as a function_call_output item with the same call_id. In Anthropic's Messages API, a tool request arrives as a tool_use content block inside the response, with the arguments already parsed as a dictionary. The result goes back as a tool_result block inside a user message, since Anthropic has no tool role. Both APIs also offer built-in tools, such as web search and code execution, alongside the functions you define. The model decides when a tool is needed. Your code decides what actually happens.
Designing tools the model can use safely
A tool is a contract. It has a name, a description of when to use it, and a JSON schema for its inputs. The description matters more than most developers expect. The model reads it to decide whether to call the tool. Write descriptions that state the purpose, the required inputs and the limits. Name tools as verbs with clear nouns, such as get_order_status or cancel_booking. Keep the schema tight. Every extra optional field is a chance for the model to invent a value. Use strict mode where the API offers it. OpenAI's strict option enforces the schema on the arguments the model returns. That removes a whole class of parsing errors. Apply least privilege to every tool. A read-only lookup tool should not be able to write. A tool that sends email should only send to addresses the user confirmed. A tool that moves money should need a separate confirmation step outside the model loop. Never expose a tool that runs raw SQL, shell commands or arbitrary code with model-chosen input. If the agent needs that power, put a human approval step in front of it.
Timeouts: stop a slow call from stalling the agent
Every tool call needs a timeout. External APIs hang. Databases lock. Third-party services go slow at the worst moment. Without a timeout, one stuck call freezes the whole agent run and burns tokens while it waits. Set a per-call timeout that fits the tool. A fast lookup gets a few seconds. A slow report gets a minute. Enforce the timeout in your code, not in the model prompt. The model cannot enforce time. When a call times out, treat it as a failed call with a clear error. Tell the model the tool timed out and let it decide what to do next. It may retry, try another tool, or tell the user it could not complete the step. Also set a global deadline for the whole agent run, and a maximum number of tool iterations. A confused model can request tools indefinitely. OpenAI's and Anthropic's documentation both assume the application caps the loop. Pick a cap, such as 10 or 25 turns, and stop cleanly when it is hit. Log every timeout with the tool name and the elapsed time. Patterns show up fast in logs.
Retries and idempotency: making a second try safe
Retries are only safe when the call can run twice without harm. A lookup can run twice. A payment cannot. The fix is idempotency. Give each logical action a unique key, and send that key with every attempt. The receiving service uses the key to recognise a repeat and return the first result instead of doing the work twice. Stripe, many payment APIs and most well-built internal services support this pattern. Retry only on transient errors, such as rate limits, timeouts and brief server errors. Do not retry on validation errors or permission errors. Those will fail the same way every time. Use backoff between attempts. A short wait, then a longer one, keeps a struggling service from being hammered. Cap the retries at two or three. After that, fail the step and fall back. For read calls, a retry is cheap. For write calls, check first whether the first attempt actually landed. A timeout does not mean the call failed. It means the answer never arrived. The write may have gone through. Query the current state before retrying a write, or use the idempotency key to make the retry safe.
Fallbacks: what the agent does when a tool fails
A fallback is a planned second path. The agent should never stare at a failed tool and give up without trying something useful. The simplest fallback is a degraded answer. If the order lookup fails, the agent can say it could not reach the order system and offer the order number and a support link. A better fallback is an alternative tool. If the fast search API is down, try the slower one. If the live inventory check fails, show the last cached stock level and label it as cached. For critical actions, the fallback is a human. The agent drafts the request and hands it to a person with the full context attached. Build fallbacks into the tool design, not into prompts. A prompt that says "try your best" is not a fallback. Code that catches a specific error and runs a second path is. Test the failure paths. Turn off the network for one tool in staging and watch what the agent does. If it loops, hallucinates a result or repeats the same failing call, the fallback is missing.
OpenAI and Anthropic tool use compared
| Aspect | OpenAI function calling | Anthropic tool use |
|---|---|---|
| Main API | Responses API, with Chat Completions still common | Messages API |
| Tool definition | Function object with name, description, parameters JSON schema, strict flag | Tool object with name, description, input_schema |
| Call format | function_call item with call_id, name and arguments as a JSON string | tool_use content block with parsed input dictionary |
| Result format | function_call_output item with matching call_id | tool_result block inside a user message |
| Parallel calls | Multiple function calls in one response | Multiple tool_use blocks in one response |
| Loop control | Application repeats until text answer or iteration cap | Application repeats while stop_reason is tool_use, up to a cap |
Bottom line: the two APIs differ in message shape, not in principle. The same safety patterns apply to both. Define tight tools, enforce timeouts, retry only idempotent calls, and always have a fallback.
Building a safe tool loop in five steps
- List the tools and their risk. Mark each one read or write. Writes need confirmation, idempotency or both.
- Write strict schemas. Name every field, set the types, mark the required ones, and turn on strict mode where the API offers it.
- Wrap every call in a timeout. Give fast tools seconds and slow tools a minute. Log the elapsed time on every call.
- Add retries only where safe. Use idempotency keys for writes. Retry transient errors with backoff, at most two or three times.
- Code the fallback for each tool. Cached data, an alternative tool, a degraded answer or a human handoff. Test each one by breaking the tool in staging.
Bottom line
Safe agent tool use is mostly ordinary engineering. The model picks the tool and supplies the arguments. Your code runs it, times it, retries it when safe, and handles failure with a plan. Keep tool schemas tight and descriptions honest. Give every call a timeout and every run an iteration cap. Make writes idempotent so a retry cannot double-charge or double-book. And always have a fallback, whether that is cached data, a second tool or a human. An agent that fails gracefully is far more useful than one that fails fast and silently.