Skip to main content

Sessions

Sessions in Agentium track conversation history and arbitrary state per conversation. The SessionManager stores raw messages and replays them as context to the LLM on each run.

How It Works

Every time you call agent.run() or agent.stream() with a sessionId, the agent:
  1. Loads the session’s message history from storage
  2. Includes the history as conversation context to the LLM
  3. After the run, appends the new user/assistant exchange to the session
If you use the same sessionId across multiple calls, the agent maintains a continuous conversation.
If sessionId is omitted, a new UUID is generated per run — each call is a fresh conversation.

Session Object


History Overflow

When numHistoryRuns is set on the agent, the session automatically trims older messages to stay within the limit. This prevents the context window from growing unboundedly.
When messages overflow, if a Memory instance is configured, the trimmed messages are sent to Memory for LLM-powered summarization. This way, old conversation context is preserved as summaries rather than lost.

Token-Based Trimming

For tighter control, set maxContextTokens to automatically trim history based on estimated token count:
The agent estimates tokens for the system prompt, current input, and history. History is trimmed from oldest first until everything fits within the budget.

Session State

Sessions support arbitrary state for persisting data across turns. Access it via RunContext.sessionState in hooks or tools:
State is automatically persisted via updateState after each run.

SessionManager API

SessionManager is used internally by agents but can also be used standalone:

Storage Drivers

InMemoryStorage

Default. Sessions live in process memory. Lost on restart. Good for development.

MongoDBStorage

Persistent sessions in MongoDB. Use for production.

SqliteStorage

Persistent sessions in SQLite. Good for single-node deployments.

PostgresStorage

Persistent sessions in PostgreSQL. Use for scalable deployments.

Session vs Memory vs User Memory

Session is always active. Memory kicks in when history overflows. User Memory persists facts about a person across all their sessions. See Memory and User Memory for details.