Agents aren’t infallible; sometimes they take a wrong turn — calling the wrong tool or misinterpreting the user’s intent. In such cases you need to “undo” by rolling the Agent back to a previous state and starting over.
When You Need Rewind
| Scenario | Description |
|---|---|
| Wrong tool invoked | The Agent called the wrong tool and got a useless result |
| Misunderstood intent | The Agent misunderstood the user’s intent and answered the wrong question |
| Corrupted state | State data was corrupted, causing abnormal subsequent behavior |
| Retry with a different approach | The Agent’s first attempt failed and needs to try differently |
How Rewind Works
Current state (turn 10)
↓ Rewind to turn 5
Return to state at turn 5
↓ Re-execute
Re-run turns 6, 7, 8, 9, 10
State, message history, and custom data are all rolled back to the target turn.
Usage
Roll back to a specific turn
err := session.RewindTo(ctx, 5) // roll back to turn 5
Roll back and clear error state
// clear all tool results from turn 6 onward
err := session.RewindTo(ctx, 5, sessions.WithClearToolResults(true))
Restore from snapshot
snapshot := session.Snapshot(ctx) // create a snapshot
// ... the Agent ran into an error
session.Restore(ctx, snapshot) // restore to the snapshot point
Practical Examples
Example 1: Tool returned an error, roll back and retry
result, err := session.Run(ctx, userInput)
if err != nil {
// tool call failed, roll back to before this call
session.RewindTo(ctx, session.Turn()-1)
// try a different tool
session.Run(ctx, userInput)
}
Example 2: User changed their mind, roll back to the start
// user suddenly says "never mind, don't book" during ticket booking
session.RewindTo(ctx, 0) // return to the start and begin again
Caveats
Not all state can be rolled back
External side effects of tools (such as sending an email or transferring money) cannot be undone. Rewind only rolls back the Agent’s internal state.
Frequent rewinds hurt the user experience
Users notice when the Agent “redoes” work, which feels bad. Design your Agent to minimize the need for rewinds.
Snapshots have a cost
Creating snapshots frequently consumes memory. Set snapshot frequency and retention reasonably.
FAQ
Q: The tool actually performed an external action. Can it be undone? A: No. Rewind only rolls back the Agent’s internal state. External side effects (such as sending emails or transferring money) must be handled by the business logic.
Q: Will the Agent take the same path after rewinding? A: Not necessarily. Returning to the same state and re-executing may produce different results (for example if the context has changed).
Q: Can rewinding too many times cause problems? A: Yes. Frequent rewinds can lead to confusing state. It’s recommended to record the reason for each rewind to make debugging easier.
Summary
Module 4 complete. We covered:
- Session management: creating, persisting, and restoring conversation history
- State read/write: managing the Agent’s internal state
- Event system: listening to events during Agent execution
- Context Caching: caching context to reduce tokens
- Context Compression: compressing historical messages
- Rewind Sessions: rolling back conversations and re-executing
Next up is Module 5: Multi-Agent Collaboration — how multiple Agents work together.
← Context Compression | Agent Team Architecture →
Want to learn more hands-on Go ADK? Follow the “Full Stack Summit - Dream Beast Programming” WeChat official account for weekly Go / AI programming tutorials and practical tips.
