Method ConsolidateAsync
ConsolidateAsync(LM, CancellationToken)
Consolidates similar memory entries by merging clusters of semantically related entries into single, summarized entries using an LLM.
public Task<MemoryConsolidationResult> ConsolidateAsync(LM consolidationModel, CancellationToken cancellationToken = default)
Parameters
consolidationModelLMThe language model used to generate consolidated summaries. A smaller model (e.g., 1B-4B parameters) works well for this task.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<MemoryConsolidationResult>
A MemoryConsolidationResult describing what was merged.
Examples
Example: Running memory consolidation
using LMKit.Agents;
using LMKit.Agents.Memory;
using LMKit.Model;
using var consolidationModel = LM.LoadFromModelID("qwen3:0.6b");
memory.ConsolidationSimilarityThreshold = 0.7f;
var result = await memory.ConsolidateAsync(consolidationModel);
Console.WriteLine($"Merged {result.ClustersMerged} clusters, removed {result.EntriesRemoved} entries.");
Console.WriteLine($"Entries: {result.EntryCountBefore} -> {result.EntryCountAfter}");
Remarks
Consolidation scans all memory entries within each data source and identifies clusters of entries whose pairwise similarity exceeds ConsolidationSimilarityThreshold. For each cluster, the LLM generates a single merged entry that captures the combined information.
The original entries in each cluster are removed and replaced with the consolidated
entry. Metadata is preserved: the highest importance and the earliest
created_at timestamp from the cluster are carried forward.
Subscribe to BeforeMemoryConsolidated to inspect or cancel individual merges before they are applied.
Exceptions
- ArgumentNullException
Thrown when
consolidationModelisnull.