Core Components
The Context system consists of four key components:SessionIdentity
Uniquely identifies a storage session using agent name, user ID, chat name, group, and scope.
Context
Central orchestration layer that manages multiple storage instances for an agent.
IdentityStorage
Tracks all storage identities registered within a context for discovery and management.
Storage
Abstract base for all storage implementations (chat history, usage, custom).
Session Identity
ASessionIdentity uniquely identifies a storage key using these components:
Key Generation
The identity key is generated as:Key generation examples
Key generation examples
chatHistory_SupportAgent_user-123— User-based chat historychatHistory_SupportAgent_session-abc— Session-based chat historyusage_sql_user-456— User usage in SQL group
Creating Agents with Identities
LarAgent provides several ways to create agents with different identity configurations.Session-based Creation
Use session-based creation when you need to manage conversations by a custom session key rather than user identity. This is ideal for anonymous users, guest sessions, or when you want explicit control over session naming.User-based Creation
Use user-based creation to automatically tie conversations to authenticated users. The identity key will include the user ID, enabling easy querying and management of all conversations for a specific user.Reconstructing from Identity
UsefromIdentity() to recreate an agent instance from a previously tracked identity. This is useful for admin panels, background jobs, or any scenario where you need to operate on existing conversations.
Grouping
Groups enable shared context between multiple agents. When a group is set, the storage key uses the group name instead of the agent name.Key format without group:
{scope}_{agentName}_{userId|chatName|'default'}Key format with group: {scope}_{group}_{userId|chatName|'default'}Static Group Assignment
Dynamic Group Resolution
For runtime group resolution (e.g., based on current tenant):Custom Identity Overrides
Override identity creation methods to customize how chat history and usage tracking are scoped. This is useful for multi-tenant applications, team-based sharing, or any scenario requiring different storage isolation.Overriding History Identity
UsecreateHistoryIdentity() to customize chat history scoping:
Overriding Usage Identity
UsecreateUsageIdentity() to customize usage tracking scoping:
Dual Identity (Different Scopes)
Use different scopes for history and usage:Advanced: Full storage override
Advanced: Full storage override
Context Operations
Accessing Context
You can access the context object directly from an agent:The
contextIdentity is the general identity of the agent. It is stored in IdentityStorage and holds all other storage identities associated with the agent.Bulk Operations
Getting Tracked Keys
Temporary Sessions
Sessions prefixed with_temp are not tracked in IdentityStorage:
- Preview/demo sessions
- Test sessions
- One-time interactions
Context Events
Listen to context lifecycle events for custom behavior:Identity Storage Events
Next Steps
Chat History
Learn how to configure and manage conversation history storage.
Usage Tracking
Track token usage and costs across agent interactions.
Data Model
Create typed, structured data for custom storage implementations.
Truncation
Manage context window limits with truncation strategies.

