Build Your First AI Agent Done
Initialize an incident triage agent and connect it to a large language model provider.
Overview
Imagine you’re Jordan Miller, a senior on-call engineer, and a flood of alerts just hit your inbox. You need an agent that can instantly summarize the chaos and suggest a severity level. This guide walks you through building that agent. Using the Microsoft Agent Framework, you’ll create a triage assistant that connects to your infrastructure alerts and provides immediate, streamed analysis to help you respond faster.
What is an AI Agent?
An AI Agent is more than just a chatbot. While a chatbot responds to messages, an Agent is a goal-oriented entity that uses reasoning to decide which actions to take to accomplish a task.
Jordan’s on-call identity.
Reasoning about alerts.
External capabilities.
State and history.
Exposing as a service.
The Power of Streaming
In a critical incident, every second counts. Traditional AI interactions wait for the entire response to be generated before showing it to you. By using Streaming, Jordan’s assistant provides immediate feedback, allowing you to begin reading the analysis while the model is still “thinking” about the next steps.
Setup your environment
Before we write any code, let’s make sure your environment is ready.
📋 Pre-flight Checklist
- 🛠️ .NET 10.0 SDK (or later) installed.
- 🤖 AI Provider: Access to Azure OpenAI or a locally hosted OpenAI-compatible service (like Ollama/LM Studio).
- 🔑 Authentication: An active Azure session (
az login) or required API keys.
1 Create the project
Open your terminal and create a new console application:
dotnet new console -n IncidentTriage.HelloAgent -f net10.0
cd IncidentTriage.HelloAgent
2 Install the Agent Framework
Next, install the core framework and the provider SDK for your chosen model.
dotnet add package Microsoft.Agents.AI.OpenAI
dotnet add package OpenAI
dotnet restoreMicrosoft.Agents.AI.OpenAIThe core bridge that connects the Agent Framework to OpenAI-compatible models.
OpenAIThe official SDK for standard OpenAI REST API interactions and streaming.
dotnet add package Microsoft.Agents.AI.OpenAI
dotnet add package Azure.AI.OpenAI
dotnet add package Azure.Identity
dotnet restoreMicrosoft.Agents.AI.OpenAIThe core bridge that connects the Agent Framework to OpenAI-compatible models.
Azure.AI.OpenAIThe official SDK for accessing GPT-4o and text embeddings within Azure.
Azure.IdentityEnables secure, passwordless authentication using Azure CLI or Managed Identity.
3 Configure your provider
Finally, set the environment variables for your chosen provider. In your terminal, use export (Bash/macOS) or $env:VAR="value" (PowerShell/Windows).
Required Variables:
OPENAI_ENDPOINT="http://localhost:1234/v1"OPENAI_MODEL_NAME="<your-model-name>"
🖥️ LM Studio Quick Start
- Download and open LM Studio.
- Search for and download a model (e.g.,
google/gemma-4-e4b). - Go to the Local Server tab and click Start Server.
- Ensure the endpoint matches:
http://localhost:1234/v1.
Using Ollama? Use
http://localhost:11434/v1as your endpoint and ensure you have runollama pull <model>first.
Required Variables:
AZURE_OPENAI_ENDPOINT="https://<your-resource>.openai.azure.com/"AZURE_OPENAI_DEPLOYMENT_NAME="<your-deployment>"
Troubleshooting:
- If authentication fails, ensure you are signed in (
az login) with the correct tenant.- If you receive a model not found error, confirm your deployment name is correct.
Build the agent
Building an agent involves three main parts: identifying the model provider, defining the agent’s persona, and executing the task.
sequenceDiagram
participant App as Console App
participant Agent as 🎭 Persona
participant Brain as 🧠 Brain
App->>Agent: RunAsync(incident)
Agent->>Brain: Persona + User Input
Brain-->>Agent: AI Response
Agent-->>App: Triage Result
Note over App,Brain: Streaming Flow
App->>Agent: RunStreamingAsync(incident)
Agent->>Brain: Stream Request
Brain-->>App: (Token-by-Token Updates)
This diagram illustrates the interaction flow we are about to implement. We will follow this pattern in three clear steps: configuring the agent’s persona, providing the incident data, and finally, executing the triage.
Open Program.cs in your editor and let’s build it piece by piece.
1 Initialize the Provider and Persona 🎭 Persona 🧠 Brain
First, we set up the connection to the AI model.
- The Provider: We pull endpoints and model names from environment variables to keep infrastructure details out of our source code.
- The Persona: We use
instructionsto define the agent’s Persona. This is known as the System Prompt, and it defines how the agent should behave globally.
Paste the initialization code for your chosen provider into Program.cs:
using OpenAI;
using System.ClientModel;
using Microsoft.Agents.AI;
using OpenAI.Chat;
// 1. Pull configuration from environment (or use local defaults)
var endpoint = Environment.GetEnvironmentVariable("OPENAI_ENDPOINT")
?? "http://localhost:1234/v1";
var modelName = Environment.GetEnvironmentVariable("OPENAI_MODEL_NAME")
?? "google/gemma-4-e4b";
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? "dummy-key";
// 2. Build the client and "imbue" the agent with a persona
var chatClient = new OpenAIClient(new ApiKeyCredential(apiKey), new OpenAIClientOptions { Endpoint = new Uri(endpoint) })
.GetChatClient(modelName);
AIAgent agent = chatClient.AsAIAgent(new ChatClientAgentOptions
{
Name = "TriageAgent",
ChatOptions = new()
{
Instructions = """
You are an enterprise incident triage assistant.
Summarize the incident, identify likely severity,
and suggest the next investigation step.
Keep answers concise and operational.
"""
}
}); using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
// 1. Pull configuration from environment
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException(
"AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME")
?? throw new InvalidOperationException(
"AZURE_OPENAI_DEPLOYMENT_NAME is not set.");
// 2. Build the client and "imbue" the agent with a persona
var chatClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.GetChatClient(deploymentName);
AIAgent agent = chatClient.AsAIAgent(new ChatClientAgentOptions
{
Name = "TriageAgent",
ChatOptions = new()
{
Instructions = """
You are an enterprise incident triage assistant.
Summarize the incident, identify likely severity,
and suggest the next investigation step.
Keep answers concise and operational.
"""
}
}); 2 Define the Task
Next, we define the User Input. This is the specific data or query you want the agent to process—in this case, an active incident alert.
Append the incident data to Program.cs:
var incident = """
Checkout latency is above threshold in West Europe.
The alert started 12 minutes ago and customer support is reporting failed payment attempts.
""";
3 Run and Stream the Response
Finally, we engage the Brain by invoking the agent. We will run it twice: first to receive a complete response, and then again using Streaming. Streaming prints characters as they are generated by the model, providing a much more responsive experience for the user.
Append the execution logic to the bottom of Program.cs:
Console.WriteLine("Full response:");
Console.WriteLine(await agent.RunAsync(incident));
Console.WriteLine("\nStreaming response:");
await foreach (var update in agent.RunStreamingAsync(incident))
{
Console.Write(update);
}
Console.WriteLine();
With everything in place, execute the application from your terminal:
dotnet run
You will see the full response printed first, followed immediately by a second response that streams character by character as it arrives from the model service.
Try it
To see how the agent adapts, try modifying the variables in Program.cs and running the app again. Choose an experiment below to see how a small change impacts the agent’s behavior:
Refine the Format
Agents are highly sensitive to formatting constraints. Update the instructions parameter in your AsAIAgent builder:
instructions: """
You are an enterprise incident triage assistant.
Summarize the incident and identify likely severity.
Provide your response as a concise, bulleted checklist for an on-call engineer.
""",Result: The agent will shift from conversational prose to a structured, operational summary.
Test New Data
Verify the agent’s reasoning by swapping the incident string for a different failure:
var incident = """
CRITICAL: Database connection timeout in the East US region.
Affecting 15% of login attempts. Latency is spiking.
""";Result: The agent will pivot its severity assessment and suggest database-specific steps like checking connection pools.
Shift the Tone
An agent’s behavior is dictated by its persona. Change the instructions to a Senior SRE:
instructions: """
You are a senior Site Reliability Engineer (SRE).
Be extremely technical and focus on infrastructure-level root causes.
""",Result: The tone shifts to “technical expert,” with next steps focusing on load balancers, logs, and system metrics.
Summary and Next Steps
You learned how to securely configure and connect an Agent Framework agent to an LLM provider, and successfully built a minimal incident triage agent that processes static prompts and streams its responses.
Right now, our agent is disconnected from reality. If we ask it about a specific server outage or a database error, it will guess an answer based on its general training data, because it cannot actually see the current status of our enterprise systems. An incident triage agent isn’t very helpful if it cannot look up real incident details.
In the next tutorial, we will solve this by giving our agent Tools. We will create a plugin that connects to an external API, allowing our agent to fetch real-time ticket information and provide grounded, accurate triage responses.