C# Examples

Hands-on C# code samples using Microsoft Agent Framework RC-1. Each example is self-contained, compilable, and annotated with explanations.

C# only Agent Framework RC-1 Azure OpenAI Ollama Local LM Studio Local
🔒 No API Key Needed Try running your agents locally — free, private, no cloud account required.

Use Ollama or LM Studio to run open-source models on your own hardware. The same Agent Framework code works against any provider.

🤖 Hello Agent Beginner

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.

AIAgent RunAsync Azure OpenAI
View Example →
🔧 Function Tools Beginner

Extend your agent with custom C# functions. The LLM autonomously decides when to invoke them. Uses AIFunctionFactory.Create() from Microsoft.Extensions.AI.

AIFunctionFactory Function Calling Tools
View Example →
💬 Multi-Turn Conversations Intermediate

Build conversational agents that remember context across messages. Pass response.Messages back to maintain the chat history between turns.

AgentResponse ChatHistory Context
View Example →
⚡ Streaming Responses Intermediate

Stream tokens in real time using RunStreamingAsync() and await foreach. Perfect for chatbots and interactive applications where responsiveness matters.

RunStreamingAsync IAsyncEnumerable Streaming
View Example →
📦 Structured Output Intermediate

Get strongly-typed C# objects directly from the LLM using RunAsync<T>(). Ideal for data extraction, classification, and any scenario requiring predictable JSON output.

RunAsync<T> JSON Schema Records
View Example →
🔀 Sequential Workflows Advanced

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.

AgentWorkflow Sequential Orchestration
View Example →
🕸️ Multi-Agent Orchestration Advanced

Use agents as tools for other agents via AsAIFunction(). An orchestrator agent delegates specialised tasks to expert sub-agents automatically.

AsAIFunction Agent-as-Tool Multi-Agent
View Example →
🦙 Ollama — Local AI Intermediate

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.

OllamaSharp Local LLM No API Key
View Example →
🖥️ LM Studio — Local AI Intermediate

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.

OpenAIClient Local LLM Custom Endpoint
View Example →
🧠 Agent Memory Advanced

Persist conversation history across separate sessions using ChatHistoryMemoryProvider and a vector store. The agent recalls facts from past conversations — even in brand-new sessions.

AgentSession VectorStore Long-Term Memory
View Example →
🔍 RAG — Retrieval-Augmented Generation Advanced

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.

TextSearchProvider VectorStore Knowledge Base
View Example →
🔌 MCP Tools Advanced

Connect your agent to any Model Context Protocol server — GitHub, filesystem, databases and more — using McpClient and StdioClientTransport from the official .NET MCP SDK.

McpClient MCP External Tools
View Example →
📊 OpenTelemetry Advanced

Instrument your agents with distributed traces, custom metrics, and structured logging using .UseOpenTelemetry(). Visualise everything in the .NET Aspire Dashboard or Application Insights.

UseOpenTelemetry Tracing Aspire
View Example →
🎧 Customer Support Triage Real-World

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.

Router Pattern Triage AsAIFunction()
View Example →
🔬 Research & Report Pipeline Real-World

A sequential pipeline: ResearchAgent gathers sources, FactCheckerAgent verifies claims, WriterAgent produces a polished article. Each stage builds on the previous.

Pipeline Pattern Sequential Agents Quality Gates
View Example →
🤔 Tools vs Sub-Agents Guide

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.

Decision Guide Patterns Anti-Patterns
Read Guide →

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