Skip to main content
Every turn is snapshotted automatically. You can rewind to any prior point and continue from there — without re-sending old messages to the model.
Think save states in a game. Run a path, don’t like the outcome? Branch back and try again. The first exchange stays cached.

Key Benefits

  • Zero Duplicate Token Costs: Rewinding to a previous turn reuses cached snapshot history instead of re-sending past messages to the model API.
  • Stateful Branching: Create parallel conversation threads (e.g., Branch 1 vs Branch 2) from a shared context checkpoint.
  • Instant Rollback: Restore clean conversational state instantly when handling user “Undo” or “Edit” actions in UI applications.

How It Works

  1. Run standard conversation turns using agent.run().
  2. Access historical checkpoints via agent.context.listSnapshots().
  3. Call agent.context.branchAt(turnIndex) to create an isolated AgentContext branched at that turn.

Basic Example

In this example, we ask the model two questions, rewind the context back to after Turn 0, and branch off down a completely different path.
time-travel.ts

Production Use Cases

In real-world applications, you rarely hardcode turn numbers. Instead, you map turnIndex to UI actions or database message logs.

1. “Undo” Button in a Chat Interface

When a user clicks Undo on a previous message in your UI, retrieve that message’s turnIndex and branch the context back to before it occurred.

2. “Edit & Resend” Message

When a user edits an old prompt, create a parallel branch without overwriting the original conversation history:

3. “Regenerate Response”

If a model response is unsatisfactory, jump back to the user’s last prompt and re-run execution cleanly without polluting context with the failed assistant attempt: