Topic 3 of 5 · 5 min read
Persisting agent state between steps
Goal: Decide which parts of an agent's state to persist after each step so a crashed job resumes without repeating side effects.
After every step, save what the agent needs to continue and the results of steps that cost money or changed the world, so a crashed job resumes after the last finished step instead of starting again. Without this, a crash on step 9 of 10 repeats steps 1 to 8, and their model calls, emails and charges.
Check yourself · 1 of 3
In an event-history engine such as Temporal, what happens to a model call that already finished when the workflow is replayed after a crash?
Why it matters
Agent jobs are long and made of expensive steps. A restart that loses progress wastes tokens and, worse, repeats side effects. Durable systems solve this by recording progress outside the process: in a database, in an event log, or in a checkpoint store.
Two models
Event history and replay (Temporal, Restate, DBOS). Your code is a workflow. Every call to something non-deterministic, such as a model call or an HTTP request, is run as a recorded activity (Restate calls it a durable step). The engine stores each activity's result in a history. After a crash it re-runs your workflow code from the top, but when the code reaches an activity that already has a result in the history, it returns the saved result without calling again. The workflow code itself must be deterministic: no random numbers, no current time, no direct network calls outside activities.
Checkpointers (LangGraph and similar frameworks). The agent is a graph of nodes. After each node, the framework saves a checkpoint of the graph state under a thread id. To resume, you start the graph with the same thread id and it continues from the last checkpoint.
What to persist
- Results of model calls and tool calls, so they are not repeated.
- The decisions the agent made, such as which tool it chose and with which arguments.
- The conversation or working memory the next step needs.
- A step marker: which step finished last.
- Not large raw blobs. Store a pointer (a file id, a URL) and keep the document in object storage. History entries have size limits and slow replay.
# LangGraph: same thread_id resumes from the last checkpoint
config = {"configurable": {"thread_id": "job-81"}}
graph.invoke(inputs, config) # crashes at node 3
graph.invoke(None, config) # continues from the saved state
Before and after
Before: the agent keeps its plan and tool results in a Python list in memory. A deploy kills the process; the next worker starts at step 1 and sends the customer a second email.
After: each tool call result is saved before the next step, and the email step reads the saved result for step 2 instead of calling again.
What goes wrong
- Non-deterministic workflow code. Calling
Math.random()orDate.now()directly in a replayed workflow makes replay diverge from history, and the engine raises an error. - Saving state before the side effect, not after. A crash between them loses the record that the action happened. Pair this with idempotency keys.
- Changing code under a running job. A new version with different step order cannot replay an old history. Engines provide versioning for this.
- Unbounded state. Pushing every full tool output into state makes each checkpoint larger and slower.
Examples
Activity wrapping a model call
// Temporal TypeScript: the model call runs as an activity and its result is recorded
const { callModel } = proxyActivities<typeof activities>({
startToCloseTimeout: "2 minutes",
});
export async function researchWorkflow(topic: string) {
const outline = await callModel(`Outline: ${topic}`);
const draft = await callModel(`Write from: ${outline}`);
return draft;
}
If the worker dies after outline finishes, replay returns the saved outline and only draft runs.
What to save for an email step
Save the recipient, subject and body the model chose, plus the provider's message id once sent. On resume the step sees the message id and skips sending.
Key points
- Persist results of model and tool calls, the agent's decisions, the working memory and a step marker after each step.
- Replay engines rerun your workflow code and return saved results for steps that already finished.
- Workflow code under replay must be deterministic; put randomness, time and network calls in activities.
- A checkpointer saves graph state after each node under a thread id, and resuming with that id continues the run.
- Store large documents outside the state and keep only a pointer in it.
Common mistakes
- Keeping progress only in process memory.
- Calling random or clock functions directly in replayed workflow code.
- Storing whole documents in the state or history.
- Deploying a reordered workflow while old jobs are still running.
Check yourself · 2 of 3
Which item should an agent job NOT store directly in its persisted state?
Check yourself · 3 of 3
A workflow function calls Date.now() to stamp a report and the engine reports a non-determinism error on replay. What is the fix?
Practice items, not counted towards your mastery.
Start here
- Temporal documentation (opens in a new tab)DocsFree
Workflows, activities, signals and timers, with the replay model explained from the source.
- Persistence (LangGraph docs) (opens in a new tab)DocsFree
How checkpointers save graph state after each step so a run can resume or wait.
- Restate documentation (opens in a new tab)DocsFree
A durable execution engine that journals each step; useful for comparing replay designs.