Why it matters

State management is what separates toy agents from production agents. Handling long conversations, resuming after restarts, and controlling context usage are non-trivial.

Advertisement

The architecture

Session: keyed by session ID (per-user or per-conversation). Contains message history, tool results, custom state.

SessionService: pluggable backend for storage. In-memory (dev), database (production), custom implementations.

Session state pipelineSession IDconversation keySessionServicestorage backendState restoreon next turnPersistent SessionService restores state across restarts; in-memory loses it
Session storage flow.
Advertisement

How it works end to end

Context window management: as conversation grows, history exceeds model context. Strategies: sliding window (keep last N turns), summarization (compress old history into a summary), semantic retrieval (retrieve relevant past turns).

Extracted facts: agent can promote conversational information to persistent memory (see memory article).

Concurrent access: if two turns from same session hit concurrently, race conditions possible. Lock or serialize per-session.