Skip to main content

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 the agent.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:
Historical context is read-only. You cannot insert, update, or delete components in historical snapshots.

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

Trigger names help identify snapshots later:
Snapshots consume memory. Create them at meaningful points only:
Persist critical snapshots to disk for durability:
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
Returns:
  • int: Snapshot ID (same as current episode number)
Example:

history.at_snapshot(episode: int) -> Context

Access historical context at specific episode. Parameters:
  • episode (int): Episode number of snapshot
Returns:
  • Context: Read-only context instance from that episode
Raises:
  • KeyError: If snapshot doesn’t exist at that episode
Example:

history.snapshots -> Dict[int, Dict]

Dictionary of all snapshots. Returns:
  • Dict[int, Dict]: Mapping of episode numbers to snapshot data
Snapshot data structure:
Example:

Limitations and Considerations

Memory Usage: Each snapshot stores a full copy of context state. For long-running agents, consider:
  • Periodic snapshot cleanup (delete old snapshots)
  • Selective snapshotting (only at critical points)
  • External storage (serialize and remove from memory)

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