Use this file to discover all available pages before exploring further.
Agents are the primary abstraction in Microsoft Agent Framework for building AI-powered conversational experiences. An agent combines a language model, optional tools (functions), instructions, and configuration into a reusable component that can process user messages and generate responses.
from agent_framework.azure import AzureOpenAIResponsesClientfrom azure.identity import AzureCliCredentialimport os# Create a clientcredential = AzureCliCredential()client = AzureOpenAIResponsesClient( project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"], credential=credential,)# Create an agentagent = client.as_agent( name="HelloAgent", instructions="You are a friendly assistant. Keep your answers brief.",)# Run the agentresult = await agent.run("What is the capital of France?")print(result.text)
using Azure.AI.OpenAI;using Azure.Identity;using Microsoft.Agents.AI;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";// Create an agentAIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) .GetChatClient(deploymentName) .AsAIAgent( name: "HelloAgent", instructions: "You are a friendly assistant. Keep your answers brief.");// Run the agentvar result = await agent.RunAsync("What is the capital of France?");Console.WriteLine(result.Messages.LastOrDefault()?.Text);
# Get complete response at onceresponse = await agent.run("Hello, how are you?")print(response.text) # Full response textprint(response.messages) # All messages in responseprint(response.usage_details) # Token usage information
The run() method returns an AgentResponse containing:
messages - List of response messages
text - Concatenated text from all messages
response_id - Unique identifier for this response
usage_details - Token usage and cost information
value - Structured output (if using response format)
// Get complete response at oncevar response = await agent.RunAsync("Hello, how are you?");Console.WriteLine(response.Messages.LastOrDefault()?.Text);// Access response detailsforeach (var message in response.Messages){ Console.WriteLine($"{message.Role}: {message.Text}");}
The RunAsync() method returns an AgentResponse containing:
# Stream tokens as they are generatedasync for chunk in agent.run("Tell me a story", stream=True): if chunk.text: print(chunk.text, end="", flush=True)# Or get final response after streamingstream = agent.run("Tell me a story", stream=True)async for chunk in stream: print(chunk.text, end="")final_response = await stream.get_final_response()print(f"\n\nTotal tokens: {final_response.usage_details}")
Streaming returns a ResponseStream that yields AgentResponseUpdate objects and provides get_final_response() to retrieve the complete response.
// Stream tokens as they are generatedawait foreach (var update in agent.RunStreamingAsync("Tell me a story")){ Console.Write(update.Text);}
Streaming returns an IAsyncEnumerable<AgentResponseUpdate> that yields response chunks in real-time.
Instructions define the agent’s behavior, personality, and capabilities:
Python
.NET
agent = client.as_agent( name="CustomerSupportAgent", instructions="""You are a customer support agent. - Be empathetic and professional - Always offer to escalate complex issues - Keep responses under 3 sentences """,)
var agent = client.GetChatClient(deploymentName).AsAIAgent( name: "CustomerSupportAgent", instructions: @"You are a customer support agent. - Be empathetic and professional - Always offer to escalate complex issues - Keep responses under 3 sentences");
Agents can be converted to tools for use by other agents:
# Create a specialized agentresearch_agent = client.as_agent( name="ResearchAgent", description="Performs web research and fact-checking", tools=web_search,)# Convert to a toolresearch_tool = research_agent.as_tool( arg_name="task", arg_description="Research task to perform",)# Use in another agentcoordinator = client.as_agent( name="Coordinator", instructions="Delegate research tasks to the research agent.", tools=research_tool,)# Session propagationresearch_tool = research_agent.as_tool( propagate_session=True # Share session with parent agent)
In .NET, agents can be registered as tools via the function invocation system:
// Agents can be called from other agents through AIFunctionFactoryvar researchAgent = CreateResearchAgent();var coordinatorAgent = client.GetChatClient(deploymentName).AsAIAgent( name: "Coordinator", instructions: "Delegate research tasks appropriately.", tools: [AIFunctionFactory.Create( (string task) => researchAgent.RunAsync(task).Result.Messages.LastOrDefault()?.Text ?? "", name: "ResearchAgent", description: "Performs web research and fact-checking")]);