Skip to content

.NET Compaction - Introducing compaction strategies and pipeline - #4533

Merged
Chris (crickman) merged 108 commits into
mainfrom
copilot/create-message-compaction-provider
Mar 11, 2026
Merged

.NET Compaction - Introducing compaction strategies and pipeline#4533
Chris (crickman) merged 108 commits into
mainfrom
copilot/create-message-compaction-provider

Conversation

@crickman

@crickmanChris (crickman) commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Context management (compaction) is one of the key features in the "dev-harness" effort. This change introduces structured handling of long-running AI chat conversations by compacting historical context while preserving key decisions and intent. By reducing token growth and context drift, it improves response quality, performance, and cost predictability over extended sessions. The implementation is designed to be extensible and transparent, making context lifecycle management a first‑class concern for agent development.

[Spec]

Description

The goal of this approach is inject compaction as part of the IChatClient architecture. This approach maintains an index that allows for presenting a compacted version of the conversation without modifying the source "chat history".

Features

  • Built-in compaction strategies
  • Supports pipeline strategy
  • Supports direct invocation
  • Supports IChatReducer
  • State retained with session serialization
  • Incremental message processing
  • Atomic tool-call grouping
  • Includes tool loops
  • Retains original chat history

Details

Compaction occurs via a CompactionStrategy. A set of strategies are incuded as part of this initial release, including a pipeline strategy that is able to sequentially apply one or more strategies:

StrategyAggressivenessPreserves contextRequires LLMBest for
ToolResultCompactionStrategyLowHigh — only collapses tool resultsNoReclaiming space from verbose tool output
SummarizationCompactionStrategyMediumMedium — replaces history with a summaryYesLong conversations where context matters
SlidingWindowCompactionStrategyHighLow — drops entire turnsNoHard turn-count limits
TruncationCompactionStrategyHighLow — drops oldest groupsNoEmergency token-budget backstops
PipelineCompactionStrategyConfigurableDepends on child strategiesDependsLayered compaction with multiple fallbacks
ChatReducerCompactionStrategyConfigurableDepends on the IChatReducerDependsCompact using an existing IChatReducer

Code

// Setup a chat client for summarizationIChatClientsummarizingChatClient= ...;// Configure the compaction pipeline with one of each strategy, ordered least to most aggressive.PipelineCompactionStrategycompactionPipeline=new(// 1. Gentle: collapse old tool-call groups into short summaries like "[Tool calls: LookupPrice]"newToolResultCompactionStrategy(CompactionTriggers.TokensExceed(0x200)),// 2. Moderate: use an LLM to summarize older conversation spans into a concise messagenewSummarizationCompactionStrategy(summarizingChatClient,CompactionTriggers.TokensExceed(0x8000)),// 3. Aggressive: keep only the last N user turns and their responsesnewSlidingWindowCompactionStrategy(CompactionTriggers.TurnsExceed(4)),// 4. Emergency: drop oldest groups until under the token budgetnewTruncationCompactionStrategy(CompactionTriggers.GroupsExceed(0x1000)));AIAgentagent=agentChatClient.AsAIAgent(newChatClientAgentOptions{Name="ShoppingAssistant",ChatOptions=new(){Instructions="...",Tools=[...],},AIContextProviders=[newCompactionProvider(compactionPipeline)],});

or

AIAgentagent=agentChatClient.AsBuilder().UseAIContextProviders(newCompactionProvider(compactionPipeline)).BuildAIAgent(newChatClientAgentOptions{Name="ShoppingAssistant",ChatOptions=new(){Instructions="...",Tools=[...],}});

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentationUsage: [Issues, PRs], Target: documentation in the code base and learn docs.NETUsage: [Issues, PRs], Target: .Net

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

.NET Compaction - Define and Implement Core Compaction Strategies .NET Harness - Support for chat history compaction

6 participants

@crickman@alliscode@westey-m@markwallace-microsoft