openai-responses-agent
Learn how to use Microsoft Agent Framework with OpenAI Responses 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 OpenAI Responses service.
Agent definition
openai-responses-agent.mdtitle: OpenAI Responses Agents
description: Learn how to use Microsoft Agent Framework with OpenAI Responses service.
zone_pivot_groups: programming-languages
author: westey-m
ms.topic: tutorial
ms.author: westey
ms.date: 09/24/2025
ms.service: agent-framework
OpenAI Responses Agents
Microsoft Agent Framework supports creating agents that use the [OpenAI responses](https://platform.openai.com/docs/api-reference/responses/create) service.
::: zone pivot="programming-language-csharp"
Getting Started
Add the required NuGet packages to your project.
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
Create an OpenAI Responses Agent
As a first step you need to create a client to connect to the OpenAI service.
using System;
using Microsoft.Agents.AI;
using OpenAI;
OpenAIClient client = new OpenAIClient("<your_api_key>");OpenAI supports multiple services that all provide model-calling capabilities. Pick the Responses service to create a Responses based agent.
#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
var responseClient = client.GetOpenAIResponseClient("gpt-4o-mini");
#pragma warning restore OPENAI001Finally, create the agent using the `AsAIAgent` extension method on the `ResponseClient`.
AIAgent agent = responseClient.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."));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"
Prerequisites
Install the Microsoft Agent Framework package.
pip install agent-framework-core --pre
Configuration
Environment Variables
Set up the required environment variables for OpenAI authentication:
# Required for OpenAI API access
OPENAI_API_KEY="your-openai-api-key"
OPENAI_RESPONSES_MODEL_ID="gpt-4o" # or your preferred Responses-compatible model
Alternatively, you can use a `.env` file in your project root:
OPENAI_API_KEY=your-openai-api-key
OPENAI_RESPONSES_MODEL_ID=gpt-4o
Getting Started
Import the required classes from Agent Framework:
import asyncio
from agent_framework import ChatAgent
from agent_framework.openai import OpenAIResponsesClient
Create an OpenAI Responses Agent
Basic Agent Creation
The simplest way to create a responses agent:
async def basic_example():
# Create an agent using OpenAI Responses
agent = OpenAIResponsesClient().as_agent(
name="WeatherBot",
instructions="You are a helpful weather assistant.",
)
result = await agent.run("What's a good way to check the weather?")
print(result.text)Using Explicit Configuration
You can provide explicit configuration instead of relying on environment variables:
async def explicit_config_example():
agent = OpenAIResponsesClient(
ai_model_id="gpt-4o",
api_key="your-api-key-here",
).as_agent(
instructions="You are a helpful assistant.",
)
result = await agent.run("Tell me about AI.")
print(result.text)Basic Usage Patterns
Streaming Responses
Get responses as they are generated for better user experience:
async def streaming_example():
agent = OpenAIResponsesClient().as_agent(
instructions="You are a creative storyteller.",
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Tell me a short story about AI."):
if chunk.text:
print(chunk.text, end="", flush=True)
print() # New line after streamingAgent Features
Reasoning Models
Use advanced reasoning capabilities with models like GPT-5:
from agent_framework import HostedCodeInterpreterTool, TextContent, TextReasoningContent
async def reasoning_example():
agent = OpenAIResponsesClient(ai_model_id="gpt-5").as_agent(
name="MathTutor",
instructions="You are a personal math tutor. When asked a math question, "
"write and run code to answer the question.",
tools=HostedCodeInterpreterTool(),
default_options={"reasoning": {"effort": "high", "summary": "detailed"}},
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Solve: 3x + 11 = 14"):
if chunk.contents:
for content in chunk.contents:
if isinstance(content, TextReasoningContent):
# Reasoning content in gray text
print(f"\033[97m{content.text}\033[0m", end="", flush=True)
elif isinstance(content, TextContent):
print(content.text, end="", flush=True)
print()Structured Output
Get responses in structured formats:
from pydantic import BaseModel
from agent_framework import AgentResponse
class CityInfo(BaseModel):
"""A structured output for city information."""
city: str
description: str
async def structured_output_example():
agent = OpenAIResponsesClient().as_agent(
name="CityExpert",
instructions="You describe cities in a structured format.",
)
# Non-streaming structured output
result = await agent.run("Tell me about Paris, France", options={"response_format": CityInfo})
if result.value:
city_data = result.value
print(f"City: {city_data.city}")
print(f"Description: {city_data.description}")
# Streaming structured output
structured_result = await AgentRunResponse.from_agent_response_generator(
agent.run_stream("TRead more
title: OpenAI Responses Agents description: Learn how to use Microsoft Agent Framework with OpenAI Responses service. zone_pivot_groups: programming-languages author: westey-m ms.topic: tutorial ms.author: westey ms.date: 09/24/2025 ms.service: agent-framework
OpenAI Responses Agents
Microsoft Agent Framework supports creating agents that use the [OpenAI responses](https://platform.openai.com/docs/api-reference/responses/create) service.
::: zone pivot="programming-language-csharp"
Getting Started
Add the required NuGet packages to your project.
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
Create an OpenAI Responses Agent
As a first step you need to create a client to connect to the OpenAI service.
using System;
using Microsoft.Agents.AI;
using OpenAI;
OpenAIClient client = new OpenAIClient("<your_api_key>");OpenAI supports multiple services that all provide model-calling capabilities. Pick the Responses service to create a Responses based agent.
#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
var responseClient = client.GetOpenAIResponseClient("gpt-4o-mini");
#pragma warning restore OPENAI001Finally, create the agent using the `AsAIAgent` extension method on the `ResponseClient`.
AIAgent agent = responseClient.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."));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"
Prerequisites
Install the Microsoft Agent Framework package.
pip install agent-framework-core --pre
Configuration
Environment Variables
Set up the required environment variables for OpenAI authentication:
# Required for OpenAI API access OPENAI_API_KEY="your-openai-api-key" OPENAI_RESPONSES_MODEL_ID="gpt-4o" # or your preferred Responses-compatible model
Alternatively, you can use a `.env` file in your project root:
OPENAI_API_KEY=your-openai-api-key OPENAI_RESPONSES_MODEL_ID=gpt-4o
Getting Started
Import the required classes from Agent Framework:
import asyncio from agent_framework import ChatAgent from agent_framework.openai import OpenAIResponsesClient
Create an OpenAI Responses Agent
Basic Agent Creation
The simplest way to create a responses agent:
async def basic_example():
# Create an agent using OpenAI Responses
agent = OpenAIResponsesClient().as_agent(
name="WeatherBot",
instructions="You are a helpful weather assistant.",
)
result = await agent.run("What's a good way to check the weather?")
print(result.text)Using Explicit Configuration
You can provide explicit configuration instead of relying on environment variables:
async def explicit_config_example():
agent = OpenAIResponsesClient(
ai_model_id="gpt-4o",
api_key="your-api-key-here",
).as_agent(
instructions="You are a helpful assistant.",
)
result = await agent.run("Tell me about AI.")
print(result.text)Basic Usage Patterns
Streaming Responses
Get responses as they are generated for better user experience:
async def streaming_example():
agent = OpenAIResponsesClient().as_agent(
instructions="You are a creative storyteller.",
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Tell me a short story about AI."):
if chunk.text:
print(chunk.text, end="", flush=True)
print() # New line after streamingAgent Features
Reasoning Models
Use advanced reasoning capabilities with models like GPT-5:
from agent_framework import HostedCodeInterpreterTool, TextContent, TextReasoningContent
async def reasoning_example():
agent = OpenAIResponsesClient(ai_model_id="gpt-5").as_agent(
name="MathTutor",
instructions="You are a personal math tutor. When asked a math question, "
"write and run code to answer the question.",
tools=HostedCodeInterpreterTool(),
default_options={"reasoning": {"effort": "high", "summary": "detailed"}},
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Solve: 3x + 11 = 14"):
if chunk.contents:
for content in chunk.contents:
if isinstance(content, TextReasoningContent):
# Reasoning content in gray text
print(f"\033[97m{content.text}\033[0m", end="", flush=True)
elif isinstance(content, TextContent):
print(content.text, end="", flush=True)
print()Structured Output
Get responses in structured formats:
from pydantic import BaseModel
from agent_framework import AgentResponse
class CityInfo(BaseModel):
"""A structured output for city information."""
city: str
description: str
async def structured_output_example():
agent = OpenAIResponsesClient().as_agent(
name="CityExpert",
instructions="You describe cities in a structured format.",
)
# Non-streaming structured output
result = await agent.run("Tell me about Paris, France", options={"response_format": CityInfo})
if result.value:
city_data = result.value
print(f"City: {city_data.city}")
print(f"Description: {city_data.description}")
# Streaming structured output
structured_result = await AgentRunResponse.from_agent_response_generator(
agent.run_stream("TStop 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

