Skip to main content
This example is available on GitHub: examples/01_standalone_sdk/10_persistence.py
Save conversation state to disk and restore it later for long-running or multi-session workflows:
examples/01_standalone_sdk/10_persistence.py
Running the Example

Saving State

Create a conversation with a unique ID to enable persistence:

Restoring State

Restore a conversation using the same ID and persistence directory:

What Gets Persisted

The conversation state includes information that allows seamless restoration:
  • Message History: Complete event log including user messages, agent responses, and system events
  • Agent Configuration: LLM settings, tools, MCP servers, and agent parameters
  • Execution State: Current agent status (idle, running, paused, etc.), iteration count, and stuck detection settings
  • Tool Outputs: Results from bash commands, file operations, and other tool executions
  • Statistics: LLM usage metrics like token counts and API calls
  • Workspace Context: Working directory and file system state
  • Activated Skills: Skills that have been enabled during the conversation
  • Secrets: Managed credentials and API keys
For the complete implementation details, see the ConversationState class in the source code.

Persistence Directory Structure

When you set a persistence_dir, your conversation will be persisted to a directory structure where each conversation has its own subdirectory. By default, the persistence directory is workspace/conversations/ (unless you specify a custom path). Directory structure:
Each conversation directory contains:
  • base_state.json: The core conversation state including agent configuration, execution status, statistics, and metadata
  • events/: A subdirectory containing individual event files, each named with a sequential index and event ID (e.g., event-00000-abc123.json)
The collection of event files in the events/ directory represents the same trajectory data you would find in the trajectory.json file from OpenHands V0, but split into individual files for better performance and granular access.

Next Steps