Skip to main content
The Context system in LarAgent provides a unified way to manage storage isolation, session tracking, and data scoping across agents and users. It serves as the central orchestration layer that connects agents with their storage backends.

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

A SessionIdentity uniquely identifies a storage key using these components:

Key Generation

The identity key is generated as:
  • chatHistory_SupportAgent_user-123 — User-based chat history
  • chatHistory_SupportAgent_session-abc — Session-based chat history
  • usage_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

Use fromIdentity() 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'}
This is useful for multi-tenant applications where agents should share context within a tenant, or when you want multiple agent types to work with the same conversation history.

Static Group Assignment

Dynamic Group Resolution

For runtime group resolution (e.g., based on current tenant):
The group setting applies to all storages by default. To use different scopes for chat history vs usage tracking, use the custom identity override methods below.

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

Use createHistoryIdentity() to customize chat history scoping:

Overriding Usage Identity

Use createUsageIdentity() to customize usage tracking scoping:

Dual Identity (Different Scopes)

Use different scopes for history and usage:
Use createHistoryIdentity() and createUsageIdentity() for simple identity customization. For full control over storage creation (including drivers and options), override createChatHistory() or createUsageStorage() instead.
Use this approach when you need full control over storage configuration, such as custom drivers or storage options.

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:
This is useful for:
  • 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.