Skip to main content

ConversationState: Event-Sourced State Management

ConversationState is the single source of truth for all conversation data in the OpenHands SDK. Rather than storing mutable state directly, it derives all state on-demand from an immutable event log.

Key Concept: Event Sourcing

Benefits:
  • Perfect Reproducibility: Same events always produce same state
  • Time-Travel Debugging: Replay any conversation exactly
  • Audit Trail: Complete history of what happened and when
  • No Race Conditions: Immutable events eliminate entire class of bugs

Event Hierarchy

Events form a three-level hierarchy:

Base Event

All events share common structure:

LLMConvertibleEvent

Events that can be sent to the LLM:

ConversationState API

Creating and Managing State

Persistence

Events automatically save to disk when configured:

Event Store Implementation

Derived State Properties

All state is computed from events, not stored directly:

Agent Execution Status

Status Transitions:

Conversation History

Metrics

Task List

Event Replay

Replay conversations for debugging:

Reproducibility Guarantee

The same event sequence always produces the same state:

Event Serialization

Events use discriminated union pattern for type-safe serialization:

Discriminated Union Pattern

Advanced: Efficient Persistence

The SDK uses differential persistence to minimize I/O:

Example: Pause and Resume

Best Practices

✅ Do

  • Enable persistence for production workflows
  • Use unique conversation IDs for different tasks
  • Replay conversations when debugging issues
  • Monitor metrics via state.metrics

❌ Don’t

  • Mutate events after creation (they’re immutable)
  • Store state externally - always derive from events
  • Manually manage event files - let the SDK handle it

API Reference

Next Steps

  • Agent - Learn about stateless agents
  • Events - Deep dive into event types
  • Persistence - Advanced persistence patterns
  • Debugging - Use replay for debugging