Share via


ChatHistoryProvider.InvokingCoreAsync Method

Definition

Called at the start of agent invocation to provide messages for the next agent invocation.

protected virtual System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>> InvokingCoreAsync(Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext context, System.Threading.CancellationToken cancellationToken = default);
abstract member InvokingCoreAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<seq<Microsoft.Extensions.AI.ChatMessage>>
override this.InvokingCoreAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<seq<Microsoft.Extensions.AI.ChatMessage>>
Protected Overridable Function InvokingCoreAsync (context As ChatHistoryProvider.InvokingContext, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of IEnumerable(Of ChatMessage))

Parameters

context
ChatHistoryProvider.InvokingContext

Contains the request context including the caller provided messages that will be used by the agent for this invocation.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

A task that represents the asynchronous operation. The task result contains a collection of ChatMessage instances that will be used for the agent invocation.

Remarks

If the total message history becomes very large, implementations should apply appropriate strategies to manage storage constraints, such as:

  • Truncating older messages while preserving recent context
  • Summarizing message groups to maintain essential context
  • Implementing sliding window approaches for message retention
  • Archiving old messages while keeping active conversation context

The default implementation of this method, calls ProvideChatHistoryAsync(ChatHistoryProvider+InvokingContext, CancellationToken) to get the chat history messages, applies the optional retrieval output filter, and merges the returned messages with the caller provided messages (with chat history messages appearing first) before returning the full message list to be used for the invocation. For most scenarios, overriding ProvideChatHistoryAsync(ChatHistoryProvider+InvokingContext, CancellationToken) is sufficient to return the desired chat history messages, while still benefiting from the default merging and filtering behavior. However, for scenarios that require more control over message filtering, merging or source stamping, overriding this method allows you to directly control the full set of messages returned for the invocation.

Applies to