Context History
The ContextHistory system provides snapshot-based access to historical context states. Think of it as “time travel” for your agent’s memory - you can capture context at any point and access it later with full PACT compliance.Why Context History?
As conversations progress, the context tree continuously evolves through ODI shifting, component expiration, and new insertions. ContextHistory lets you:- Capture snapshots at critical moments (before tool calls, after important decisions)
- Access historical state to understand how context changed over time
- Debug complex behaviors by examining context at specific episodes
- Implement undo/redo or rollback functionality
Every snapshot is PACT v0.1 compliant - you can serialize, store, and restore context states with full fidelity.
Core Concepts
Snapshots
A snapshot is an immutable copy of context state at a specific episode:Historical Access
Access past context states using theagent.history accessor:
Snapshot Triggers
Snapshots are typically created at key moments:- Before tool execution - Capture pre-call state
- After important decisions - Save branching points
- Before context modifications - Implement undo functionality
- At regular intervals - Periodic checkpoints
Creating Snapshots
Using context.seal()
The seal() method creates a snapshot and returns the episode number:
Automatic Snapshot Creation
Egregore creates snapshots automatically at key moments:Accessing Historical Context
The history Accessor
The agent.history accessor provides methods for historical access:
Querying Historical Context
Historical context supports all PACT selectors:Snapshot Metadata
Each snapshot stores metadata for debugging and tracking:PACT Compliance and Serialization
Serializing Snapshots
All snapshots are PACT v0.1 compliant and fully serializable:Restoring from Snapshots
While historical context is read-only, you can restore state by creating a new context:Restoring snapshots creates a new context instance. Use this carefully as it replaces the current context entirely.
Common Patterns
Checkpoint System
Implement periodic checkpoints for long-running agents:Undo/Redo Implementation
Use snapshots to implement undo functionality:Debugging Context Changes
Track how context evolved between episodes:Tool Call Auditing
Capture context before/after tool execution for audit trails:Integration with Other Systems
With MessageScheduler
Snapshots capture episode numbers for correlation:With ContextExplorer
ContextExplorer can load historical snapshots for debugging:With Scaffolds
Scaffolds can access historical context for retrospective analysis:Best Practices
Use descriptive trigger names
Use descriptive trigger names
Trigger names help identify snapshots later:
Don't snapshot too frequently
Don't snapshot too frequently
Snapshots consume memory. Create them at meaningful points only:
Serialize important snapshots
Serialize important snapshots
Persist critical snapshots to disk for durability:
Use snapshots for testing
Use snapshots for testing
Create test fixtures from known-good context states:
API Reference
context.seal(trigger: str) -> int
Create a snapshot of current context state.
Parameters:
trigger(str): Descriptive name for this snapshot
int: Snapshot ID (same as current episode number)
history.at_snapshot(episode: int) -> Context
Access historical context at specific episode.
Parameters:
episode(int): Episode number of snapshot
Context: Read-only context instance from that episode
KeyError: If snapshot doesn’t exist at that episode
history.snapshots -> Dict[int, Dict]
Dictionary of all snapshots.
Returns:
Dict[int, Dict]: Mapping of episode numbers to snapshot data
Limitations and Considerations
Read-Only Access
Historical context is immutable:Snapshot Lifecycle
Snapshots persist for the agent’s lifetime unless explicitly deleted:What’s Next?
Message Scheduler
Understand episode management and rendering
Context Management
Master context operations and component lifecycles
PACT Specification
Deep dive into PACT v0.1 compliance
Context Debugging
Debug context with ContextExplorer and snapshots

