Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
dotnet-build-and-test / paths-filter (push) Waiting to run
dotnet-build-and-test / dotnet-build-and-test (Debug, windows-latest, net9.0) (push) Blocked by required conditions
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, ubuntu-latest, net10.0) (push) Blocked by required conditions
dotnet-build-and-test / dotnet-build-and-test (Release, integration, true, windows-latest, net472) (push) Blocked by required conditions
dotnet-build-and-test / dotnet-build-and-test (Release, ubuntu-latest, net8.0) (push) Blocked by required conditions
dotnet-build-and-test / dotnet-build-and-test-check (push) Blocked by required conditions
Agent Middleware
This sample demonstrates how to add middleware to intercept:
- Chat client calls (global and per‑request)
- Agent runs (guardrails and PII filtering)
- Function calling (logging/override)
What This Sample Shows
- Azure OpenAI integration via
AzureOpenAIClientandAzureCliCredential - Chat client middleware using
ChatClientBuilder.Use(...) - Agent run middleware (PII redaction and wording guardrails)
- Function invocation middleware (logging and overriding a tool result)
- Per‑request chat client middleware
- Per‑request function pipeline with approval
- Combining agent‑level and per‑request middleware
Function Invocation Middleware
Not all agents support function invocation middleware.
Attempting to use function middleware on agents that do not wrap a ChatClientAgent or derives from it will throw an InvalidOperationException.
Prerequisites
- Environment variables:
AZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpointAZURE_OPENAI_DEPLOYMENT_NAME: Chat deployment name (optional; defaults togpt-4o)
- Sign in with Azure CLI (PowerShell):
az login
Running the Sample
Use PowerShell:
cd dotnet/samples/GettingStarted/Agents/Agent_Step14_Middleware
dotnet run