ChatHistoryMemoryProvider Class

Definition

A context provider that stores all chat history in a vector store and is able to retrieve related chat history later to augment the current conversation.

public sealed class ChatHistoryMemoryProvider : Microsoft.Agents.AI.MessageAIContextProvider, IDisposable
type ChatHistoryMemoryProvider = class
    inherit MessageAIContextProvider
    interface IDisposable
Public NotInheritable Class ChatHistoryMemoryProvider
Inherits MessageAIContextProvider
Implements IDisposable
Inheritance
Implements

Remarks

This provider stores chat messages in a vector store and retrieves relevant previous messages to provide as context during agent invocations. It uses the VectorStore and VectorStoreCollection abstractions to work with any compatible vector store implementation.

Messages are stored during the Microsoft.Agents.AI.ChatHistoryMemoryProvider.StoreAIContextAsync(Microsoft.Agents.AI.AIContextProvider.InvokedContext,System.Threading.CancellationToken) method and retrieved during the Microsoft.Agents.AI.ChatHistoryMemoryProvider.ProvideAIContextAsync(Microsoft.Agents.AI.AIContextProvider.InvokingContext,System.Threading.CancellationToken) method using semantic similarity search.

Behavior is configurable through ChatHistoryMemoryProviderOptions. When OnDemandFunctionCalling is selected the provider exposes a function tool that the model can invoke to retrieve relevant memories on demand instead of injecting them automatically on each invocation.

Security considerations:

  • Indirect prompt injection: Messages retrieved from the vector store via semantic search are injected into the LLM context. If the vector store is compromised, adversarial content could influence LLM behavior. The data returned from the store is accepted as-is without validation or sanitization.
  • PII and sensitive data: Conversation messages (including user inputs and LLM responses) are stored as vectors in the underlying store. These messages may contain PII or sensitive information. Ensure the vector store is configured with appropriate access controls and encryption at rest.
  • On-demand search tool: When using OnDemandFunctionCalling, the AI model controls when and what to search for. The search query is AI-generated and should be treated as untrusted input by the vector store implementation.
  • Trace logging: When Trace is enabled, full search queries and results may be logged. This data may contain PII.

Constructors

Name Description
ChatHistoryMemoryProvider(VectorStore, String, Int32, Func<AgentSession, ChatHistoryMemoryProvider.State>, ChatHistoryMemoryProviderOptions, ILoggerFactory)

Initializes a new instance of the ChatHistoryMemoryProvider class.

Properties

Name Description
ProvideInputMessageFilter

Gets the filter function to apply to input messages before providing context via ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken).

(Inherited from AIContextProvider)
StateKeys

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

StoreInputRequestMessageFilter

Gets the filter function to apply to request messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)
StoreInputResponseMessageFilter

Gets the filter function to apply to response messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)

Methods

Name Description
Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

GetService(Type, Object)

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

(Inherited from AIContextProvider)
GetService<TService>(Object)

Asks the AIContextProvider for an object of type TService.

(Inherited from AIContextProvider)
InvokedAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokedCoreAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokingAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
InvokingAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional messages.

(Inherited from MessageAIContextProvider)
InvokingCoreAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
InvokingCoreAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional messages.

(Inherited from MessageAIContextProvider)
ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken)

When overridden in a derived class, provides additional AI context to be merged with the input context for the current invocation.

(Inherited from MessageAIContextProvider)
ProvideMessagesAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

When overridden in a derived class, provides additional messages to be merged with the input messages for the current invocation.

(Inherited from MessageAIContextProvider)
StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken)

When overridden in a derived class, processes invocation results at the end of the agent invocation.

(Inherited from AIContextProvider)

Applies to