Agent Framework OpenTelemetry tracing in C# with Claude A developer demonstrated how Microsoft Agent Framework 1.24.0 emits OpenTelemetry spans for agents and workflows in C#, with one span per model call, tool call and workflow step carrying token counts. Using a kitchen-assistant agent and a planner-to-reviewer workflow on Claude Haiku 5.5, the sample prints traces to the console via a plain dotnet run or renders them as a tree in the Aspire dashboard, and converts token counts into dollar costs using a self-maintained price table. The code depends only on IChatClient, so the provider can be swapped for OpenAI or Ollama. The idea: a trace shows what happened inside a run, one span per model call, tool call and workflow step, with the token counts on each. The framework counts tokens. Turning them into money takes a price table that you keep yourself. Ask the kitchen assistant "What can I cook tonight?" and it answers with a frittata. The answer doesn't show that it took two model calls and a tool call, that the first call already sent 410 input tokens for a three-line instruction and a five-word question, or that the second call took about two seconds. A trace does. Microsoft Agent Framework writes OpenTelemetry spans for agents and workflows. A plain dotnet run prints them in the terminal, and the Aspire dashboard shows them as a tree. Below, the kitchen assistant and the planner-to-reviewer workflow from the first workflow post https://kootstra.io/writing/agent-framework-workflow-planner-reviewer/ are traced, the messages appear in the spans only in development, and the token counts become a cost in dollars. Versions: Microsoft Agent Framework 1.24.0, Aspire 13.6.5, OpenTelemetry 1.19.1 SDK, console and OTLP exporters , .NET 10. Code: samples/agent-framework/12-observability https://github.com/nkootstra/samples/tree/main/agent-framework/12-observability , a runnable version of this post with its tests. Who this is for: C developers who have an agent with tools or a workflow running, as in Agent Framework tool calling in C with Claude https://kootstra.io/writing/tool-calling-ai-agents-csharp/ and Agent Framework workflows in C : planner, then reviewer https://kootstra.io/writing/agent-framework-workflow-planner-reviewer/ . Note The code runs on Claude Haiku 5.5, but it depends only on IChatClient : the first post https://kootstra.io/writing/microsoft-agent-framework-first-csharp-agent/ swap-the-provider-by-changing-one-statement shows the swap to OpenAI or Ollama. The prices and the cache token counts follow Anthropic's price list and connector; another provider needs its own prices and may report cached tokens under another key. The output limit of 4,096 tokens is sized for Haiku, which counts its reasoning as output. A console app with the agent, Anthropic and workflow packages, plus the OpenTelemetry SDK and two exporters. The console exporter prints every span and metric in the terminal. The OTLP exporter sends them to anything that speaks the OpenTelemetry protocol, the Aspire dashboard included: dotnet new console -n KitchenTraces cd KitchenTraces dotnet add package Microsoft.Agents.AI --version 1.24.0 dotnet add package Microsoft.Agents.AI.Anthropic --version 1.24.0 dotnet add package Microsoft.Agents.AI.Workflows --version 1.24.0 dotnet add package OpenTelemetry --version 1.19.1 dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.19.1 dotnet add package OpenTelemetry.Exporter.Console --version 1.19.1 Libraries write spans to an ActivitySource , which has a name. Nothing is recorded for a source until something subscribes to that name. In OpenTelemetry that is the tracer provider, and each AddSource call adds one name: Program.cs using System.ComponentModel; using System.Globalization; using Anthropic; using Anthropic.Core; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Workflows; using Microsoft.Extensions.AI; using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; const string SourceName = "Kitchen"; // Aspire sets the endpoint. A plain dotnet run doesn't, so the // spans and metrics go to the console instead. bool hasCollector = string.IsNullOrEmpty Environment.GetEnvironmentVariable "OTEL EXPORTER OTLP ENDPOINT" ; TracerProviderBuilder tracerBuilder = Sdk.CreateTracerProviderBuilder .AddSource SourceName .AddSource "Microsoft.Agents.AI.Workflows" ; using TracerProvider tracing = hasCollector ? tracerBuilder.AddOtlpExporter .Build : tracerBuilder.AddConsoleExporter .Build ; MeterProviderBuilder meterBuilder = Sdk.CreateMeterProviderBuilder .AddMeter SourceName ; using MeterProvider metrics = hasCollector ? meterBuilder.AddOtlpExporter .Build : meterBuilder.AddConsoleExporter .Build ; Kitchen is the app's own name for its agent spans; the agents below are told to use it. Microsoft.Agents.AI.Workflows is the name the workflow spans use. The meter provider does the same for metrics. Which exporter runs depends on one environment variable. AddOtlpExporter without arguments takes its endpoint and protocol from the standard OTEL EXPORTER OTLP variables, which Aspire sets further down. A plain dotnet run doesn't set them, so the spans and metrics go to the terminal. The OTLP exporter sends in batches on a background thread, so the using declarations matter in a console app: disposing the providers when the program ends flushes the spans that are still waiting. The chat client is Claude Haiku 5.5, as in the other parts, with an output limit of 4,096 tokens because the reviewer below reasons before it answers. A small local function wraps any agent in telemetry: bool isDevelopment = Environment.GetEnvironmentVariable "DOTNET ENVIRONMENT" == "Development"; var apiKey = Environment.GetEnvironmentVariable "ANTHROPIC API KEY" ?? throw new InvalidOperationException "Set ANTHROPIC API KEY." ; IChatClient chatClient = new AnthropicClient new ClientOptions { ApiKey = apiKey } .AsIChatClient "claude-haiku-5-5", 4096 ; AIAgent Traced AIAgent agent = agent .AsBuilder .UseOpenTelemetry SourceName, configure: otel = otel.EnableSensitiveData = isDevelopment .Build ; UseOpenTelemetry wraps the agent in an OpenTelemetryAgent , which writes an invoke agent span for every run. It also switches on telemetry inside the agent's own chat pipeline, below the FunctionInvokingChatClient that runs the tools, under the same source name. That gives a chat span for every model call and an execute tool span for every tool call, nested under the agent's span, without instrumenting the chat client yourself. If the chat client already carries OpenTelemetry instrumentation of its own, the agent leaves it alone and adds no second set of chat spans. The source name is optional, and leaving it out is a common reason for an empty dashboard. Without one, the spans go to a source named Experimental.Microsoft.Agents.AI , which neither AddSource "Microsoft.Agents.AI" nor AddSource "Microsoft.Agents.AI " matches. Passing the same constant to UseOpenTelemetry and AddSource avoids the guesswork. The agent is the kitchen assistant from the tools post https://kootstra.io/writing/tool-calling-ai-agents-csharp/ , with one simpler tool, list pantry , instead of the two pantry tools, and shorter instructions: AIAgent kitchen = Traced chatClient.AsAIAgent name: "kitchen-assistant", instructions: """ You help one household decide what to cook. Check the pantry before you suggest a dish. Answer in at most three sentences. """, tools: AIFunctionFactory.Create ListPantry, name: "list pantry" ; AgentResponse answer = await kitchen.RunAsync "What can I cook tonight?" ; Console.WriteLine answer.Text ; The tool is a static local function at the end of Program.cs : Description "Lists what is in the household's pantry." static string ListPantry = "rice, eggs, onions, spinach, feta"; dotnet run , with the key in ANTHROPIC API KEY , prints each span when it ends. A child ends before its parent, so the chat and execute tool spans come first and the agent's invoke agent span after them. One run prints 28 spans and about 900 lines, most of them the workflow's spans from further down. Filtered to the span names and token counts, the kitchen assistant's part reads: Activity.DisplayName: chat claude-haiku-5-5 gen ai.usage.input tokens: 410 gen ai.usage.output tokens: 28 Activity.DisplayName: execute tool list pantry Activity.DisplayName: chat claude-haiku-5-5 gen ai.usage.input tokens: 461 gen ai.usage.output tokens: 150 Activity.DisplayName: invoke agent kitchen-assistant 4ef44cf1aa4b4ae489ba5caa5718c06d gen ai.usage.input tokens: 871 gen ai.usage.output tokens: 178 The filter is grep -E 'DisplayName|usage\. in|out put tokens' . Two model calls with a tool call between them, and the agent span adds their tokens up. The first chat span in full: Activity.TraceId: c2bb31d567c3b1dc002405709252d3df Activity.SpanId: adf6d6fe538ffc24 Activity.TraceFlags: Recorded Activity.ParentSpanId: 5c2c875c0230768e Activity.DisplayName: chat claude-haiku-5-5 Activity.Kind: Client Activity.StartTime: 2026-10-15T10:09:02.6574430Z Activity.Duration: 00:00:00.9885410 Activity.Tags: gen ai.operation.name: chat gen ai.request.model: claude-haiku-5-5 gen ai.provider.name: anthropic server.address: api.anthropic.com server.port: 443 gen ai.tool.definitions: { "type": "function", "name": "list pantry" } gen ai.response.finish reasons: "tool calls" gen ai.response.id: msg 011Cg3qehCvcSHFgNQvzMU9M gen ai.response.model: claude-haiku-5-5 gen ai.usage.input tokens: 410 gen ai.usage.output tokens: 28 gen ai.usage.cache read.input tokens: 0 Instrumentation scope ActivitySource : Name: Kitchen Resource associated with Activity: telemetry.sdk.name: opentelemetry telemetry.sdk.language: dotnet telemetry.sdk.version: 1.19.1 service.name: unknown service:KitchenTraces Schema URL: https://opentelemetry.io/schemas/1.44.0 ParentSpanId is how the spans fit together: both chat spans and the execute tool span have the agent span as their parent. The finish reason tool calls says the model stopped to ask for a tool, and the second chat span carries the answer. The service name is unknown service , because nothing in a plain dotnet run sets one; Aspire does, below. Model output varies from run to run, so the counts in the sections that follow, from a run under Aspire, differ slightly from these. The terminal shows every span, but as a flat list. An Aspire AppHost starts the app, hands it the API key and runs the dashboard that receives its telemetry. It needs the Aspire CLI, which installs with one command from aspire.dev https://aspire.dev the samples repository's prerequisites https://github.com/nkootstra/samples/blob/main/docs/prerequisites.md list it for macOS, Linux and Windows ; this post uses Aspire 13.6.5. Then, from the folder above KitchenTraces , install the project templates of the same version, create the AppHost and give it a reference to the app: cd .. dotnet new install Aspire.ProjectTemplates::13.6.5 dotnet new aspire-apphost -n KitchenTraces.AppHost dotnet add KitchenTraces.AppHost reference KitchenTraces The reference is where Projects.KitchenTraces below comes from: the AppHost gets a generated class for every project it references. The template's AppHost.cs only builds and runs; this one adds the app: AppHost.cs js var builder = DistributedApplication.CreateBuilder args ; var apiKey = builder.AddParameter "anthropic-api-key", secret: true ; builder.AddProject