C# Examples
Hands-on C# code samples using Microsoft Agent Framework RC-1. Each example is self-contained, compilable, and annotated with explanations.
Use Ollama or LM Studio to run open-source models on your own hardware. The same Agent Framework code works against any provider.
Create your very first AI agent using AsAIAgent(). Understand the
AIAgent abstraction, how to set instructions, and how to call
RunAsync() to get a response.
Extend your agent with custom C# functions. The LLM autonomously decides when to
invoke them. Uses AIFunctionFactory.Create() from Microsoft.Extensions.AI.
Build conversational agents that remember context across messages. Pass
response.Messages back to maintain the chat history between turns.
Stream tokens in real time using RunStreamingAsync() and
await foreach. Perfect for chatbots and interactive applications
where responsiveness matters.
Get strongly-typed C# objects directly from the LLM using
RunAsync<T>(). Ideal for data extraction, classification,
and any scenario requiring predictable JSON output.
Chain multiple agents into a sequential workflow using
AgentWorkflow. Each agent's output becomes the next agent's input —
great for research-then-write pipelines.
Use agents as tools for other agents via AsAIFunction(). An
orchestrator agent delegates specialised tasks to expert sub-agents automatically.
Run agents entirely on your own machine using OllamaApiClient from
OllamaSharp. No cloud, no API key, no data leaving your network. Supports streaming,
function tools, and multi-turn conversations.
Connect to any GGUF model running in LM Studio via its OpenAI-compatible
/v1 endpoint. Uses the standard OpenAIClient — just
point it at localhost:1234. No API key, no cloud.
Persist conversation history across separate sessions using
ChatHistoryMemoryProvider and a vector store. The agent recalls facts
from past conversations — even in brand-new sessions.
Ground your agent's answers in your own documents. Uses
TextSearchProvider to semantically search a vector store before every
model call and inject the most relevant results as context.
Connect your agent to any Model Context Protocol server — GitHub, filesystem,
databases and more — using McpClient and StdioClientTransport
from the official .NET MCP SDK.
Instrument your agents with distributed traces, custom metrics, and structured
logging using .UseOpenTelemetry(). Visualise everything in the
.NET Aspire Dashboard or Application Insights.
A routing orchestrator classifies incoming tickets and delegates to specialist
sub-agents: BillingAgent, TechnicalAgent, and AccountAgent —
each with their own tools and focused system prompts.
A sequential pipeline: ResearchAgent gathers sources, FactCheckerAgent
verifies claims, WriterAgent produces a polished article. Each stage builds on the previous.
The essential architectural decision guide: when to use a C# function tool vs a full LLM sub-agent. Includes decision table, side-by-side code, pattern catalog, and anti-patterns.
Ready to dive in?
Start with the Hello Agent example and work your way through progressively more advanced topics.
Start with Hello Agent Learn More About Agent Framework