Tuesday, April 14, 2026

Microsoft Agent Frameworks' Agent Middleware with .NET

In this post, let's have a look at Microsoft Agent Framework's Agent Middleware with .NET. In my previous post, we looked at getting started with Microsoft Agent Framework in .NET by building an agent with tools. In this post, let's see how we can use middleware to intercept and inspect agent runs, function calls, and chat client calls.
 
Microsoft Agent Framework supports three types of middleware.
  • Agent Run Middleware: Allows interception of all agent runs, so that input and output can be inspected and/or modified as needed.
  • Function Calling Middleware: Allows interception of all function calls executed by the agent, so that input and output can be inspected and modified as needed.
  • Chat Client Middleware: Allows interception of calls to an IChatClient implementation, where an agent is using IChatClient  for inference calls, for example, when using ChatClientAgent.
Let's see how we can set up each of these middleware types.

The middleware is registered using the builder pattern. Agent Run Middleware and Function Calling Middleware are applied to the AIAgent via AsBuilder()Chat Client Middleware is applied to the IChatClient  instance via the clientFactory parameter.
AIAgent agent = new AIProjectClient(new Uri(endpoint), credential)
    .AsAIAgent(
        model: deploymentName,
        name: "weekend-planner",
        instructions: """
            You help users plan their weekends and choose the best activities for the given weather.
            If an activity would be unpleasant in weather, don't suggest it.
            Include date of the weekend in response.
            """,
        tools: [
            AIFunctionFactory.Create(GetWeather),
            AIFunctionFactory.Create(GetActivities),
            AIFunctionFactory.Create(GetCurrentDate)
        ],
        clientFactory: (chatClient) => chatClient
            .AsBuilder()
            // Chat Client Middleware
            .Use(getResponseFunc: CustomChatClientMiddleware, getStreamingResponseFunc: CustomChatClientStreamlingMiddleware)
            .Build())
    .AsBuilder()
    // Agent Run Middleware
    .Use(runFunc: CustomAgentRunMiddleware, runStreamingFunc: CustomAgentRunStreamingMiddleware)
    // Function Calling Middleware
    .Use(CustomFunctionCallingMiddleware)
    .Build();


Agent Run Middleware


The Agent Run Middleware intercepts all agent runs. Here we are logging the message count before and after the agent run. The middleware calls agent.RunAsync() to invoke the next middleware in the chain.
async Task<AgentResponse> CustomAgentRunMiddleware(IEnumerable<ChatMessage> messages,
    AgentSession? session,
    AgentRunOptions? options,
    AIAgent agent,
    CancellationToken cancellationToken)
{
    Console.WriteLine($"[AgentRun] Message Count: '{messages.Count()}'.");

    AgentResponse response = await agent.RunAsync(messages, session, options, cancellationToken)
        .ConfigureAwait(false);

    Console.WriteLine($"[AgentRun] Response Message Count: '{response.Messages.Count}'.");

    return response;
}
It also supports a streaming variant using IAsyncEnumerable<AgentResponseUpdate>.
async IAsyncEnumerable<AgentResponseUpdate> CustomAgentRunStreamingMiddleware(IEnumerable<ChatMessage> messages,
    AgentSession? session,
    AgentRunOptions? options,
    AIAgent agent,
    [EnumeratorCancellation] CancellationToken cancellationToken)
{
    Console.WriteLine($"[AgentRunStreaming] Message Count: '{messages.Count()}'.");

    List<AgentResponseUpdate> updates = [];
    await foreach (AgentResponseUpdate update in agent.RunStreamingAsync(messages, session, options, cancellationToken))
    {
        updates.Add(update);
        yield return update;
    }

    Console.WriteLine($"[AgentRunStreaming] Response Message Count: '{updates.ToAgentResponse().Messages.Count}'.");
}

Function Calling Middleware


The Function Calling Middleware intercepts all function calls executed by the agent. Here we are logging the function name being called and the result returned. The middleware uses the next delegate to invoke the next middleware in the chain.
async ValueTask<object?> CustomFunctionCallingMiddleware(
    AIAgent agent,
    FunctionInvocationContext context,
    Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next,
    CancellationToken cancellationToken)
{
    Console.WriteLine($"      [FunctionCall] Calling: '{context!.Function.Name}'.");

    object? result = await next(context, cancellationToken);

    Console.WriteLine($"      [FunctionCall] Result: '{context!.Function.Name}' = '<OMITTED>'.");

    return result;
}

Chat Client Middleware


The Chat Client Middleware intercepts calls to IChatClient . This is useful when you want to inspect the raw messages being sent to and received from the underlying LLM. Here we are logging all messages with their content types, distinguishing between TextContent, FunctionCallContent, and FunctionResultContent.
async Task<ChatResponse> CustomChatClientMiddleware(IEnumerable<ChatMessage> messages,
    ChatOptions? options,
    IChatClient client,
    CancellationToken token)
{
    LogChatClientMessages("Messages", messages);

    ChatResponse response = await client.GetResponseAsync(messages, options, token)
        .ConfigureAwait(false);

    LogChatClientMessages("Response", response.Messages);

    return response;
}
And the streaming variant.
async IAsyncEnumerable<ChatResponseUpdate> CustomChatClientStreamlingMiddleware(IEnumerable<ChatMessage> messages,
    ChatOptions? options,
    IChatClient client,
    [EnumeratorCancellation] CancellationToken token)
{
    LogChatClientMessages("Messages", messages);

    List<ChatResponseUpdate> updates = [];
    await foreach (ChatResponseUpdate update in client.GetStreamingResponseAsync(messages, options, token))
    {
        updates.Add(update);
        yield return update;
    }

    LogChatClientMessages("Response", updates.ToChatResponse().Messages);
}
The helper method LogChatClientMessages logs the messages with their content types.
void LogChatClientMessages(string label, IEnumerable<ChatMessage> messages)
{
    Console.WriteLine();
    Console.WriteLine($"   [ChatClient] {label}:");
    foreach (ChatMessage message in messages)
    {
        foreach (AIContent content in message.Contents)
        {
            string detail = content switch
            {
                TextContent text => text.Text,
                FunctionCallContent fc => $"Call: {fc.Name}({string.Join(", ", fc.Arguments?.Select(a => $"{a.Key}={a.Value}") ?? [])})",
                FunctionResultContent fr => $"Result: {fr.CallId} = '<OMITTED>'",
                _ => content.GetType().Name
            };
            Console.WriteLine($"      [{message.Role}] {detail}");
        }
    }
}
When I run this, I could see something like below:
Agent Middleware Output
Complete source code:
   https://github.com/jaliyaudagedara/maf-samples/blob/main/dotnet/samples/02-agent-middleware/Program.cs

Hope this helps.

More read:

Happy Coding.

Regards,
Jaliya

Wednesday, April 8, 2026

Getting Started with Microsoft Agent Framework 1.0.0 in .NET

Microsoft Agent Framework 1.0.0 was released few days ago with support for both .NET and Python. Agent Framework is the direct successor to both Semantic Kernel and AutoGen and includes many features such as Persistence, Monitoring, Humans in the Loop etc.

In this post, let's see how we can create a simple agent with multiple tools in .NET.

I have created a Console App and the first step is adding the new Microsoft.Agents.AI.Foundry package.
dotnet add package Microsoft.Agents.AI.Foundry --version 1.0.0
An AI Agent uses an LLM to run tools in a loop to achieve it's goal. So first let's define some tools that our agent can use. In Agent Framework, tools are plain C# methods decorated with [Description] attributes. The framework automatically generates the tool schema for the LLM from the attribute.
[Description("Returns weather data for a given city, including temperature (in Celsius) and description.")]
static WeatherResult GetWeather(
    [Description("The city to get the weather for.")] string city)
{
    Console.WriteLine($"[Tool] Getting weather for '{city}'.");

    return new WeatherResult(18, "Rainy");
}

[Description("Returns a list of leisure activities for a given city and date, each with a name and location.")]
static List<LeisureActivity> GetActivities(
    [Description("The city to get activities for.")] string city,
    [Description("The date to get activities for in format YYYY-MM-DD.")] string date)
{
    Console.WriteLine($"[Tool] Getting activities for '{city}' on '{date}'.");

    return
    [
        new("Hiking", city),
        new("Beach", city),
        new("Museum", city)
    ];
}

[Description("Gets the current date from the system and returns as a string in format YYYY-MM-DD.")]
static string GetCurrentDate()
{
    Console.WriteLine("[Tool] Getting current date.");

    return DateTime.Now.ToString("yyyy-MM-dd");
}

record WeatherResult(int Temperature, string Description);

record LeisureActivity(string Name, string Location);
Note: these are mock implementations returning hardcoded data, but in a real application, these tools would call actual APIs. Here we are using typed records (WeatherResultLeisureActivity) instead of raw JSON strings. The framework serializes these to JSON automatically. The [Description] attributes on the methods should describe the return shape since the framework only generates input parameter schemas, the LLM won't see the return type otherwise (.NET [Feature]: Consider including return type schema in generated tool definitions)

Now let's create an agent that uses these tools.
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using System.ComponentModel;

string endpoint = "https://<FOUNDRY_RESOURCE>.services.ai.azure.com/";
string deploymentName = "<DEPLOYMENT_NAME>";

// For local development, using AzureCliCredential
var credential = new AzureCliCredential();

AIAgent agent = new AIProjectClient(new Uri(endpoint), credential)
    .AsAIAgent(
        model: deploymentName,
        name: "weekend-planner",
        instructions: """
            You help users plan their weekends and choose the best activities for the given weather.
            If an activity would be unpleasant in weather, don't suggest it.
            Include date of the weekend in response.
            """,
        tools: [
            AIFunctionFactory.Create(GetWeather),
            AIFunctionFactory.Create(GetActivities),
            AIFunctionFactory.Create(GetCurrentDate)
        ]);

string userInput = "What should I do this weekend in Auckland?";
Console.WriteLine(await agent.RunAsync(userInput));
The key things to note here:
  • AsAIAgent() is an extension method on AIProjectClient provided by the Microsoft.Agents.AI.Foundry package.
  • AIFunctionFactory.Create() inspects the method's [Description] attributes and parameter types to automatically generate the tool schema that gets sent to the LLM. The LLM then decides which tools to call and with what arguments.
  • RunAsync() handles the entire tool calling loop for you. It sends the prompt, processes tool call requests from the LLM, invokes the matching local functions, sends results back, and returns the final response.
Now when I run this, I can see something like this.
Output
Isn't it nice.

Tuesday, April 7, 2026

Azure Content Understanding: Custom Usage Tracking with APIM

I had a requirement to track LLM and Content Understanding token usage within a multi-tenant application for downstream customer billing, rather than relying solely on Application Insights.

Thought of using AI gateway in Azure API Management in front of Azure OpenAI / Foundry endpoints.

Specifically:
  • Expose AI endpoints via APIM (e.g., Language Model APIs / Foundry)
  • Use policies such as llm-emit-token-metric  (but this seems tightly coupled to App Insights). 
  • Worst case: custom policies to intercept responses, capture token usage metadata (prompt, completion, total tokens) and emit usage events to Event Hub from APIM via log-to-eventhub
    • Then process these events via a worker to persist usage records to our billing datastore.
Believed this is a common requirement, but couldn't find any better solution. Proceeded with custom policies.

Thought of giving it a try with Content Understanding first as it felt a bit challenging.

Didn't go through the AI gateway in Azure API Management path, instead just added a REST API to APIM.
Create an HTTP API
Then added 2 Operations POST: /* and GET: /*.
{
    "openapi": "3.0.1",
    "info": {
        "title": "Test Foundry API",
        "description": "",
        "version": "1.0"
    },
    "servers": [{
        "url": "https://<SOME_APIM>.com/test-foundry-api"
    }],
    "paths": {
        "/{*path}": {
            "post": {
                "summary": "POST",
                "operationId": "post",
                "parameters": [{
                    "name": "*path",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": ""
                    }
                }],
                "responses": {
                    "200": {
                        "description": ""
                    }
                }
            },
            "get": {
                "summary": "GET",
                "operationId": "get",
                "parameters": [{
                    "name": "*path",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": ""
                    }
                }],
                "responses": {
                    "200": {
                        "description": ""
                    }
                }
            }
        }
    },
    "components": {
        "securitySchemes": {
            "apiKeyHeader": {
                "type": "apiKey",
                "name": "Ocp-Apim-Subscription-Key",
                "in": "header"
            },
            "apiKeyQuery": {
                "type": "apiKey",
                "name": "subscription-key",
                "in": "query"
            }
        }
    },
    "security": [{
        "apiKeyHeader": []
    }, {
        "apiKeyQuery": []
    }]
}
Now the most important part. Added the following All Operations policy. Here instead of sending messages to Event Hub, I am sending to Service Bus using send-service-bus-message (Sending messages to Azure Service Bus from Azure API Management) for testing purposes.
<policies>
    <inbound>
        <base />
        <set-variable name="tenantId" value="@(context.Request.Headers.GetValueOrDefault("x-tenant-id", "unknown"))" />
        <set-backend-service base-url="https://<SOME_FOUNDRY>.services.ai.azure.com" />
    </inbound>
    <backend>
        <forward-request buffer-request-body="true" />
    </backend>
    <outbound>
        <base />
        <set-header name="Operation-Location" exists-action="override">
            <value>@{
                var location = context.Response.Headers.GetValueOrDefault("Operation-Location", "");
                if (string.IsNullOrEmpty(location)) 
                {
                    return location;
                }
                
                var uri = new Uri(location);
                var req = context.Request.OriginalUrl;
                return req.Scheme + "://" + req.Host + "/" + context.Api.Path + uri.PathAndQuery;
            }</value>
        </set-header>
        <choose>
            <when condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 300)">
                <set-variable name="body" value="@(context.Response.Body.As<string>(preserveContent: true))" />
                <choose>
                    <when condition="@{
                        var text = (string)context.Variables["body"];
                        if (string.IsNullOrEmpty(text) || !text.TrimStart().StartsWith("{"))
                        {
                            return false;
                        }

                        var json = Newtonsoft.Json.Linq.JObject.Parse(text);
                        var statusToken = json["status"];
                        var status = statusToken == null ? string.Empty : ((string)statusToken).ToLowerInvariant();

                        return status == "succeeded" || status == "completed" || status == "failed";
                    }">
                        <send-service-bus-message 
                          topic-name="sbt-test-usage-tracking" 
                          namespace="<SOME_SERVICEBUS_NAMESPACE>.servicebus.windows.net" 
                          client-id="<SOME_MANAGED_IDENTITY_CLIENT_ID>">
                            <payload>@{
                                var json = Newtonsoft.Json.Linq.JObject.Parse((string)context.Variables["body"]);
                                var operationIdToken = json["id"];
                                var analyzerIdToken = json["result"]?["analyzerId"];
                                var statusToken = json["status"];

                                return new Newtonsoft.Json.Linq.JObject(
                                    new Newtonsoft.Json.Linq.JProperty("tenantId", (string)context.Variables["tenantId"]),
                                    new Newtonsoft.Json.Linq.JProperty("eventType", "cu-analysis-completed"),
                                    new Newtonsoft.Json.Linq.JProperty("requestId", context.RequestId.ToString()),
                                    new Newtonsoft.Json.Linq.JProperty("operationId", operationIdToken == null ? string.Empty : (string)operationIdToken),
                                    new Newtonsoft.Json.Linq.JProperty("analyzerId", analyzerIdToken == null ? string.Empty : (string)analyzerIdToken),
                                    new Newtonsoft.Json.Linq.JProperty("status", statusToken == null ? string.Empty : (string)statusToken),
                                    new Newtonsoft.Json.Linq.JProperty("usage", json["usage"] ?? new Newtonsoft.Json.Linq.JObject()),
                                    new Newtonsoft.Json.Linq.JProperty("timestamp", DateTime.UtcNow.ToString("o"))
                                ).ToString();
                            }</payload>
                        </send-service-bus-message>
                    </when>
                </choose>
            </when>
        </choose>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Important points:
  • Forward Request: buffer-request-body="true": Needed for binary PDF forwarding
  • Header Operation-Location Rewrite: Routes SDK polling back through APIM so the outbound policy fires
Then used the Azure Content Understanding .NET Client (Azure Content Understanding Client Library for .NET) to trigger an analysis and polled for the result.
// NOTE: Endpoint is now APIM    
string endpoint = "https://<SOME_APIM>.com/test-foundry-api";

ContentUnderstandingClientOptions contentUnderstandingClientOptions = new();
contentUnderstandingClientOptions.AddPolicy(new TenantHeaderPolicy("<SOME_TENANT_ID>"), HttpPipelinePosition.PerCall);

ContentUnderstandingClient contentUnderstandingClient =
    new(new Uri(endpoint), new DefaultAzureCredential(), contentUnderstandingClientOptions);

// TODO: Trigger analysis and poll
// REFER:
https://jaliyaudagedara.blogspot.com/2026/03/azure-content-understanding-client.html sealed class TenantHeaderPolicy(string tenantId) : HttpPipelineSynchronousPolicy { public override void OnSendingRequest(HttpMessage message) { Console.WriteLine($"Calling: {message.Request.Method} {message.Request.Uri}"); message.Request.Headers.SetValue("x-tenant-id", tenantId); message.Request.Headers.SetValue("Ocp-Apim-Trace", "true"); } }
Looked promising.
Service Bus Message
I still don't know whether there is a better option. But this seems to be doing what's required.

Hope this helps.

Happy Coding.

Regards,
Jaliya