SlidingWindowCompactionStrategy 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.
A compaction strategy that removes the oldest user turns and their associated response groups to bound conversation length.
public sealed class SlidingWindowCompactionStrategy : Microsoft.Agents.AI.Compaction.CompactionStrategy
type SlidingWindowCompactionStrategy = class
inherit CompactionStrategy
Public NotInheritable Class SlidingWindowCompactionStrategy
Inherits CompactionStrategy
- Inheritance
Remarks
This strategy always preserves system messages. It identifies user turns in the conversation (via TurnIndex) and excludes the oldest turns one at a time until the Target condition is met.
MinimumPreservedTurns is a hard floor: even if the Target has not been reached, compaction will not touch the last MinimumPreservedTurns turns (by TurnIndex). Groups with a TurnIndex of 0 or null are always preserved regardless of this setting.
This strategy is more predictable than token-based truncation for bounding conversation length, since it operates on logical turn boundaries rather than estimated token counts.
Constructors
| Name | Description |
|---|---|
| SlidingWindowCompactionStrategy(CompactionTrigger, Int32, CompactionTrigger) |
Initializes a new instance of the SlidingWindowCompactionStrategy class. |
Fields
| Name | Description |
|---|---|
| DefaultMinimumPreserved |
The default minimum number of most-recent turns to preserve. |
Properties
| Name | Description |
|---|---|
| MinimumPreservedTurns |
Gets the minimum number of most-recent turns (by TurnIndex) that are always preserved.
This is a hard floor that compaction cannot exceed, regardless of the target condition.
Groups with TurnIndex of |
| 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. (Inherited from CompactionStrategy) |
Methods
| Name | Description |
|---|---|
| CompactAsync(CompactionMessageIndex, ILogger, CancellationToken) |
Evaluates the Trigger and, when it fires, delegates to CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken) and reports compaction metrics. (Inherited from CompactionStrategy) |
| CompactCoreAsync(CompactionMessageIndex, ILogger, CancellationToken) |
Applies the strategy-specific compaction logic to the specified message index. (Inherited from CompactionStrategy) |
Extension Methods
| Name | Description |
|---|---|
| AsChatReducer(CompactionStrategy) |
Returns an IChatReducer that applies this CompactionStrategy to reduce a list of messages. |