Session as Context Object

concept
ai-agentscontext-engineeringsession-managementdurability

A pattern where the agent’s session — the full history of events — is stored as a durable object outside both Claude’s context window and the sandbox. The harness interrogates this object to construct the context window, rather than treating the context window itself as the primary record.

Problem

Long-horizon tasks exceed the context window. Standard approaches — compaction, summarization, trimming — involve irreversible decisions about what to keep. It’s difficult to know which tokens future turns will need. Once messages are compacted and the originals discarded, they’re unrecoverable.

Solution

Separate two concerns:

  1. Durable storage — the session log. An append-only event stream that never loses data. Interface: emitEvent(id, event) to write, getEvents() to read positional slices.
  2. Context management — the harness. Fetches events from the session, transforms them (compression, reordering, cache-hit optimization), and constructs Claude’s context window.

The session guarantees durability. The harness handles ephemeral context engineering. Because context management lives in the harness, it can change as models and techniques improve without affecting stored data.

Capabilities

The getEvents() interface supports flexible access:

  • Pick up from the last read position
  • Rewind to events before a specific moment
  • Reread context before a specific action
  • Fetch arbitrary positional slices of the event stream

Events can be transformed in the harness before injection — reorganized for prompt cache hit rate, compressed, filtered, or annotated.

Contrast with in-context approaches

ApproachStorageRecoverabilityFlexibility
Context window onlyEphemeralNone — tokens lost on compactionLimited by window size
Memory tool (files in sandbox)Semi-durableTied to sandbox lifetimeAgent-directed, ad-hoc
REPL-based context objectSemi-durableTied to sandbox lifetimeProgrammatic access via code
Session log (this pattern)Fully durableIndependent of harness and sandboxPositional slicing, harness-managed

The session log is the most durable option because it survives both harness and sandbox failures. Prior work explored REPL-based approaches where context is an object the LLM accesses by writing code — the session log achieves the same interrogability without coupling context to the sandbox.

Connections

  • Managed Agents Architecture: the system that implements this pattern
  • Context window compression: one of the transformations the harness can apply to events fetched from the session
  • Shared persistent memory (CORAL): a related pattern for multi-agent systems — agents coordinate through a durable filesystem rather than direct messaging. The session log serves a similar role for a single agent across time.