ContextWindowCompactionStrategy Class

Definition

A compaction strategy that derives token thresholds from a model's context window size and maximum output tokens, applying a two-phase compaction pipeline:

  1. Tool result eviction (ToolResultCompactionStrategy) — collapses old tool call groups into concise summaries when the token count exceeds the ToolEvictionThreshold.
  2. Truncation (TruncationCompactionStrategy) — removes the oldest non-system message groups when the token count exceeds the TruncationThreshold.
public sealed class ContextWindowCompactionStrategy : Microsoft.Agents.AI.Compaction.CompactionStrategy
type ContextWindowCompactionStrategy = class
    inherit CompactionStrategy
Public NotInheritable Class ContextWindowCompactionStrategy
Inherits CompactionStrategy
Inheritance
ContextWindowCompactionStrategy

Remarks

The input budget is defined as maxContextWindowTokens - maxOutputTokens, representing the maximum number of tokens available for the conversation input (including system messages, tools, and history).

This strategy is a convenience wrapper around PipelineCompactionStrategy that automates threshold calculation from model specifications.

Constructors

Name Description
ContextWindowCompactionStrategy(Int32, Int32, Double, Double)

Initializes a new instance of the ContextWindowCompactionStrategy class.

Fields

Name Description
DefaultToolEvictionThreshold

The default fraction of the input budget at which tool result eviction triggers.

DefaultTruncationThreshold

The default fraction of the input budget at which truncation triggers.

Properties

Name Description
InputBudgetTokens

Gets the computed input budget in tokens (MaxContextWindowTokens minus MaxOutputTokens).

MaxContextWindowTokens

Gets the maximum context window size in tokens.

MaxOutputTokens

Gets the maximum output tokens per response.

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)
ToolEvictionThreshold

Gets the fraction of the input budget at which tool result eviction triggers.

Trigger

Gets the trigger predicate that controls when compaction proceeds.

(Inherited from CompactionStrategy)
TruncationThreshold

Gets the fraction of the input budget at which truncation triggers.

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