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 1vsBranch 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
- Run standard conversation turns using
agent.run(). - Access historical checkpoints via
agent.context.listSnapshots(). - Call
agent.context.branchAt(turnIndex)to create an isolatedAgentContextbranched 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 mapturnIndex 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’sturnIndex and branch the context back to before it occurred.