Skip to main content

How to Add Instructions to Every Agent Run with an AIContextProvider in C#

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

An AIContextProvider lets a Microsoft Agent Framework agent add instructions at the start of every run, worked out at that moment. Its ProvideAIContextAsync returns an AIContext, and the agent adds that context's Instructions to its own for the run. Here it gives an agent today's date, something a model can't know by itself, and the same question shows the difference.

An agent's own instructions are fixed when you create it. Some of what a model needs changes all the time: the date, the signed-in user's name, the opening hours of today. A context provider fills that in, every run, without you passing it each time. Agent Framework's documentation: context providers run around each invocation to add context before execution and process data after execution.

Syntax​

Syntax
sealed class <ProviderName> : AIContextProvider
{
protected override ValueTask<AIContext> ProvideAIContextAsync(
InvokingContext context, CancellationToken cancellationToken) =>
ValueTask.FromResult(new AIContext { Instructions = <instructions> });
}

AIAgent <agent> = <project>.AsAIAgent(new ChatClientAgentOptions
{
ChatOptions = new ChatOptions { ModelId = "<model>", Instructions = "<agent instructions>" },
AIContextProviders = [new <ProviderName>()],
});
PartWhat it is
AIContextProviderThe base class for context providers, in Microsoft.Agents.AI
ProvideAIContextAsyncThe method the agent calls at the start of every run
<instructions>The text to add for this run, worked out now, such as today's date
AIContext.InstructionsThe reference: additional instructions to provide to the AI model for the current invocation
<project>The AIProjectClient of your Microsoft Foundry project, or another chat client
<agent instructions>The agent's own instructions, which apply to every run
AIContextProvidersThe providers the agent calls, in order

The documentation says what the agent does with the result: it merges the messages, tools and instructions returned by ProvideAIContextAsync with the existing ones, and messages, tools and instructions are appended to existing ones. So the provider's text comes after the agent's own instructions, and doesn't replace them.

One rule shapes a provider: the same instance would be used for all sessions, so it shouldn't keep anything about one conversation in its fields. A date is the same for everyone, which makes it a simple first provider.

The Example​

A small agent helps people plan their week, in one sentence. A model doesn't know today's date, so the app asks What day of the week is it today? twice: once of an agent without a provider, once of the same agent with TodayProvider, which adds the current date and time to every run. A second question, How many days are left until Saturday?, needs the date too.

Prerequisites​

  • A model deployment in a Microsoft Foundry project, and the Foundry User role on the Foundry resource for your account. The app calls the model as you.
  • The .NET 10 SDK (dotnet --version prints a version that starts with 10.0) and the Azure CLI, signed in with az login.

Step 1: Create a Console App​

Terminal
dotnet new console -n DayHelper
cd DayHelper
dotnet add package Microsoft.Agents.AI.Foundry --version 1.28.0-preview.261106.1
dotnet add package Azure.Identity --version 1.21.0

Microsoft.Agents.AI.Foundry turns a model in a Foundry project into an agent and brings in Agent Framework, where AIContextProvider lives. It's a preview package: it's the one that ships with Agent Framework 1.28, whose core is generally available. Azure.Identity signs the app in with your Azure CLI sign-in.

Step 2: Write the Provider​

Replace everything in Program.cs. The provider goes at the end of the file:

Program.cs (the provider)
// Adds today's date and the time to the instructions of every run.
sealed class TodayProvider(TimeProvider time) : AIContextProvider
{
protected override ValueTask<AIContext> ProvideAIContextAsync(
InvokingContext context, CancellationToken cancellationToken)
{
DateTimeOffset now = time.GetUtcNow();
string today = now.ToString("dddd, d MMMM yyyy, HH:mm", CultureInfo.InvariantCulture);
return ValueTask.FromResult(new AIContext { Instructions = $"Right now it's {today} UTC." });
}
}
  • TimeProvider is .NET's clock. Passing it in, instead of calling DateTimeOffset.UtcNow inside the provider, lets the app that creates the provider decide which clock it reads.
  • CultureInfo.InvariantCulture writes the day and month in English on every machine.
  • ValueTask.FromResult: nothing here needs to wait, so the method returns a finished result. A provider that reads a database would be async instead.

Step 3: Give It to an Agent​

Program.cs (the agents)
using System.Globalization;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

AIProjectClient project = new(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential());

// 1. An agent without the provider: it has no way to know today's date.
AIAgent plain = project.AsAIAgent(
model: "gpt-5.6-luna", instructions: "You help people plan their week. Answer in one sentence.", name: "DayHelper");
Console.WriteLine($"Without the provider: {await plain.RunAsync("What day of the week is it today?")}");

// 2. The same agent with the provider: every run gets today's date in its instructions.
AIAgent withToday = project.AsAIAgent(new ChatClientAgentOptions
{
Name = "DayHelper",
ChatOptions = new ChatOptions
{
ModelId = "gpt-5.6-luna",
Instructions = "You help people plan their week. Answer in one sentence.",
},
AIContextProviders = [new TodayProvider(TimeProvider.System)],
});
Console.WriteLine($"With the provider: {await withToday.RunAsync("What day of the week is it today?")}");
Console.WriteLine($"With the provider: {await withToday.RunAsync("How many days are left until Saturday?")}");
  • plain is created with AsAIAgent(model:, instructions:, name:), without a provider.
  • withToday is created from ChatClientAgentOptions, the overload that accepts AIContextProviders. Its own instructions are the same as plain's.

Use your own Foundry project endpoint and model deployment.

Step 4: Run It​

Terminal
dotnet run
Output
Without the provider: I can’t determine today’s day of the week without access to the current date.
With the provider: Today is Wednesday.
With the provider: There are 3 days left until Saturday.

macOS-style terminal window titled Terminal — DayHelper showing dotnet run: Without the provider: I can't determine today's day of the week without access to the current date. With the provider: Today is Wednesday. With the provider: There are 3 days left until Saturday.

  • Without the provider, the model says it can't know the day.
  • With the provider, it answers Wednesday, the day I ran it, and works out three days until Saturday in the next run, because that run got the date too.

The Complete File​

Program.cs
using System.Globalization;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

AIProjectClient project = new(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential());

// 1. An agent without the provider: it has no way to know today's date.
AIAgent plain = project.AsAIAgent(
model: "gpt-5.6-luna", instructions: "You help people plan their week. Answer in one sentence.", name: "DayHelper");
Console.WriteLine($"Without the provider: {await plain.RunAsync("What day of the week is it today?")}");

// 2. The same agent with the provider: every run gets today's date in its instructions.
AIAgent withToday = project.AsAIAgent(new ChatClientAgentOptions
{
Name = "DayHelper",
ChatOptions = new ChatOptions
{
ModelId = "gpt-5.6-luna",
Instructions = "You help people plan their week. Answer in one sentence.",
},
AIContextProviders = [new TodayProvider(TimeProvider.System)],
});
Console.WriteLine($"With the provider: {await withToday.RunAsync("What day of the week is it today?")}");
Console.WriteLine($"With the provider: {await withToday.RunAsync("How many days are left until Saturday?")}");

// Adds today's date and the time to the instructions of every run.
sealed class TodayProvider(TimeProvider time) : AIContextProvider
{
protected override ValueTask<AIContext> ProvideAIContextAsync(
InvokingContext context, CancellationToken cancellationToken)
{
DateTimeOffset now = time.GetUtcNow();
string today = now.ToString("dddd, d MMMM yyyy, HH:mm", CultureInfo.InvariantCulture);
return ValueTask.FromResult(new AIContext { Instructions = $"Right now it's {today} UTC." });
}
}

macOS-style code window titled Program.cs — DayHelper showing an AIProjectClient, an agent named DayHelper created without a provider, the same agent created from ChatClientAgentOptions with AIContextProviders set to a new TodayProvider, the two questions, and the TodayProvider class, an AIContextProvider whose ProvideAIContextAsync returns an AIContext with the instructions Right now it's, the date and time, UTC

Common Mistakes​

MistakeWhat happensDo this instead
Putting the date in the agent's own instructionsIt's the date the agent was created, and goes staleAdd it in ProvideAIContextAsync, at the start of each run
Keeping one user's data in a field of the providerOne provider serves every conversation, so the data leaks into othersRead per-user data at the start of each run, from the session or a store
Using AsAIAgent(model:, instructions:) and looking for a providers parameterThat overload has noneUse AsAIAgent(new ChatClientAgentOptions { ... })

FAQ​

What's the difference from run options?​

How to Add Instructions to One Microsoft Agent Framework Agent Run in C# passes extra instructions to one RunAsync call you choose. A context provider adds them to every run, without each call having to remember.

Can the provider add tools as well?​

Yes, through AIContext.Tools: How to Add Tools to Every Agent Run with an AIContextProvider in C# shows it.

Conclusion​

To give an agent instructions that change from run to run, derive a class from AIContextProvider, return them as AIContext.Instructions from ProvideAIContextAsync, and add the provider to ChatClientAgentOptions.AIContextProviders. The agent appends them to its own instructions at the start of every run: an agent that didn't know the day of the week knew it, and used it in the next question too.

Additional Resources​