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 allAgentEvent 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:- Pruning — replaces tool result content in older messages with
"[pruned]", preserving the most recent ~40K tokens of context. No LLM call needed. - Summarization — when pruning isn’t enough, calls the model to generate a structured summary and replaces the entire history with it.
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 theSessionStore interface: