← All posts

Agent Orchestration · 8 min read

Durable execution for long-running agents: checkpoints, retries, idempotent tools and resuming after failure

An agent that runs for an hour, or waits days for a human, will eventually hit a crash, a deploy or a rate limit halfway through. Durable execution lets it pick up where it left off instead of starting again. Here's how it works, what's on offer, and the rules your agent code has to follow.

Subhash Nunna ·

A chat turn that takes ten seconds can afford to fail. If something breaks, the user presses retry. An agent that spends forty minutes researching, calls twenty tools, and then waits two days for a manager to approve a refund is different. Somewhere in that run there will be a deploy, a crashed container, a model API returning 429s. If the agent starts again from zero each time, you pay for every model call twice, and worse, you may send the same email or issue the same refund twice.

Durable execution fixes this. Here’s what it is, how the main options implement it, and the rules your code has to follow. (For the bigger picture of how agents are wired together, see our post on agent orchestration patterns.)

What durable execution means

The short version: the runtime records every step your agent completes, and if the process dies, a new process picks up the run, skips what’s already done and continues from the step that was in flight.

The Pydantic AI docs put it well. A durable engine “journals the agent’s model requests and the tool calls it wraps, and a restarted run replays the completed ones from the journal” before retrying the one that was in flight. They recommend it “when a run in flight has to survive its process stopping, through a deploy, a crash or a transient API failure”, and for when “a workflow waits hours or days for a person or another system.”

AWS describes its version, Lambda durable functions, in nearly the same terms: a durable execution “uses checkpoints to track progress and automatically recover from failures through replay.” During replay, “your code runs from the beginning but skips over completed checkpoints, using stored results instead of re-executing completed operations.”

That sentence is the key to everything below: your code really does run again from the top, and the recorded results make that fast and safe.

Checkpointing state vs durable execution

These two ideas get mixed up, and the difference matters.

State checkpointing Durable execution
What’s saved The agent’s state after each step The result of every step, plus the run’s progress
Who resumes You, by loading the last checkpoint The engine, automatically
Crash mid-step That step’s work is lost; you decide what to do The in-flight step is retried; completed ones are skipped
Waiting days You build it (queue, cron, state table) Built in: the run suspends and wakes on a signal or timer
Example LangGraph checkpointers Temporal, Restate, DBOS, Lambda durable functions

LangGraph’s persistence layer is a good example of the first column. Its docs say “Checkpointers persist a thread’s graph state as checkpoints,” keyed by a thread_id, and list “human-in-the-loop workflows, time travel, and fault tolerance” among the uses. That’s very useful, but notice the warning in the same docs: the in-memory savers “store checkpoints in RAM. When the process restarts, all checkpoints are lost.” For anything long-running you need a database-backed checkpointer such as PostgresSaver, and something has to notice the crash and resume the thread.

Pydantic AI draws the same line. It offers step persistence that “snapshots every settled step to a store you choose, the way a checkpointer does,” and separately says a durable engine “keeps one run alive when it crashes partway through a step.”

Checkpoints are enough for a chat agent that resumes when the user returns. Durable execution earns its keep when nobody is around to press resume.

What’s on offer right now

The space has moved quickly in the past year.

  • Temporal + OpenAI Agents SDK. Temporal announced the integration in July 2025, and a note on that post says it became generally available in March 2026. In it, “every agent invocation is executed through a Temporal Activity,” and Temporal “stores all Activity results, and that’s how it can simply keep going.” Temporal’s cookbook shows tools wrapped with activity_as_tool, each with its own start_to_close_timeout, and the model call given a separate 30-second timeout.
  • AWS Lambda durable functions. Ordinary Lambda functions using a “checkpoint/replay mechanism,” with SDKs for JavaScript, TypeScript, Python, Java, Go and C#. AWS says executions can run “for up to one year,” and that “waits suspend execution without incurring compute charges.” A June 2026 AWS post builds a multi-agent workflow on it with context.step() for each agent and context.waitForCallback() for a physician’s review, with a seven-day timeout.
  • Pydantic AI. Rather than being an engine, it plugs into many: the docs list Temporal, DBOS, Prefect, Restate, AWS Lambda, Absurd, pgtask, Azure Durable Task and Apache Airflow.

If you already run one of these engines, use it. If not, start from where your code already lives.

The rules your agent code has to follow

Because code replays from the start, it must behave the same way each time up to the crash. Four rules cover most of it.

1. Put every side effect inside a step. Model calls, tool calls, HTTP requests, database writes, reading the clock, generating a random ID: each goes inside the engine’s step or activity wrapper so its result is recorded. Anything outside a step runs again on replay. Pydantic AI’s docs make this point for external reads: “use a durable operation for external reads so replay uses the same values.”

2. Keep the orchestration code deterministic. The code between steps should only make decisions based on step results and inputs. Don’t branch on datetime.now(), environment variables that can change between deploys, or iteration order of something unordered. On replay, the code has to walk the same path to reach the same recorded steps.

3. Make tools idempotent. The engine guarantees completed steps won’t re-run. It can’t guarantee the in-flight one didn’t half-finish: the payment API may have charged the card just before the worker died. So the step will run again, and the tool must cope. The standard fix is an idempotency key that is stable across retries. The AWS post uses exactly this, adding “idempotency tokens” to stop duplicate submissions to a payer. A sketch:

# Pseudocode: the shape is the same across engines.
def issue_refund(ctx, order_id, amount):
    # Generate the key inside a recorded step, so replay returns the same key
    key = ctx.step("refund-key", lambda: f"{ctx.run_id}:{order_id}:refund")
    # The payments API de-duplicates on the key, so a retry can't refund twice
    return ctx.step("refund", lambda: payments.refund(order_id, amount,
                                                      idempotency_key=key))

If an API doesn’t support keys, check before you act (“does a refund for this order already exist?”) inside the same step.

4. Version your agent when you change it. A run started on Monday replays on Wednesday’s code if you deployed in between. Changing the prompt or the order of steps can make the replayed path diverge from the journal. Check how your engine handles code changes for in-flight runs; at minimum, let old runs finish on old code before you change step order.

Retries, timeouts and waiting for people

With a durable engine, retries become configuration rather than code. Temporal’s post sums up the appeal: “you get to code the happy path, and Temporal does the error handling for you.” A few practical settings:

  • Timeouts per step. Give each model call and tool its own timeout, as Temporal’s cookbook does, so a hung call fails fast and is retried rather than holding the run forever.
  • Backoff for polling. The AWS example polls an external system with exponential backoff from 30 seconds to 5 minutes, capped at 48 attempts. Some cap is essential; an agent retrying forever is just a slow outage.
  • Separate transient from permanent errors. Retry 429s and timeouts; fail fast on invalid input.
  • Durable waits for humans. Approval steps should suspend the run and resume on a callback or signal, with a deadline. AWS notes that its function “incurs no compute charges while the physician takes minutes, hours, or days to respond.”

Trade-offs

  • Another system to run. Temporal or Restate are services to operate (or pay for). Lambda durable functions and Postgres-backed engines reduce that, but you’re still adopting a new execution model.
  • Discipline in the code. Determinism and step wrapping are easy to break by accident, especially when someone adds a quick logging call that reads the clock. Code review needs to know the rules.
  • Journal size. Recording every model response and tool output adds up for long agents with large contexts. LangGraph’s docs advise you to “prune old checkpoints periodically or set a retention policy,” and the same applies to any journal.
  • Not every agent needs it. Value grows with run length, cost per run and how bad a duplicated action would be.

Where to start

  1. List your agent’s side effects. Every tool that writes, sends, pays or deploys. Those are the ones that must be idempotent, durable engine or not.
  2. Add idempotency keys to those tools first. This pays off even before you adopt an engine, because ordinary retries already cause duplicates.
  3. Persist state with a real database. If you’re on LangGraph, swap the in-memory saver for a Postgres checkpointer and practise resuming a thread after killing the process.
  4. Pick one long-running workflow, ideally one with a human approval in it, and move it onto a durable engine that fits your stack.
  5. Test the failure path on purpose. Kill the worker mid-run, deploy during a run, force a 429. Check that the run finishes, nothing happened twice, and the bill shows completed model calls weren’t repeated.

durable executionAI agentsagent orchestrationreliabilityidempotency