AIContextProvider Class

Definition

Provides an abstract base class for components that enhance AI context during agent invocations.

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

Remarks

An AI context provider is a component that participates in the agent invocation lifecycle by:

  • Listening to changes in conversations
  • Providing additional context to agents during invocation
  • Supplying additional function tools for enhanced capabilities
  • Processing invocation results for state management or learning

Context providers operate through a two-phase lifecycle: they are called at the start of invocation via InvokingAsync(AIContextProvider+InvokingContext, CancellationToken) to provide context, and optionally called at the end of invocation via InvokedAsync(AIContextProvider+InvokedContext, CancellationToken) to process results.

Security considerations: Context providers may inject messages with any role, including system, which has the highest trust level and directly shapes LLM behavior. Developers must ensure that all providers attached to an agent are trusted. Agent Framework does not validate or filter the data returned by providers — it is accepted as-is and merged into the request context. If a provider retrieves data from an external source (e.g., a vector database or memory service), be aware that a compromised data source could introduce adversarial content designed to manipulate LLM behavior via indirect prompt injection. Implementers should validate and sanitize data retrieved from external sources before returning it.

Constructors

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

Initializes a new instance of the AIContextProvider class.

Properties

Name Description
ProvideInputMessageFilter

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

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).

StoreInputResponseMessageFilter

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

Methods

Name Description
GetService(Type, Object)

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

GetService<TService>(Object)

Asks the AIContextProvider for an object of type TService.

InvokedAsync(AIContextProvider+InvokedContext, CancellationToken)

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

InvokedCoreAsync(AIContextProvider+InvokedContext, CancellationToken)

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

InvokingAsync(AIContextProvider+InvokingContext, CancellationToken)

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

InvokingCoreAsync(AIContextProvider+InvokingContext, CancellationToken)

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

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.

StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken)

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

Applies to