梦兽编程
AI_SUITE

Rewind Sessions: Rollback conversations and let the Agent restart

Detailed explanation of ADK Go's Rewind mechanism — how to roll an Agent back to a historical state and re-execute, useful when the Agent goes down the wrong path or needs to start over.

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

ScenarioDescription
Wrong tool invokedThe Agent called the wrong tool and got a useless result
Misunderstood intentThe Agent misunderstood the user’s intent and answered the wrong question
Corrupted stateState data was corrupted, causing abnormal subsequent behavior
Retry with a different approachThe 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.

Frequently Asked Questions

What is Rewind in ADK Go?

Rewind lets an Agent roll back to a previous state in the conversation and re-execute from there, effectively giving the conversation an undo capability.

When should I use Rewind?

Use Rewind when the Agent takes a wrong turn, produces incorrect results, or when the user wants to explore an alternative branch from an earlier point.

How does Rewind work?

ADK Go records snapshots of the Session or State at key points. Rewind restores one of these snapshots and continues execution from that point.

Can Rewind recover from runtime errors?

Yes, if a snapshot was taken before the error occurred. Rewind restores the last valid snapshot so the Agent can retry or take a different path.

Are there limits to how far back I can Rewind?

The rewind depth depends on how many snapshots your application keeps. You should balance storage cost against the need for long rollback windows.