---
title: "Chapter 19. The Durable Agent"
url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch19"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18T12:24:57.879Z"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
access: "lead-in"
purchase: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch19"
    markdown: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch19.md"
  - locale: "en"
    url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch19"
    markdown: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch19.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch19"
    markdown: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch19.md"
---

# Chapter 19. The Durable Agent

*[JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)*

The agent from the previous chapter lived inside one process. While it works for a few seconds, that is enough. But the tasks agents are built for take longer: sort through a hundred customer requests, move data between systems, carry out a refund that requires a human’s signature. Such work goes on for hours and days, and in that time the machine under it will be restarted at least once. If the agent’s whole conversation with the model lives in memory, it will vanish after a restart, and the agent will either start over or do something twice.

In the twelfth chapter we saw how Temporal solves this problem for ordinary processes. The code of a process is written as an asynchronous function, and every step that touches the outside world is made an activity whose result is recorded in a log. After a crash the function is run again, recorded activities are not executed again, and the function proceeds to the first step that is not yet in the log. It works under one condition: the code between activities must behave the same way on every run. An agent violates this condition by its very nature, because a model can answer the same question differently.

* * *

The way out of this contradiction is to move the model call into an activity. Then the agent loop from the previous chapter becomes the process code, and every model call and every tool call becomes an activity whose result lies in the log. On replay the function gets from the log the same model answers as the first time and goes down the same road. The model itself is nondeterministic, but its answers are recorded, and everything that happens between them is deterministic.

The experiment in `experiments/17-durable-agent` shows both versions. The «model» in it chooses at random in what order to issue a refund and send an email; the process crashes after the first action and restarts. If the model is called directly from the process code, on restart it may choose a different order, and in eighteen runs out of forty the code asked for a step other than the one in the log and stopped with a divergence message. If the model is called through an activity, there are no divergences at all. In the twelfth chapter a branch was chosen through `Math.random`, and Temporal hid that randomness in a sandbox. A model’s randomness cannot be hidden like that; it can only be recorded.

---

The rest of this chapter is free on the site once the reader confirms an email address: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch19
