Skip to content

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

Description

@marcominerva

Hi!

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

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:

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:

ChatReducer = compactionStrategy.AsChatReducer(),

chatClientBuilder = chatClientBuilder.UseAIContextProviders(compactionProvider);

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

Metadata

Metadata

Assignees

Labels

.NETUsage: [Issues, PRs], Target: .NetagentsUsage: [Issues, PRs], Target: Single agentdocumentationUsage: [Issues, PRs], Target: documentation in the code base and learn docs

Type

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions