Mastering LangGraph Time Travel

Mastering LangGraph Time Travel
When using model-based decision-making non-deterministic systems (e.g., LLM-driven agents), it is useful to closely examine their decision-making processes:
πŸ€” Understanding Reasoning: Analyzing the steps that lead to successful outcomes.
🐞 Debugging Errors: Identifying where and why errors occur.
πŸ” Exploring Alternatives: Testing different paths to discover better solutions.
We refer to these debugging techniques as time travel, consisting of two key operations:
Replay πŸ” and Forking πŸ”€.
Replay
Mastering LangGraph Time Travel
The replay function allows us to revisit and reproduce the past operations of the agent. This can be done from the current state of the graph (or a checkpoint) or from a specific checkpoint. To replay from the current state, simply pass None as the input along with the thread id:
thread = {"configurable": {"thread_id": "1"}}for event in graph.stream(None, thread, stream_mode="values"):    print(event)
To replay operations from a specific checkpoint, first retrieve all checkpoints of the thread:
all_checkpoints = []for state in graph.get_state_history(thread):    all_checkpoints.append(state)
Each checkpoint has a unique ID. After identifying the desired checkpoint (e.g., xyz), include its ID in the configuration:
config = {'configurable': {'thread_id': '1', 'checkpoint_id': 'xyz'}}for event in graph.stream(None, config, stream_mode="values"):    print(event)
The graph effectively replays previously executed nodes rather than re-executing them, leveraging its awareness of previous checkpoint executions.
Forking
Mastering LangGraph Time Travel
Forking allows re-accessing past operations of the agent and exploring alternative paths in the graph.
To edit a specific checkpoint (e.g., xyz), provide its checkpoint_id when updating the graph state:
config = {"configurable": {"thread_id": "1", "checkpoint_id": "xyz"}}graph.update_state(config, {"state": "updated state"})
This will create a new fork checkpoint xyz-fork, from which we can continue running the graph:
config = {'configurable': {'thread_id': '1', 'checkpoint_id': 'xyz-fork'}}for event in graph.stream(None, config, stream_mode="values"):    print(event)

Leave a Comment