Module 3 · Persistence & memory
Time-travel, replays and execution forks
5 min read
Since the checkpointer keeps every step, a thread's history is a complete film of the run. LangGraph lets you rewind it: see the state at any moment, replay the run from a past point, or fork it by changing the state to explore another scenario. This capability — time-travel — is one of the best debugging tools for agentic systems, and a valuable correction mechanism in production.
Browsing the history
config = {"configurable": {"thread_id": "t1"}}
async for snapshot in graph.aget_state_history(config):
print(snapshot.metadata["step"], snapshot.metadata["source"], snapshot.next)
Checkpoints are listed from newest to oldest. For a small write → polish graph:
2 loop () ← end of the run
1 loop ('polish',) ← after write, before polish
0 loop ('write',) ← after the input, before write
-1 input ('__start__',)
Each snapshot has its own config, which contains a checkpoint_id: the exact address of that moment in the run. The source field gives the checkpoint's origin: input (input received), loop (graph step), update (manual state change, hence also the forks described below), fork (copy of another checkpoint).
Relay's server exposes this history on GET /threads/{id}/history.
Replay
To restart the run from a past checkpoint, invoke the graph with None as input and that checkpoint's configuration:
history = [s async for s in graph.aget_state_history(config)]
before_polish = next(s for s in history if s.next == ("polish",))
result = await graph.ainvoke(None, before_polish.config)
LangGraph resumes from that state and runs the remaining nodes (polish). Earlier steps are not run again: their results are read back from the checkpoint.
Replay is used to reproduce a behavior: with a real model, the call in polish is made again, and you can observe whether it gives the same result, with a corrected prompt for example. Beware: replayed nodes have their side effects again — hence, once more, the importance of idempotency.
Fork
The real power of time-travel is changing the past. update_state creates a new checkpoint, derived from an existing one, with a modified state:
fork_config = await graph.aupdate_state(before_polish.config, {"draft": "a rewritten version"})
result = await graph.ainvoke(None, fork_config) # polish runs on the modified draft
Three important points:
- The update goes through the reducers, as if a node had returned it. For a key with an addition reducer, you append elements; to replace it entirely, use
Overwrite. - The fork lives in the same thread. It destroys nothing: the original run stays in the history. But the fork becomes the latest checkpoint, hence the thread's new current state.
as_nodecontrols what comes next. The update is considered written by a node; by default, the last node that ran. By specifyingas_node, you choose which node is deemed to have produced the update, and therefore which edges apply next.
Relay's test_fork_from_past_checkpoint test illustrates this last point:
history = [s async for s in graph.aget_state_history(config)]
after_triage = next(s for s in history if s.next == ("agent",))
# "What if triage had classified the request as 'other'?"
fork = await graph.aupdate_state(after_triage.config, {"intent": "other"}, as_node="triage")
result = await graph.ainvoke(None, fork, context=Context("cus_lea"), version="v2")
assert result.value["status"] == "escalated"
With as_node="triage", the engine considers that triage just wrote intent = "other": it therefore re-evaluates the route_intent router, which sends to handoff. Without as_node, the update would be attributed to the previous node, and the next path could differ from the expected one.
Concrete uses
Debugging a bad answer. A customer complains about an absurd answer. You find the thread, browse the history, identify the step where the state drifted (a wrong triage, a tool that returned wrong data). You fork just before, fix the state, and check that what follows becomes correct. You then know exactly what to fix: the triage prompt, or the tool.
Testing a prompt fix on a real case. After changing a prompt, replay checkpoints from problematic real cases (anonymized) to verify the fix works, without having to reproduce the whole conversation.
Correcting a run in production. An agent misextracted an order number and is waiting for approval on the wrong order. An operator fixes order_id with update_state, and the run resumes on the right basis. It is the most direct form of human-in-the-loop, detailed in lesson 4.3.
Exploring alternatives. For a creative or planning task, fork the same point several times with variants, and compare the results.
Limits and precautions
- Side effects don't rewind. Time-travel restores the graph's state, not the outside world. A sent email, an issued payment stay done. Any node acting on the outside must be idempotent or protected.
- History has a cost. Time-travel is only possible on retained checkpoints. An aggressive retention policy (keeping only the last checkpoint) makes it impossible: trade off storage cost against investigation capability.
- The code may have changed. Replaying an old checkpoint with a new version of the graph works as long as node names and state keys are compatible. It is one of the stakes of graph versioning (lesson 5.4).
Key takeaways
aget_state_history(config)lists checkpoints from newest to oldest; each has its ownconfig.- Replay:
ainvoke(None, checkpoint_config)resumes the run from that point.- Fork:
aupdate_state(checkpoint_config, values, as_node=...)creates a new modified checkpoint in the same thread.as_nodedetermines which node is deemed to have written the update, hence what runs next.- Time-travel restores state, not the world: side effects must be idempotent.
Practice
- Run
examples/04_persistence.pyand display the thread's whole history, with each checkpoint'sstep,sourceandnext. - On a Relay order-tracking conversation, fork just after
triage, replacingorder_idwithORD-1001: what answer do you get? - Replay the same checkpoint of an automatic refund request twice: how many refunds does the ledger contain, and why?