ChatHistoryProvider Class

Definition

Provides an abstract base class for fetching chat messages from, and adding chat messages to, chat history for the purposes of agent execution.

public abstract class ChatHistoryProvider
type ChatHistoryProvider = class
Public MustInherit Class ChatHistoryProvider
Inheritance
ChatHistoryProvider
Derived

Remarks

ChatHistoryProvider defines the contract that an AIAgent can use to retrieve messsages from chat history and provide notification of newly produced messages. Implementations are responsible for managing message persistence, retrieval, and any necessary optimization strategies such as truncation, summarization, or archival.

Key responsibilities include:

  • Storing chat messages with proper ordering and metadata preservation
  • Retrieving messages in chronological order for agent context
  • Managing storage limits through truncation, summarization, or other strategies

The ChatHistoryProvider is passed a reference to the AgentSession via ChatHistoryProvider.InvokingContext and ChatHistoryProvider.InvokedContext allowing it to store state in the StateBag. Since a ChatHistoryProvider is used with many different sessions, it should not store any session-specific information within its own instance fields. Instead, any session-specific state should be stored in the associated StateBag.

A ChatHistoryProvider is only relevant for scenarios where the underlying AI service that the agent is using does not use in-service chat history storage.

Security considerations: Agent Framework does not validate or filter the messages returned by the provider during load — they are accepted as-is and treated identically to user-supplied messages. Implementers must ensure that only trusted data is returned. If the underlying storage is compromised, adversarial content could influence LLM behavior via indirect prompt injection — for example, injected messages could alter the conversation context or impersonate different roles. Messages stored in chat history may contain PII and sensitive conversation content; implementers should consider encryption at rest and appropriate access controls for the storage backend.

Constructors

Name Description
ChatHistoryProvider(Func<IEnumerable<ChatMessage>,IEnumerable<ChatMessage>>, Func<IEnumerable<ChatMessage>,IEnumerable<ChatMessage>>, Func<IEnumerable<ChatMessage>, IEnumerable<ChatMessage>>)

Initializes a new instance of the ChatHistoryProvider class.

Properties

Name Description
StateKeys

Gets the set of keys used to store the provider state in the StateBag.

Methods

Name Description
GetService(Type, Object)

Asks the ChatHistoryProvider for an object of the specified type serviceType.

GetService<TService>(Object)

Asks the ChatHistoryProvider for an object of type TService.

InvokedAsync(ChatHistoryProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to add new messages to the chat history.

InvokedCoreAsync(ChatHistoryProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to add new messages to the chat history.

InvokingAsync(ChatHistoryProvider+InvokingContext, CancellationToken)

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

InvokingCoreAsync(ChatHistoryProvider+InvokingContext, CancellationToken)

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

ProvideChatHistoryAsync(ChatHistoryProvider+InvokingContext, CancellationToken)

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

StoreChatHistoryAsync(ChatHistoryProvider+InvokedContext, CancellationToken)

When overridden in a derived class, adds new messages to the chat history at the end of the agent invocation.

Applies to