A timeout is a third outcome, not a failure. Resolve it by looking up a client reference before any retry, and keep the retry decision out of the agent loop.
The refund that was issued twice
Geetha's team at Belgavi's Book Store added an agent to the help desk. For a damaged-book case, the agent calls an issue_refund tool, which posts to the payment service the store has used for years. One afternoon the payment service is slow. The tool's HTTP client gives up after five seconds and returns an error to the agent. The agent, reasonably, tries again. Both refunds go through: the first request had reached the payment service and was still being processed when the client timed out.
Nothing in that chain is new. Clients have timed out on slow servers since there were clients. What changes with agents is who decides to retry. A model that sees "request failed" will often try again, perhaps with slightly different arguments, and it has no way to know whether the first call took effect. This guide treats that situation as an explicit third outcome, shows how to resolve it deterministically before any retry, and keeps the agent out of the decision.
Three outcomes, not two
A call to a service that changes something ends in one of three states:
- Known success. The service returned a success response; the effect happened.
- Known failure. The service returned an error that says the effect did not happen, such as a validation error or a 409 for an amount above the order total.
- Unknown. The caller does not know. The request may never have arrived, may have arrived and failed, or may have succeeded with the response lost. Timeouts, connection resets after the request was sent, a 502 or 504 from a proxy, and a crash of the caller between sending and recording the reply all land here.
The third state is the one most tool layers collapse into "failure". The AWS Builders' Library article on retries and idempotent APIs describes the core problem plainly: a client that does not receive a response cannot distinguish a failure from a lost response, so a retry of a non-idempotent call can apply the effect twice. The standard remedy is an idempotency key, and the next sections build on it, but many existing services have no such key. The design below works either way.
What stays deterministic
The decision about what to do after an unknown outcome is not a judgement call, and it should not be made by the model. It is a small state machine owned by the tool layer:
| Outcome of the call | Downstream supports idempotency keys | Next step |
|---|---|---|
| Known success | either | record the effect, return success |
| Known failure | either | return the failure with its reason |
| Unknown | yes | retry with the same key and the same arguments |
| Unknown | no, but supports lookup by a client reference | look up the reference, then decide |
| Unknown | neither | stop, mark the step as needing a person, do not retry |
The agent sees only the final result: success with the effect's id, a definite failure, or "pending reconciliation, a person will confirm". It never sees a bare timeout, and it never gets a tool it could call to "try the refund again".
The reference the client chooses
Both remedies depend on one value: a reference chosen by the caller before the first attempt, derived from the business intent rather than from the attempt. For the refund, that is the case id plus the refund purpose, for example CASE-1042:refund:damaged-cover. It must be the same on every retry, and it must not include a timestamp, an attempt number or anything the model generated.
If the payment service supports an idempotency key, the reference is the key. Stripe's API documentation describes the behaviour such services offer: for its v1 API, Stripe saves the status code and body of the first request made with a key once the endpoint starts executing, returns that saved response to retries with the same key, including 500 errors, and rejects a reuse of the key with different parameters. The IETF HTTPAPI working group's draft for an Idempotency-Key request header standardises the same idea for HTTP APIs, and it is still a draft.
If the service has no idempotency key but lets the caller attach a reference to the payment and search by it, which many older payment and order systems do as a "merchant reference" or "external id", the reference makes the lookup possible.
Reconcile before any retry
The resolver runs inside the tool, not in the agent loop:
import enum
class Outcome(enum.Enum):
SUCCEEDED = "succeeded"
FAILED = "failed"
UNKNOWN = "unknown"
def issue_refund(client, case_id: str, purpose: str, amount_cents: int) -> dict:
ref = f"{case_id}:refund:{purpose}" # same on every attempt
outcome, body = client.post_refund(ref, amount_cents, timeout_s=5)
if outcome is Outcome.UNKNOWN:
found = client.find_refund_by_reference(ref, timeout_s=5)
if found is None:
# Not found is only safe if the service guarantees a request it accepted
# becomes searchable before it answers. Otherwise wait and look again.
return {"status": "pending_reconciliation", "reference": ref}
outcome, body = Outcome.SUCCEEDED, found
if outcome is Outcome.SUCCEEDED:
return {"status": "refunded", "refund_id": body["id"], "reference": ref}
return {"status": "rejected", "reason": body.get("error", "unknown")}Two details in that code are where most implementations go wrong.
First, "not found" is not the same as "did not happen". A lookup that runs while the original request is still being processed can miss it. A service whose writes become searchable a few seconds after they commit can miss it. Treat "not found" as a reason to retry only if the service documents that an accepted request is searchable before it returns, or after a waiting period the service documents. Otherwise keep the step pending and look again later, from a reconciliation job rather than from the agent.
Second, the lookup itself can time out. That leaves the outcome unknown, not failed. The step stays pending.
Recording the attempt
The tool should write a row before the first call, with the reference, the arguments and the state attempting, and update it after. If the caller crashes between sending the request and recording the result, the row is the evidence that a request may have gone out. A restart that finds a row in attempting must reconcile it by reference before doing anything else, exactly as after a timeout. This is the same journal that a transactional outbox or a saga log provides; if the system already has one, reuse it rather than adding a second.
Worked example
A replay of the afternoon with the resolver in place, using the companion example's fake payment service, which commits the refund and then delays its reply past the client's timeout:
| Step | Event | Recorded state | What the agent sees |
|---|---|---|---|
| 1 | Row written for CASE-1042:refund:damaged-cover | attempting | nothing yet |
| 2 | POST times out at 5 s; the service committed at 3 s | unknown | nothing yet |
| 3 | Lookup by reference finds refund R-5571 | succeeded | refunded, R-5571 |
| 4 | Agent asks to refund the same case again | succeeded (existing row) | already refunded, R-5571 |
Without the resolver, step 2 returns an error, the agent retries, and the fake service records two refunds. With it, the agent's second request at step 4 is answered from the recorded row and never reaches the payment service. The companion example runs both variants and asserts one refund in the second.
Migrating an existing integration
- Inventory the tools that change state and classify each downstream call: idempotency key supported, lookup by reference supported, or neither.
- Add the client reference to every state-changing call, derived from the business intent. For services that store an external reference, send it even if you cannot yet search by it.
- Move retry logic out of the agent loop. The model's tool should return the three-way result, and the tool layer's own retry policy should apply only to the "retry with the same key" row of the table.
- Add the attempt journal and a reconciliation job that resolves rows left in
attemptingorpending_reconciliation. - For calls with neither key nor lookup, route unknown outcomes to a person, and prioritise adding a reference to that service.
When this is unnecessary
Calls that read, and calls whose effect is naturally idempotent, such as setting a case's status to a specific value or a PUT of a whole resource, can be retried without reconciliation; RFC 9110 defines PUT and DELETE as idempotent methods for this reason, although an implementation still has to honour that. A workflow where a person confirms every state change already has a reconciliation step built in. The pattern matters for appends, transfers, sends and creates, where doing it twice means two of something.
Limits
The worked example uses a synthetic payment service in process; no real payment API was called. Lookup semantics, retention of idempotency keys and searchability delays differ per service and must be checked in each one's documentation.