microsoft / microsoft/agent-framework

.NET: Update compaction docs to show different ways to use a Compaction Strategy with their implications

Open
#7,358 8 comments 0 reactions 1 assignee View on GitHub

@westey-m is already working on this.

Since Jul 28, 2026.

.NET agents compaction documentation harness
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

Description

Hi!

I'm wondering what the difference is between defining a compaction strategy like this:

```csharp
var compactionStrategy = new ContextWindowCompactionStrategy(
maxContextWindowTokens: 4000,
maxOutputTokens: 800);

var agent = chatClient
.AsAIAgent(new ChatClientAgentOptions
{
ChatOptions = new()
{
Instructions = "You are a helpful assistant.",
},
AIContextProviders = [new CompactionProvider(compactionStrategy)]
});
```

and instead of using it as a `ChatReducer`:

```csharp
var agent = chatClient
.AsAIAgent(new ChatClientAgentOptions
{
ChatOptions = new()
{
Instructions = "You are a helpful assistant.",
},
ChatHistoryProvider = new InMemoryChatHistoryProvider(new()
{
ChatReducer = compactionStrategy.AsChatReducer()
})
});
```

I'm asking because I read the documentation at https://learn.microsoft.com/en-us/agent-framework/agents/conversations/compaction?#registering-through-chatclientagentoptions, but it doesn't mention using the compaction strategy as a ChatReducer.

More generally, what is the recommended way to register a compaction strategy? For example, in Agent Harness I noticed that the strategy is registered both as a `ChatReducer` and via `UseAIContextProviders(...)` on the `ChatClientBuilder`:

https://github.com/microsoft/agent-framework/blob/7b6d2579884b0fde7729342f046ea58847ffdf37/dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs#L195

https://github.com/microsoft/agent-framework/blob/7b6d2579884b0fde7729342f046ea58847ffdf37/dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs#L241

Should one of these approaches be preferred, or do they serve different purposes?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.