Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In Microsoft Agent Framework, agent middleware wraps an agent run, while ChatClient middleware wraps calls to an IChatClient. Use agent middleware for run-wide behavior such as handling inputs, outputs, or control flow. Use ChatClient middleware when you need to inspect or change the messages, options, or response at the model-call boundary. Tool-specific behavior belongs in function-calling middleware.

Where each middleware layer sits

A simplified request path for an IChatClient-backed agent is:

Agent middleware → agent execution → ChatClient middleware → IChatClient → model response

Function-calling middleware handles tool or function invocations during agent execution. Microsoft describes middleware handlers of the same type as a chain: a handler calls the next handler to continue through the pipeline. See Microsoft Learn’s Agent Middleware and Agent Pipeline Architecture documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Layer What it wraps Best fit
Agent middleware Agent execution, including run-level inputs, outputs, and control flow Logging or validating the overall run; applying run-wide behavior or changing the final result
ChatClient middleware Calls from an agent to an IChatClient, including messages and options sent and the response received Logging or transforming model requests and responses
Function-calling middleware Function or tool calls made during agent execution Validating tool arguments, transforming results, or controlling tool execution

When to choose agent middleware

Choose agent middleware when the policy or behavior applies to the agent run as a whole, rather than only to a model request. It can inspect or modify run inputs and outputs and participate in execution flow. Examples include run-level validation, shared context handling, and overriding a result. Its precise behavior depends on what the particular agent implementation supports.

Microsoft’s Agent Middleware and Adding Middleware pages describe middleware use cases such as guardrails, exception handling, result overrides, shared state, and runtime context.

When to choose ChatClient middleware

Choose ChatClient middleware when your logic needs to work with the conversation sent to the model or the model’s returned response. This layer operates at the IChatClient boundary, where middleware can inspect messages and options, call the inner client, and inspect or transform its response.

This boundary is particularly useful for model-call logging and request or response transformations. It is narrower than agent middleware: it does not, by itself, represent the entire agent run or tool-execution lifecycle.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check whether the agent uses an IChatClient

ChatClient middleware requires an IChatClient-based path. A ChatClientAgent uses an IChatClient, so its client can be decorated before it is supplied to the agent. Microsoft’s Agent Pipeline Architecture documentation also describes specialized or remote agents that communicate with remote services instead of using a local IChatClient. Those agents may still support agent middleware, but ChatClient middleware cannot run where there is no IChatClient in the path. The Agent concepts documentation explains the relationship between ChatClientAgent and IChatClient.

Keep tool policies at the function-calling boundary

If the logic concerns a tool invocation rather than the agent run or model exchange, use function-calling middleware. That is the relevant layer for checking tool arguments, altering tool results, or controlling whether a tool call proceeds. Applying such logic only at the model-call boundary would target a different part of the pipeline.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Middleware ordering and language-specific APIs

Handlers of the same middleware type form a chain. Each handler needs to call the next handler when it should continue processing, so ordering can affect how requests and responses pass through multiple handlers. Check the relevant language-specific Microsoft documentation for the API and registration pattern; examples for C#, Python, or Go are not interchangeable by default.

The cited documentation explains middleware responsibilities and pipeline placement, but does not establish that either layer is generally faster. Performance depends on the workload and implementation; choose the boundary based on what the middleware needs to see, then benchmark the actual application if latency or throughput is a concern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.