Skip to main content
While Agent is a stateless executor, Session adds the statefulness and resilience you need for interactive, multi-turn conversations. It owns the message history and handles compaction, retry, persistence, and lifecycle hooks automatically.

Creating a Session

session.send() yields all the same AgentEvent types as agent.run(), plus additional session lifecycle events.

Configuration

Session Events

In addition to all AgentEvent types, session.send() yields:

Compaction

When a conversation approaches the context window limit, the session automatically compacts the message history. The default strategy works in two phases:
  1. Pruning — replaces tool result content in older messages with "[pruned]", preserving the most recent ~40K tokens of context. No LLM call needed.
  2. Summarization — when pruning isn’t enough, calls the model to generate a structured summary and replaces the entire history with it.
You can customize compaction at multiple levels:

Retry

Transient API errors (429, 500, 502, 503, 504, 529, rate limits, timeouts) are retried automatically with exponential backoff and jitter. Retries only happen before any content has been streamed — once the model starts producing output, the session commits to that attempt.

Hooks

Hooks let you intercept and customize the session lifecycle:

Persistence

Plug in any storage backend by implementing the SessionStore interface:

Direct State Access

The session’s message history is directly readable and writable: