Your AI Agent Timed Out. Did It Start the Job Twice?
A practical AWS design for retrying agent tool calls with Lambda, DynamoDB, and stable operation IDs, plus five failure checks to try.
The tool call timed out. The job may already exist.
Picture an agent that turns a document into a report. It calls a tool that starts a background job. The job is recorded, but the response times out on the way back. The agent tries again. Now there are two workers, two model calls, and potentially two copies of the report.
The question I’d ask before adding a retry policy: how does the tool know the second request is the same operation?
Give each operation a stable ID
Create an operation ID when the user confirms the action. Pass it into the tool and reuse it across retries. A deliberate second run gets a new ID. Keep this decision in your application so it survives a new model turn or a process restart.
For a small AWS prototype, I’d use an AWS Lambda tool handler and an Amazon DynamoDB table. The handler would attempt a conditional write using
attribute_not_exists(pk) . The item could contain the operation ID, the tenant ID, a fingerprint of the inputs, the current status, and eventually a result reference.When a request arrives with an existing ID, the handler looks up the operation:
- Accepted or running: return the same job ID and its status.
- Succeeded: return the saved result reference.
- Same ID, different inputs: reject the request and surface the mismatch.
- Failed: return the failure state and let an explicit recovery policy decide the next step.
Scope the lookup to the authenticated tenant. Keep records for at least as long as old retries can arrive.
Check what happens between the write and the work
A conditional write can prevent duplicate records. There’s still a gap if the handler writes the item and crashes before submitting the job. One design is to have a DynamoDB Streams consumer start the work from newly inserted job records, with a way to detect jobs that stay in the accepted state too long.
The worker also needs to handle redelivery. For a report stored in Amazon S3, one option is to use an operation-specific object key and a conditional create. For a downstream API with side effects, pass the same operation ID as an idempotency key if the API supports one. If the downstream result is ambiguous and there’s no safe way to check it, surface that ambiguity for recovery before trying again.
Run the demo with the failure turned on
- Start a job and force a timeout after the DynamoDB write succeeds.
- Retry the same operation ID from two callers at once.
- Verify that both callers get the same job ID.
- Deliver the worker event twice and check how many outputs are committed.
- Reuse the ID with different inputs and verify that the tool rejects it.
Log the operation ID alongside the invocation ID in CloudWatch. The invocation ID tells you which attempt ran. The operation ID lets you put the whole sequence back together when you’re debugging.
If you could add just one failure case to your agent’s demo, which would you pick: a lost response, a duplicate worker event, or a crash halfway through writing the result?
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article