SlidingWindowCompactionStrategy Class

Definition

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
SlidingWindowCompactionStrategy

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 0 or null are always preserved independently of this value.

Target

Gets the target predicate that controls when compaction stops. Strategies re-evaluate this after each incremental exclusion and stop when it returns true.

(Inherited from CompactionStrategy)
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.

Applies to