MessageCountingChatReducer Class

Definition

Provides a chat reducer that limits the number of non-system messages in a conversation to a specified maximum count, preserving the most recent messages and the first system message if present.

public ref class MessageCountingChatReducer sealed : Microsoft::Extensions::AI::IChatReducer
[System.Diagnostics.CodeAnalysis.Experimental("MEAI001")]
public sealed class MessageCountingChatReducer : Microsoft.Extensions.AI.IChatReducer
public sealed class MessageCountingChatReducer : Microsoft.Extensions.AI.IChatReducer
[<System.Diagnostics.CodeAnalysis.Experimental("MEAI001")>]
type MessageCountingChatReducer = class
    interface IChatReducer
type MessageCountingChatReducer = class
    interface IChatReducer
Public NotInheritable Class MessageCountingChatReducer
Implements IChatReducer
Inheritance
MessageCountingChatReducer
Attributes
Implements

Remarks

This reducer is useful for scenarios where it is necessary to constrain the size of a chat history, such as when preparing input for models with context length limits. The reducer always includes the first encountered system message, if any, and then retains up to the specified number of the most recent non-system messages. Messages containing function call or function result content are excluded from the reduced output.

Constructors

MessageCountingChatReducer(Int32)

Initializes a new instance of the MessageCountingChatReducer class.

Methods

ReduceAsync(IEnumerable<ChatMessage>, CancellationToken)

Reduces the size of a list of chat messages.

Applies to