Language

ChatHistoryProvider.ProvideChatHistoryAsync Method

Definition

When overridden in a derived class, provides the chat history messages to be used for the current invocation.

protected virtual System.Threading.Tasks.ValueTask<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>> ProvideChatHistoryAsync(Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext context, System.Threading.CancellationToken cancellationToken = default);
abstract member ProvideChatHistoryAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<seq<Microsoft.Extensions.AI.ChatMessage>>
override this.ProvideChatHistoryAsync : Microsoft.Agents.AI.ChatHistoryProvider.InvokingContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<seq<Microsoft.Extensions.AI.ChatMessage>>
Protected Overridable Function ProvideChatHistoryAsync (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 in ascending chronological order (oldest first).

Remarks

This method is called from InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken). Note that InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken) can be overridden to directly control message filtering, merging and source stamping, in which case it is up to the implementer to call this method as needed to retrieve the unfiltered/unmerged chat history messages.

In contrast with InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken), this method only returns additional messages to be added to the request, while InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken) is responsible for returning the full set of messages to be used for the invocation (including caller provided messages).

Messages are returned in chronological order to maintain proper conversation flow and context for the agent. The oldest messages appear first in the collection, followed by more recent messages.

Security consideration: Messages loaded from storage should be treated with the same caution as user-supplied messages. A compromised storage backend could alter message roles to escalate trust (e.g., changing user messages to system messages) or inject adversarial content that influences LLM behavior.

Applies to