azure-openai-chat-completion-agent
Learn how to use Microsoft Agent Framework with Azure OpenAI ChatCompletion service.
$ npx -y skills add managedcode/dotnet-skills --agent claude-codeHow it fires
How this agent gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
Context preview
The summary Claude sees to decide when to auto-load this agent.
Learn how to use Microsoft Agent Framework with Azure OpenAI ChatCompletion service.
Agent definition
azure-openai-chat-completion-agent.mdtitle: Azure OpenAI ChatCompletion Agents
description: Learn how to use Microsoft Agent Framework with Azure OpenAI ChatCompletion service.
zone_pivot_groups: programming-languages
author: westey-m
ms.topic: tutorial
ms.author: westey
ms.date: 09/24/2025
ms.service: agent-framework
Azure OpenAI ChatCompletion Agents
Microsoft Agent Framework supports creating agents that use the [Azure OpenAI ChatCompletion](/azure/ai-foundry/openai/how-to/chatgpt) service.
::: zone pivot="programming-language-csharp"
Getting Started
Add the required NuGet packages to your project.
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
Create an Azure OpenAI ChatCompletion Agent
As a first step you need to create a client to connect to the Azure OpenAI service.
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;
AzureOpenAIClient client = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com"),
new AzureCliCredential());Azure OpenAI supports multiple services that all provide model calling capabilities. Pick the ChatCompletion service to create a ChatCompletion based agent.
var chatCompletionClient = client.GetChatClient("gpt-4o-mini");Finally, create the agent using the `AsAIAgent` extension method on the `ChatCompletionClient`.
AIAgent agent = chatCompletionClient.AsAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));Agent Features
Function Tools
You can provide custom function tools to Azure OpenAI ChatCompletion agents:
using System;
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
=> $"The weather in {location} is cloudy with a high of 15°C.";
// Create the chat client and agent, and provide the function tool to the agent.
AIAgent agent = new AzureOpenAIClient(
new Uri(endpoint),
new AzureCliCredential())
.GetChatClient(deploymentName)
.AsAIAgent(instructions: "You are a helpful assistant", tools: [AIFunctionFactory.Create(GetWeather)]);
// Non-streaming agent interaction with function tools.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));Streaming Responses
Get responses as they are generated using streaming:
AIAgent agent = chatCompletionClient.AsAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent with streaming support.
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate."))
{
Console.Write(update);
}Using the Agent
The agent is a standard `AIAgent` and supports all standard `AIAgent` operations.
For more information on how to run and interact with agents, see the [Agent getting started tutorials](../../../tutorials/overview.md).
::: zone-end ::: zone pivot="programming-language-python"
Configuration
Environment Variables
Before using Azure OpenAI ChatCompletion agents, you need to set up these environment variables:
export AZURE_OPENAI_ENDPOINT="https://<myresource>.openai.azure.com"
export AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-4o-mini"
Optionally, you can also set:
export AZURE_OPENAI_API_VERSION="2024-10-21" # Default API version
export AZURE_OPENAI_API_KEY="<your-api-key>" # If not using Azure CLI authentication
Installation
Add the Agent Framework package to your project:
pip install agent-framework-core --pre
Getting Started
Authentication
Azure OpenAI agents use Azure credentials for authentication. The simplest approach is to use `AzureCliCredential` after running `az login`:
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
Create an Azure OpenAI ChatCompletion Agent
Basic Agent Creation
The simplest way to create an agent is using the `AzureOpenAIChatClient` with environment variables:
import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())Explicit Configuration
You can also provide configuration explicitly instead of using environment variables:
import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIChatClient(
endpoint="https://<myresource>.openai.azure.com",
deployment_name="gpt-4o-mini",
credential=AzureCliCredential()
).as_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())Agent Features
Function Tools
You can provide custom function tools to Azure OpenAI ChatCompletion agents:
import asyncio
from typing import Annotated
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import
Read more
title: Azure OpenAI ChatCompletion Agents description: Learn how to use Microsoft Agent Framework with Azure OpenAI ChatCompletion service. zone_pivot_groups: programming-languages author: westey-m ms.topic: tutorial ms.author: westey ms.date: 09/24/2025 ms.service: agent-framework
Azure OpenAI ChatCompletion Agents
Microsoft Agent Framework supports creating agents that use the [Azure OpenAI ChatCompletion](/azure/ai-foundry/openai/how-to/chatgpt) service.
::: zone pivot="programming-language-csharp"
Getting Started
Add the required NuGet packages to your project.
dotnet add package Azure.AI.OpenAI --prerelease dotnet add package Azure.Identity dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
Create an Azure OpenAI ChatCompletion Agent
As a first step you need to create a client to connect to the Azure OpenAI service.
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;
AzureOpenAIClient client = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com"),
new AzureCliCredential());Azure OpenAI supports multiple services that all provide model calling capabilities. Pick the ChatCompletion service to create a ChatCompletion based agent.
var chatCompletionClient = client.GetChatClient("gpt-4o-mini");Finally, create the agent using the `AsAIAgent` extension method on the `ChatCompletionClient`.
AIAgent agent = chatCompletionClient.AsAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));Agent Features
Function Tools
You can provide custom function tools to Azure OpenAI ChatCompletion agents:
using System;
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
=> $"The weather in {location} is cloudy with a high of 15°C.";
// Create the chat client and agent, and provide the function tool to the agent.
AIAgent agent = new AzureOpenAIClient(
new Uri(endpoint),
new AzureCliCredential())
.GetChatClient(deploymentName)
.AsAIAgent(instructions: "You are a helpful assistant", tools: [AIFunctionFactory.Create(GetWeather)]);
// Non-streaming agent interaction with function tools.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));Streaming Responses
Get responses as they are generated using streaming:
AIAgent agent = chatCompletionClient.AsAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent with streaming support.
await foreach (var update in agent.RunStreamingAsync("Tell me a joke about a pirate."))
{
Console.Write(update);
}Using the Agent
The agent is a standard `AIAgent` and supports all standard `AIAgent` operations.
For more information on how to run and interact with agents, see the [Agent getting started tutorials](../../../tutorials/overview.md).
::: zone-end ::: zone pivot="programming-language-python"
Configuration
Environment Variables
Before using Azure OpenAI ChatCompletion agents, you need to set up these environment variables:
export AZURE_OPENAI_ENDPOINT="https://<myresource>.openai.azure.com" export AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-4o-mini"
Optionally, you can also set:
export AZURE_OPENAI_API_VERSION="2024-10-21" # Default API version export AZURE_OPENAI_API_KEY="<your-api-key>" # If not using Azure CLI authentication
Installation
Add the Agent Framework package to your project:
pip install agent-framework-core --pre
Getting Started
Authentication
Azure OpenAI agents use Azure credentials for authentication. The simplest approach is to use `AzureCliCredential` after running `az login`:
from azure.identity import AzureCliCredential credential = AzureCliCredential()
Create an Azure OpenAI ChatCompletion Agent
Basic Agent Creation
The simplest way to create an agent is using the `AzureOpenAIChatClient` with environment variables:
import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())Explicit Configuration
You can also provide configuration explicitly instead of using environment variables:
import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIChatClient(
endpoint="https://<myresource>.openai.azure.com",
deployment_name="gpt-4o-mini",
credential=AzureCliCredential()
).as_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())Agent Features
Function Tools
You can provide custom function tools to Azure OpenAI ChatCompletion agents:
import asyncio from typing import Annotated from agent_framework.azure import AzureOpenAIChatClient from azure.identity import
Stop explaining .NET to your AI. Start building. We've all been there: asking Claude to use Entity Framework, only to get EF6 patterns in a .NET 8 project. Explaining to Copilot that Blazor Server and Blazor WebAssembly aren't the same thing.
Repo: managedcode/dotnet-skills
Other agents on dotnet-skills.
- AGENT
Specialist orchestration agent for .NET Aspire work. Use when the problem is clearly about AppHost design, ServiceDefaults, first-party versus CommunityToolkit/Aspire integrations, dashboard and testing, `DistributedApplicationTestingBuilder`, `WebApplicationFactory`
Open agent - agent-as-function-tool
Legacy tutorial alias retained locally; the live Learn URL now resolves into the broader Function Tools surface
Open agent - agent-as-mcp-tool
Learn how to expose an agent as a tool over the MCP protocol
Open agent - create-and-run-durable-agent
Learn how to create and run a durable AI agent with Azure Functions and the durable task extension for Microsoft Agent Framework
Open agent - enable-observability
Enable OpenTelemetry for an agent so agent interactions are automatically logged
Open agent - function-tools-approvals
Learn how to use function tools with human in the loop approvals
Open agent

