AI Hints
Home›Agentic AI Tutorial›LangGraph Power
Agentic AI · Lesson 45 of 100

Time Travel

Rewinding an agent.

⏱ 2 min read▶ Video at 1:18:34🎓 Free
Watch this lesson in the Agentic AI courseWatch on YouTube ↗Full Agentic AI course

An agent refunded nine hundred dollars by mistake. What went wrong? With a normal program, you'd read logs, and guess. But we saved every step. So we can rewind to the moment before the mistake, change one thing, and run it again.

Time Travel
From the video: Time Travel
  • A $900 mistake
  • We saved every step
  • Rewind, change, rerun

Rewind and fix

This agent decides on nine hundred dollars. We can list every saved step. We find the moment just before it acted. We change the amount to forty, which creates a new branch of history. Then we continue from there. Run it. The first run refunded nine hundred. Then, from the same point, with one change, forty. The original history is still there. It's like version control for an agent's decisions.

timetravel.pypython
from typing import TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    amount: int
    note: str

g = StateGraph(State)
g.add_node("decide", lambda s: {"amount": 900})
g.add_node("act", lambda s: {"note": f"Refunded {s['amount']}"})
g.add_edge(START, "decide")
g.add_edge("decide", "act")
g.add_edge("act", END)
app = g.compile(checkpointer=InMemorySaver())

t = {"configurable": {"thread_id": "1"}}
print("First run:", app.invoke({"amount": 0, "note": ""}, t)["note"])

history = list(app.get_state_history(t))
print("Saved steps:", len(history))

before_act = next(s for s in history if s.next == ("act",))
fixed = app.update_state(before_act.config, {"amount": 40})
print("Rerun:", app.invoke(None, fixed)["note"])
Output
First run: Refunded 900
Saved steps: 4
Rerun: Refunded 40

What time travel is for

This isn't just a party trick. You can debug by replaying a failed run, and seeing the state at each step. You can test a new prompt from a certain point, without repeating the costly early steps. You can try what if questions. And support people can fix a stuck case by hand. But remember, it rewinds the agent's state, not the real world. A refund that's been paid stays paid.

Debugreplay and inspect
Testfrom any step
What iftry another path
Carefulthe real world doesn't rewind

Summary

Graphs give you pause, resume, streaming, and time travel. That's LangGraph. You can now build one very capable agent. So what about many of them? Next, multi-agent systems.

Key takeaways
  • Every step can be replayed
  • update_state creates a branch
  • The real world doesn't rewind
Next up: Why Many Agents? — and when not to