Durable execution is the missing runtime for AI agents
At Mixmax, we prototyped an agent that runs once per contact in an outreach campaign. It reads what we know, picks an action, and waits for something to happen. A reply might arrive in an hour. A timer might fire five days later. A person might approve or reject a draft. The loop continues for two or three weeks.
A Lambda invocation lasts 15 minutes. This workflow lasts weeks. We first built the missing pieces ourselves, then built the same lifecycle four more ways to see what we were really signing up for.
The version we owned
The first implementation used SQS for incoming events, MongoDB for state, and EventBridge Scheduler for timers. Each contact had a document recording its position in the journey.
The hard case was simple to describe: wait for a reply or a timeout, whichever happens first. The reply handler and timer handler can arrive together, so they both used findOneAndUpdate with a status condition to claim the contact. The losing handler stopped. The winner had to cancel the other schedule. That race and its cleanup took around 160 lines.
Retries with backoff took 109 lines. A collection for messages that exhausted retries, plus a job to return them to the flow, took about 140. A sweep for contacts stuck because an event or schedule never arrived took another 160.
The implementation was 897 lines in eight files. It was not bad code. It was workflow-engine code that the feature team would own for as long as the feature existed.
Five implementations
We rebuilt the same lifecycle as small prototypes.
| Implementation | Lines | Files |
|---|---|---|
| AWS Lambda durable functions | 244 | 1 |
| Inngest | 251 | 3 |
| Restate | 330 | 2 |
| Step Functions | 630 | 7 |
| Hand-built SQS, MongoDB, EventBridge Scheduler | 897 | 8 |
Inngest and Restate host long-running workflows. Step Functions is AWS's state-machine service. We chose AWS Lambda durable functions. Its programming model is an ordinary async handler, with meaningful operations wrapped in context.step(...). The runtime records completed steps and can resume the handler after a wait or crash.
The event-or-timeout case became this:
const outcome = await context.waitForCallback(
`await-reply-${invocationCount}`,
async (callbackId) => {
await saveCallbackId({ contactId, callbackId });
},
{ timeoutSeconds: 5 * secondsPerDay }
);
The line count was a useful warning, not the reason to choose it. A mature SDK will always look leaner than application code written last month. The deciding property was replay.
Replay gives an agent a stable decision
A durable runtime replays the handler from the beginning after a suspension or crash. It reads the execution journal, returns the recorded result for completed steps, and executes only steps it has not seen. Its rule is strict: anything that could have a different answer on another run must be recorded, not recalculated.
That rule exists for time, random values, and network calls. It also describes a model call perfectly. Ask a model twice and you can get two different decisions. If the first decision already drafted or sent a message, the second call is not a retry. It is a new judgment about a contact who has already been acted on.
So our loop made the model call, recorded its result under contactId#campaignId#invocationCount, then executed that result:
const decision = await context.step(`decide-${invocationCount}`, () => agent.decide(contactId));
await context.step(`record-${invocationCount}`, () =>
recordDecision(contactId, campaignId, invocationCount, decision)
);
await context.step(`execute-${invocationCount}`, () => executeDecision(decision));
If execution dies during the final step, replay returns the original decision from the journal. The model is not asked again and the draft is not regenerated. In this setting, exactly once means the agent decides once. The later attempts work from that decision.
The workflow only computes for about 30 seconds across three weeks. The rest is waiting. While it waits, there is no running process, only a checkpoint in AWS.
One callback ID is enough
The sleeping execution and its wake-up source share one value: a callback ID. The workflow stores that string next to the contact. A separate Lambda, with another trigger and IAM role, can call SendDurableExecutionCallbackSuccess(callbackId, payload) without knowing which step is paused or which campaign the contact belongs to.
Human approval uses the same mechanism. So does pausing a campaign, modelled as a callback wait with a 365-day timeout.
Duplicate messages are normal. A consumed callback produces InvalidCallbackException or ResourceNotFoundException. Both mean the callback already won. Delete the stored lookup row and move on. Logging those as failures would create alarms for normal duplicate delivery.
The parts that were easy to miss
Step names must be unique in an execution and stable across replays. A repeated read-contact name inside a loop returns the first iteration's cached contact every time. A name based on Date.now() or crypto.randomUUID() changes on replay and runs the step again. The invocation counter is stable and unique.
await context.step('read-contact', ...); // stale after iteration 1
await context.step(`read-contact-${Date.now()}`, ...); // runs again on replay
await context.step(`read-contact-${invocationCount}`, ...); // correct
Step results are serialized. A Mongo ObjectId that works on first execution can return as a plain JSON object on replay, without toHexString(). We fixed one such failure by carrying string IDs in the workflow input rather than rich objects.
I also blamed the local time-skipping runner for a repeated timeout failure. The real bug was our error check. In version 1 of the SDK, a callback timeout arrived as ChildContextError, with the expected CallbackError on .cause. instanceof CallbackError failed and our catch block rethrew. The runner was right. The issue is aws/aws-durable-execution-sdk-js#420. Version 2 exposes a catchable CallbackTimeoutError; in version 1, the unpleasant workaround is checking err.message === 'Callback timed out'.
Checkpoint size also changes the design. Each checkpoint is limited to 256 KB. Agent context can consume that quickly, so we build context inside a step and journal only the decision. Pass IDs between steps, not prompts or documents. The runtime allows 3,000 operations per execution. At roughly six per loop, we have room for about 500 iterations against an expected 5 to 15. An execution can last around a year.
Time is injected as secondsPerDay: 86,400 in production and 1 in tests. The workflow has no test-only branch. The local runner exercises checkpoint and replay behavior in process, without cloud credentials, so a three-week journey becomes a sub-second unit test.
Keeping the fallback was still worth it
The 897-line version was not wasted. The SDK was four months old when we chose it. A young dependency under a workflow that can decide what a person receives deserves an exit plan.
The working version on SQS, MongoDB, and EventBridge gives us one. We know what replacing the runtime costs because we already built it. More importantly, the runtime's replay rule enforces the exact idempotency rule the agent needs. In the home-grown system, we had to remember that rule in every handler. In the durable version, the execution journal remembers it for us.