Make the agent remember across a restart
By default a model is stateless: each turn starts cold and forgets the last. To build an agent that remembers, you give it durable state — a place to write facts down and read them back later. But "later" hides the hard part. Memory inside the running process vanishes the moment the process restarts, the request ends, or the agent is rescheduled onto a different machine. Durable means the state outlives all of that.
The move has two halves. Persist: before the lights go out, serialize your
state into something a store can hold — JSON.stringify(state) turns the live
object into a plain string you could write to a database, a file, or a Durable
Object. Restore: when a fresh, empty process boots, rehydrate it by reading
that string back — JSON.parse(saved) reconstructs the object. Skip the persist
and there's nothing to read; skip the restore and the fresh state stays empty.
Either gap and the memory is gone.
Watch the agent save a fact on one turn and recall it on a later turn instead of guessing:
Now build the mechanism that makes that possible. Below, the state is assembled
from a few events and the print lines are wired — but the persist and restore
steps are left as TODOs, so the "restarted" state comes up empty. Serialize the
state to saved, then rehydrate restored from it, until the order and note
survive the restart and it prints restored after restart: 2 items.
The memory isn't in the model, and it isn't in the running process — it's in the bytes you wrote down. Persist before the restart, restore after it, and the agent remembers.