All posts

Durable execution for AI agents: how to keep a long-running agent alive through crashes and deploys

Durable execution lets a long-running AI agent resume after a crash, deploy or timeout instead of starting over. How checkpointing and workflow engines differ, and when you need either.

CodeKrypt 5 min read

An agent that triages a support backlog, researches a lead or reconciles a month of invoices can run for many minutes or hours. Then a deploy rolls the server, a worker is evicted, or a provider times out at step 31 of 40, and the whole run starts again, paying for every model call twice. Durable execution for AI agents is the pattern that prevents that: record progress as you go, and let any worker resume from the last recorded step.

This post is about the domain, not any particular deployment of ours. Facts come from public docs, linked inline.

Why do long-running AI agents lose work?

A request handler lives and dies with one process. Anything held in memory, such as the message list, the tool results gathered so far and the loop counter, disappears with it. That is tolerable for a two-second chat reply. It is expensive for a run that has already spent minutes and tokens.

The usual triggers are mundane:

Our post on why AI agents fail in production lists lost state among the architectural causes. This one is the fix for it.

What does durable execution actually mean?

Temporal's documentation describes it as running your code to completion across crashes and outages. When a worker dies, the service hands the work to another worker, which replays the recorded event history and resumes at the line where execution stopped, with local variables and progress intact. A workflow can keep running "whether that takes seconds or months".

The agent loop maps onto this directly: each model call or tool call is a recorded step, and a finished step is not executed again on resume.

Agent run without and with durable execution: a crash at step 3 restarts from step 1 versus resumes at step 3Without durability: crash restarts the runStep 1Step 2Crashthen Step 1 and Step 2 run (and bill) againWith durability: resume from the recordStep 1Step 2Crashnew worker continues at Step 3

Checkpointer or workflow engine: which one do you need?

The two common approaches solve overlapping but different problems.

Checkpointing saves the agent's state after steps. LangGraph's persistence layer saves graph state as checkpoints per thread, and lists conversation continuity, human-in-the-loop, time travel and fault tolerance as uses. Its docs warn that in-memory savers lose data on restart, so production needs a persistent one such as the Postgres saver, and that checkpoints accumulate over long conversations, raising latency and storage cost unless you prune them.

A workflow engine such as Temporal owns the orchestration: timeouts, retries with exponential backoff and heartbeats are configuration, and the engine, not your app, decides when to resume.

In-memory loopCheckpointerWorkflow engine
Survives a crashNoYes, if the store is persistentYes
Survives a deployNoYes, you resume the threadYes
Waits days for a humanNoPossible, you build the triggerBuilt for it
Retry and timeout policyYou write itYou write itConfiguration
Extra infrastructureNoneA databaseA service and workers
Fits runs that lastSecondsMinutes to hoursHours to months

The last row is a rule of thumb, not a benchmark. Pick on the failure you cannot afford, not on the tool you have heard of most.

What are the limits of durable execution?

Three catches show up in practice.

Long runs hit history limits. Temporal's event history logs a warning at 10,240 events and terminates the workflow beyond 51,200, with further caps on Updates (2,000) and Signals (10,000). Its documented remedy is Continue-As-New, which closes the current execution and starts a new one with a fresh history. An agent that loops over thousands of tool calls needs that designed in from the start.

Resuming can repeat the step that was in flight. Durable execution guarantees progress is not lost, not that a side effect happened exactly once. If a worker dies after the email is sent but before the step is recorded, the step can run again. Pair this with idempotency keys as described in AI agent retries and idempotency.

State grows. Every recorded message and tool output is stored. Prune or summarise, as discussed in AI agent memory cost, and decide how long run records are retained, particularly if they hold personal data (see the DPDP Act post).

How should you decide before building?

Answer four questions:

  1. How long does one run last? Under a minute, plain code with a retry is probably enough.
  2. What does a restart cost? Add up model spend and any side effects already performed. If a restart is cheap, do not pay for durability.
  3. Does the run wait on people or external events? If yes for hours or days, you need state outside the process either way.
  4. Who will operate it? A workflow engine is another system to run, or buy as a managed service. A checkpointer on a database you already run is a smaller step.

A sensible path is to start with a persistent checkpointer, add idempotency keys on every side-effecting tool, and move to a workflow engine when waits, deploy-safety or retry policy become the main source of incidents.

What to do next

Take your longest-running agent flow and kill the worker midway in a staging environment. If the run restarts from zero, repeats a side effect, or silently disappears, you have found your durability gap. Fix that flow first.

Before adding more agents to a long-running flow, check whether you need them: single agent vs multi-agent AI has a checklist. If you are designing agents that must run unattended, our AI automation service covers architecture reviews and builds for exactly this class of workflow.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

What is durable execution for AI agents?
A way of running an agent so that progress is recorded as it happens. If the process crashes, is redeployed or times out, another worker picks the run up from the recorded point rather than restarting from the first step and repeating paid model calls.
Do I need Temporal to build a long-running AI agent?
Not always. A run that finishes in under a minute rarely needs it. A checkpointer such as LangGraph's may be enough for conversational state. A workflow engine earns its keep when runs last hours or days, wait on humans or external events, or must survive deploys.
Does durable execution make tool calls safe to retry?
No. Resuming can re-run the step that was in flight, so side-effecting tools still need idempotency keys.
What happens to a very long agent run in a workflow engine?
Event histories are bounded. Temporal, for example, warns at 10,240 events and terminates a workflow past 51,200, and recommends Continue-As-New to start a fresh history for long runs.

Related reading

Chat on WhatsApp