Safe retries for AI tool calls
A timeout tells an agent it did not get a reply. The service may already have written the record. If the agent tries again with a new request ID, the same intent can create a second record.
Keep the request ID with the original intent
I generate a request ID before a write and keep it with the pending action across interruptions. The service scopes it to the caller and operation, associates it with the original inputs and records the result. A retry repeats the same ID and inputs. If the same ID comes back with different inputs, the service returns a conflict.
The write and its deduplication record need a consistent failure boundary. For example, a database transaction can commit them together. If the action calls an external service, I check what idempotency and recovery guarantees that service provides before promising safe retries.
Test the reply getting lost after a successful write
- Send a request with a known ID and confirm the write reached the database.
- Drop the response before the caller receives it.
- Send the same request again with the same ID.
- Check the returned record identifier and verify there is exactly one committed record.
I also send two requests with the same ID concurrently and check for one committed effect. In a separate test I reuse the ID with different inputs and check that the service reports the conflict without changing the stored result.
Reconcile after the deduplication window
Deduplication history has a retention limit. If an agent resumes after that window, I use a durable business identifier, when available, to check whether the effect already exists. If the state is ambiguous, I leave the action pending for review.
Amazon’s Builders’ Library covers client request identifiers, mismatched parameters and late retries.