ToolResultCompactionStrategy Class

Definition

A compaction strategy that collapses old tool call groups into single concise assistant messages, removing the detailed tool results while preserving a record of which tools were called and what they returned.

public sealed class ToolResultCompactionStrategy : Microsoft.Agents.AI.Compaction.CompactionStrategy
type ToolResultCompactionStrategy = class
    inherit CompactionStrategy
Public NotInheritable Class ToolResultCompactionStrategy
Inherits CompactionStrategy
Inheritance
ToolResultCompactionStrategy

Remarks

This is the gentlest compaction strategy — it does not remove any user messages or plain assistant responses. It only targets ToolCall groups outside the protected recent window, replacing each multi-message group (assistant call + tool results) with a single assistant message in a YAML-like format:

[Tool Calls]
get_weather:
  - Sunny and 72°F
search_docs:
  - Found 3 docs

A custom ToolCallFormatter can be supplied to override the default YAML-like summary format. The formatter receives the CompactionMessageGroup being collapsed and must return the replacement summary string. DefaultToolCallFormatter(CompactionMessageGroup) is the built-in default and can be reused inside a custom formatter when needed.

MinimumPreservedGroups is a hard floor: even if the Target has not been reached, compaction will not touch the last MinimumPreservedGroups non-system groups.

The CompactionTrigger predicate controls when compaction proceeds. Use CompactionTriggers for common trigger conditions such as token thresholds.

Constructors

Name Description
ToolResultCompactionStrategy(CompactionTrigger, Int32, CompactionTrigger)

Initializes a new instance of the ToolResultCompactionStrategy class.

Fields

Name Description
DefaultMinimumPreserved

The default minimum number of most-recent non-system groups to preserve.

Properties

Name Description
MinimumPreservedGroups

Gets the minimum number of most-recent non-system groups that are always preserved. This is a hard floor that compaction cannot exceed, regardless of the target condition.

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

An optional custom formatter that converts a CompactionMessageGroup into a summary string. When null, DefaultToolCallFormatter(CompactionMessageGroup) is used, which produces a YAML-like block listing each tool name and its results.

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)
DefaultToolCallFormatter(CompactionMessageGroup)

The default formatter that produces a YAML-like summary of tool call groups, including tool names, results, and deduplication counts for repeated tool names.

Extension Methods

Name Description
AsChatReducer(CompactionStrategy)

Returns an IChatReducer that applies this CompactionStrategy to reduce a list of messages.

Applies to