Lesson 1 of 8 · 3 min read
Durable Execution
In this lesson
TL;DR: Write down the result of every step that touches the outside world before moving on, and rebuild a crashed run by replaying that record. Model calls, tool calls and timers are activities: they run, their results are stored, and on replay the stored result is returned instead of running again. The code that decides what to do next must replay the same way every time, so anything non-deterministic (time, randomness, model output) comes through recorded activities. Activities can still run twice around a crash, so every side effect needs an idempotency key.
After this lesson, you should be able to explain event history and replay, decide what runs as an activity, describe the determinism rule, and explain why idempotency is still required.
Before you read
Commit to an answer now. The lesson that follows explains it, and having guessed first, even wrongly, makes the explanation easier to remember.
1An agent processes an expense claim in six steps. The server restarts after step four, during a deploy. With no durability, what happens when the run is retried, and what would you want instead?
History and replay
- 01userProcess expense claim EC-2291 (₹18,400, travel).
- 02toolreceipts_extract(claim=EC-2291)
- 03result4 receipts · total ₹18,400 · recorded in historyok
- 04toolapproval_request(approver=finance-lead)
Steps are recorded as they happen
Each model call and tool call is an activity. Its result goes into the history before the run moves on.
Durable execution engines (Temporal is the best-known example; several agent frameworks and cloud workflow services offer comparable persistence, as of 2026) work this way:
- The workflow is ordinary code describing the steps and decisions.
- Each activity (anything that talks to the outside world) is run by the engine, and its result is appended to an event history.
- After a crash, a new worker runs the workflow code again from the start, but every activity that already completed returns its recorded result instantly. The code arrives at the same point with the same state and continues.
A tiny engine
class Run:
def __init__(self, history=None):
self.history = history if history is not None else []
self.pos = 0
self.executed = []
def activity(self, name, fn, *args):
if self.pos < len(self.history): # replaying: use the record
recorded = self.history[self.pos]
self.pos += 1
return recorded["result"]
result = fn(*args) # first time: run and record
self.history.append({"name": name, "result": result})
self.executed.append(name)
self.pos += 1
return result
def workflow(run):
receipts = run.activity("extract", lambda: 18400)
run.activity("request_approval", lambda: "sent")
return run.activity("summarise", lambda r: f"claim total {r}", receipts)
first = Run()
try:
first.activity("extract", lambda: 18400)
first.activity("request_approval", lambda: "sent")
raise RuntimeError("worker restarted")
except RuntimeError:
pass
second = Run(history=first.history) # a new worker, same history
print(workflow(second), second.executed) # => claim total 18400 ['summarise']
The second worker replays two recorded activities without running them and executes only the step that had not happened. That is the whole idea; production engines add persistence, timers, signals and versioning.
The determinism rule
Replay works only if the workflow code makes the same decisions given the same recorded results. So inside workflow code:
- no reading the clock, generating random numbers or calling the network directly;
- no calling the model directly: a model call is non-deterministic, so it is an activity and its output is recorded;
- branching only on values that came from recorded activities.
Breaking the rule makes the replayed run take a different path from the original, which engines detect as a non-determinism error.
Activities still run at least once
If a worker crashes after an activity's side effect happened but before its result was recorded, the activity runs again on recovery. Durable execution narrows the window but cannot close it, so every activity with side effects needs an idempotency key (see Idempotent Side Effects), and the next lesson covers what to do when the outside system offers none.
Say it in 30 seconds
"Durable execution records each step that touches the outside world, model calls, tool calls and timers, as an activity whose result goes into an event history, and recovers a crashed run by replaying the workflow code against that history, returning recorded results instead of redoing work. Workflow code must be deterministic, so time, randomness and model output come through recorded activities. Activities still run at least once around a crash, so side effects need idempotency keys."
Check yourself
Answer aloud or on paper before revealing. Producing the explanation unaided is exactly what a panel listens for.
1Why must a model call be an activity rather than called directly from workflow code?
2With durable execution, can a side effect still happen twice?
3What happens if workflow code reads the current time directly to decide a branch?