AI Engineering
When an AI tool call times out: check the idempotency contract before retrying
A missing response can hide a completed action. Seven local tests explore retry keys, changed intent, tenant scope, and the limits of an in-memory ledger.
The question a missing response leaves behind
Imagine an agent creates an issue, then loses the connection before receiving the response. The interface reports an error, but the issue may already exist. Sending the same content again can create a duplicate. A prompt asking the model to retry carefully cannot resolve this uncertainty by itself.
This is a practical execution-layer note, not a new-product announcement. A small local test demonstrates the difference between a failed action and an unknown outcome without calling any external service.
Separate an attempt from an intention
The AWS Builders’ Library: aws.amazon.com — making-retries-safe-with-idempotent-APIs describes identifying retries with a caller-provided request identifier. Identical parameters alone cannot distinguish an accidental duplicate from two intentionally identical operations.
For an agent runner, start with one operation identifier per user intention. Keep it across network retries and model reinvocations for that operation. A genuinely new request needs a new identifier. Changed content or a changed destination under the old identifier should be treated as an intent mismatch, not silently substituted.
Read the provider's contract
An idempotency key does not imply identical behavior across APIs. Stripe's documentation: docs.stripe.com — idempotent_requests says API v1 stores the status and body once execution begins, including 500 responses. Reusing a key after it has been removed can create a new request. The documentation describes different retry behavior for API v2, so do not generalize the v1 contract to every version.
Record the API version, key scope, retention period, and result-lookup method alongside the runner configuration. Use an opaque identifier rather than an email address or customer name.
Lose the response deliberately
Save the example file from: idempotency — retry-contract.test.mjs
The accompanying retry-contract.test.mjs runs entirely in memory. It records an effect, then throws an exception to simulate a completed operation whose response never reached the caller. Repeating the same key recovers the original receipt. Changing the key creates a second effect.
Run it with node --test retry-contract.test.mjs. Its seven checks cover:
1. A lost response can follow a committed effect.
2. The same key and content recover the previous result.
3. A fresh key on every retry can create duplicates.
4. Changed content under an existing key is rejected.
5. Two intentionally identical operations use different keys.
6. Identifiers are scoped by tenant.
7. Restarting the process loses the in-memory ledger.
The last check exposes a deliberate limitation. This is not a production deduplication implementation. It does not implement concurrent execution, durable storage, transactions, atomic coordination with an external system, or key expiry. Production integrations need separate tests of their provider contract and recovery procedure.
All seven tests passed on Node v26.7.0. This is a synthetic contract check with no external API or model calls.
node --test retry-contract.test.mjs Keep an unknown result visible
Use distinct states for success, confirmed failure, and unknown outcome. For an unknown outcome, inspect a receipt or operation-status endpoint first. If neither safe lookup nor an idempotency contract is available, escalate for verification instead of blindly resubmitting. Bound retry counts and waiting time as well.
An idempotency key also provides no authorization. If the approved destination or content changes, the previous approval may no longer cover the action. Retry design should reliably finish the one operation that was authorized.