Class UserScopedMemory
Provides a user-scoped view over a shared AgentMemory instance, automatically namespacing data sources by user ID and filtering retrieval to only return that user's memories.
public sealed class UserScopedMemory
- Inheritance
-
UserScopedMemory
- Inherited Members
Examples
Example: Multi-user memory with a shared store
using LMKit.Agents;
using LMKit.Agents.Memory;
using LMKit.Model;
using var embeddingModel = new LM("path/to/embedding-model.gguf");
// Shared memory store (persisted to disk)
var sharedMemory = new AgentMemory(embeddingModel);
// Create user-scoped views
var aliceMemory = new UserScopedMemory(sharedMemory, "alice");
var bobMemory = new UserScopedMemory(sharedMemory, "bob");
// Alice's data is isolated from Bob's
await aliceMemory.SaveInformationAsync("preferences", "Prefers dark mode.", "pref_001");
await bobMemory.SaveInformationAsync("preferences", "Prefers light mode.", "pref_001");
// Each user only sees their own memories
Console.WriteLine($"Alice entries: {aliceMemory.EntryCount}"); // 1
Console.WriteLine($"Bob entries: {bobMemory.EntryCount}"); // 1
// Shared memory contains both
Console.WriteLine($"Total entries: {sharedMemory.EntryCount}"); // 2
Remarks
UserScopedMemory is a thin wrapper that enables multi-user memory
on top of a single AgentMemory. Each user's data sources are
transparently prefixed with "{userId}::", and a
DataFilter is applied so that retrieval only considers data
sources belonging to the current user.
All persistence, capacity, eviction, time-decay, and consolidation features of the underlying AgentMemory remain fully functional. Serialization and deserialization operate on the shared memory, preserving all users' data.
Constructors
- UserScopedMemory(AgentMemory, string)
Initializes a new instance of the UserScopedMemory class.
Properties
- DataSources
Gets the data sources that belong to this user.
- EntryCount
Gets the total number of memory entries belonging to this user.
- SharedMemory
Gets the underlying shared AgentMemory instance.
- UserId
Gets the user identifier that scopes this memory view.
Methods
- ApplyFilter()
Applies the user-scope filter to the underlying memory so that retrieval operations only consider this user's data sources.
- ClearFilter()
Removes the user-scope filter from the underlying memory, restoring unfiltered retrieval across all users.
- ClearUserData()
Removes all data sources belonging to this user.
- IsEmpty()
Determines whether this user has any stored memories.
- RemoveDataSource(string)
Removes a user-scoped data source by its identifier.
- SaveInformation(string, string, string, MemoryType, MetadataCollection, CancellationToken)
Saves text information synchronously into a user-scoped data source.
- SaveInformationAsync(string, string, string, MemoryType, MetadataCollection, CancellationToken)
Saves text information into a user-scoped data source with a specified memory type.
- SaveInformationAsync(string, string, string, MetadataCollection, CancellationToken)
Saves text information into a user-scoped data source using semantic memory type.
- UnscopeIdentifier(string)
Strips the user namespace prefix from a data source identifier.