CompactionStrategy Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Base class for strategies that compact a CompactionMessageIndex to reduce context size.
public abstract class CompactionStrategy
type CompactionStrategy = class
Public MustInherit Class CompactionStrategy
- Inheritance
-
CompactionStrategy
- Derived
Remarks
Compaction strategies operate on CompactionMessageIndex instances, which organize messages into atomic groups that respect the tool-call/result pairing constraint. Strategies mutate the collection in place by marking groups as excluded, removing groups, or replacing message content (e.g., with summaries).
Every strategy requires a CompactionTrigger that determines whether compaction should proceed based on current CompactionMessageIndex metrics (token count, message count, turn count, etc.). The base class evaluates this trigger at the start of CompactAsync(CompactionMessageIndex, ILogger, CancellationToken) and skips compaction when the trigger returns false.
An optional target condition controls when compaction stops. Strategies incrementally exclude groups and re-evaluate the target after each exclusion, stopping as soon as the target returns true. When no target is specified, it defaults to the inverse of the trigger — meaning compaction stops when the trigger condition would no longer fire.
Strategies can be applied at three lifecycle points:
- In-run: During the tool loop, before each LLM call, to keep context within token limits.
- Pre-write: Before persisting messages to storage via ChatHistoryProvider.
- On existing storage: As a maintenance operation to compact stored history.
Multiple strategies can be composed by applying them sequentially to the same CompactionMessageIndex via PipelineCompactionStrategy.
Constructors
| Name | Description |
|---|---|
| CompactionStrategy(CompactionTrigger, CompactionTrigger) |
Initializes a new instance of the CompactionStrategy class. |
Properties
| Name | Description |
|---|---|
| Target |
Gets the target predicate that controls when compaction stops.
Strategies re-evaluate this after each incremental exclusion and stop when it returns |
| Trigger |
Gets the trigger predicate that controls when compaction proceeds. |
Methods
| Name | Description |
|---|---|
| CompactAsync(CompactionMessageIndex, ILogger, CancellationToken) |
Evaluates the Trigger and, when it fires, delegates to CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken) and reports compaction metrics. |
| CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken) |
Applies the strategy-specific compaction logic to the specified message index. |
| EnsureNonNegative(Int32) |
Ensures the provided value is not a negative number. |
Extension Methods
| Name | Description |
|---|---|
| AsChatReducer(CompactionStrategy) |
Returns an IChatReducer that applies this CompactionStrategy to reduce a list of messages. |