How to See a Microsoft Agent Framework Agent's Token Usage in the Aspire Dashboard
An agent wrapped with UseOpenTelemetry records how many tokens its model calls use, as the metric gen_ai.client.token.usage, and the Aspire Dashboard charts it. Here three runs of a small agent show up there, with numbers that look wrong at first: 64 and 256, when the runs used 31 and 118 to 150 tokens. They're not wrong, though. They're bucket boundaries.
Traces show one run at a time. Metrics are numbers recorded over many runs, such as how long each call took or how many tokens it used. In Microsoft Agent Framework, the same UseOpenTelemetry call that creates an agent's spans records its metrics too: its documentation lists Usage statistics and token consumption metrics among what it adds.
What's left is to collect them. Spans need a tracer provider; metrics need a meter provider, subscribed to the right meter.
The Example
A console app called HaikuWriter has an agent with no tools that writes a haiku about a topic. The app runs it three times, for monsoon rain, morning chai and a quiet library, prints the token counts each run reports, and sends the agent's metrics to the Aspire Dashboard, a local viewer for OpenTelemetry data.
Prerequisites
- A Microsoft Foundry project with a model deployment, and the Foundry User role on it.
- The Azure CLI, signed in with
az login. - The .NET 10 SDK.
- The Aspire Dashboard, running on your computer. It receives OTLP at
http://localhost:4317and shows its pages athttp://localhost:18888, the ports it uses by default.
Step 1: Create a Console App and Add the Packages
dotnet new console -n HaikuWriter
cd HaikuWriter
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/HaikuWriter/HaikuWriter.csproj:
Restore succeeded.
dotnet new console creates a console app in a new folder, HaikuWriter, and cd moves into it. Then add three packages:
dotnet add package Microsoft.Agents.AI.Foundry --version 1.28.0-preview.261106.1
dotnet add package Azure.Identity --version 1.21.0
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.21.0
Each command adds a package reference and restores the project. The first one ends like this:
info : PackageReference for package 'Microsoft.Agents.AI.Foundry' version '1.28.0-preview.261106.1' added to file '/Users/you/source/HaikuWriter/HaikuWriter.csproj'.
info : Generating MSBuild file /Users/you/source/HaikuWriter/obj/HaikuWriter.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/you/source/HaikuWriter/obj/project.assets.json
log : Restored /Users/you/source/HaikuWriter/HaikuWriter.csproj (in 1.51 sec).
Microsoft.Agents.AI.Foundryconnects Agent Framework to Microsoft Foundry and brings Agent Framework 1.28.0 with it. It's a preview package, as its version says.Azure.Identitygives the appAzureCliCredential, to sign in to Foundry as your Azure CLI account.OpenTelemetry.Exporter.OpenTelemetryProtocolis the OTLP exporter, and brings the OpenTelemetry SDK with it.
The project file now lists all three:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.21.0" />
<PackageReference Include="Microsoft.Agents.AI.Foundry" Version="1.28.0-preview.261106.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.21.0" />
</ItemGroup>
</Project>
Step 2: Collect the Agent's Metrics
Replace everything in Program.cs with a meter provider:
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
// 1. Collect the agent's metrics and send them over OTLP (http://localhost:4317 by default).
using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(resource => resource.AddService("HaikuWriter"))
.AddMeter("HaikuWriter")
.AddOtlpExporter()
.Build();
Sdk.CreateMeterProviderBuilder()starts a meter provider, the part of OpenTelemetry that collects metrics, as a tracer provider collects spans.AddMeter("HaikuWriter")subscribes to the meter namedHaikuWriter, the name the agent gets in the next step. Its reference: Adds given Meter names to the list of subscribed meters. On a meter provider, it'sAddMeter, notAddSource.AddOtlpExporter()sends the metrics over OTLP tolocalhost:4317, where the dashboard receives them.usingmatters more than it looks here, because the app ends right after its last run. OpenTelemetry's guidance: If you forget to dispose theMeterProviderinstance before the application ends, metrics might get dropped due to the lack of proper flush.
Step 3: Create the Agent with OpenTelemetry Around It
Add the agent below it, with your own project endpoint and model deployment:
// 2. An agent with OpenTelemetry around it. The meter has the same name as the source.
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(
model: "gpt-5.6-luna",
instructions: "You write one haiku about the topic you're given. Reply with the haiku only.",
name: "HaikuWriter")
.AsBuilder()
.UseOpenTelemetry(sourceName: "HaikuWriter")
.Build();
AsAIAgent(...)creates the agent from the Foundry project: the model deployment, the instructions and a name.UseOpenTelemetry(sourceName: "HaikuWriter")records the agent's telemetry under the nameHaikuWriter, and the dashboard in Step 5 shows its metrics under a meter of that name. That's whyAddMeteruses the same one.
This app has no tracer provider, so it collects only the metrics.
Step 4: Run the Agent Three Times
Add the rest:
// 3. Three runs, with the token counts each run reports, to compare with the dashboard.
foreach (string topic in new[] { "monsoon rain", "morning chai", "a quiet library" })
{
AgentResponse response = await agent.RunAsync(topic);
Console.WriteLine($"{topic}: {response.Usage?.InputTokenCount} input tokens, {response.Usage?.OutputTokenCount} output tokens");
}
RunAsync(topic)runs the agent with the topic as the message.response.Usageis the run's resource usage information, in its documentation's words, withInputTokenCountandOutputTokenCount. It'snullwhen usage information isn't available, hence the?..
Then run it:
dotnet run
monsoon rain: 31 input tokens, 150 output tokens
morning chai: 31 input tokens, 131 output tokens
a quiet library: 31 input tokens, 118 output tokens

Each run read 31 input tokens and wrote between 118 and 150. These exact counts are what to compare the dashboard with.
Step 5: Open the Metric in the Dashboard
Open the dashboard at http://localhost:18888, select Metrics, the HaikuWriter resource, and the instrument gen_ai.client.token.usage:

The meter HaikuWriter has two instruments, and this is the token usage one: Measures number of input and output tokens used. The graph has three lines, P50, P90 and P99 Tokens, over the window set in Duration, here Last 5 minutes. Where the runs happened, P50 rises to 64 and P90 to 256. P99 is hidden behind P90, because they're the same.
Step 6: Read the Values in the Table
Select Table above the graph:

One row, for the time of the runs: P50 64, P90 256 and P99 256. Under it, the Filters list the metric's dimensions: gen_ai.operation.name is chat, gen_ai.provider.name is microsoft.foundry, the request and response models, and gen_ai.token.type, with the values input and output. The percentiles above cover both token types together.
None of the runs used 64 or 256 tokens, so where do they come from?
Why the Dashboard Says 64 and 256
gen_ai.client.token.usage is a histogram. A histogram doesn't keep each value it's given; it counts how many values fell into each range, or bucket, between fixed boundaries. OpenTelemetry's GenAI conventions give the boundaries for this metric: This metric SHOULD be specified with ExplicitBucketBoundaries of [1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864]. Microsoft.Extensions.AI 10.11.0, the library that records the metric under Agent Framework 1.28.0, uses those same boundaries.
So the runs' counts landed in two buckets:
| Recorded values | Bucket |
|---|---|
| Each run's 31 input tokens | above 16, up to 64 |
| The runs' 150, 131 and 118 output tokens | above 64, up to 256 |
Half the values are in each. To show a percentile, the dashboard's code adds up the buckets' counts from the smallest and returns the upper boundary of the bucket where the running total reaches that share of all values. For P50, the first bucket already holds half, so the answer is 64. P90 and P99 need more than half, which only the next bucket reaches, so both are 256.
That's what a histogram gives you: where values fall, not the values themselves. For exact counts, use the numbers each run reports, as the console does with response.Usage, or the gen_ai.usage.input_tokens and gen_ai.usage.output_tokens attributes on each chat span (How to Trace a Microsoft Agent Framework Agent with OpenTelemetry in C#).
OpenTelemetry's newest GenAI conventions make the same point about their token histograms: They should not be used for total usage or cost calculations. Use histograms exclusively to monitor usage percentiles (e.g. p95/p99) and detect outliers.
The Complete File
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
// 1. Collect the agent's metrics and send them over OTLP (http://localhost:4317 by default).
using MeterProvider meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(resource => resource.AddService("HaikuWriter"))
.AddMeter("HaikuWriter")
.AddOtlpExporter()
.Build();
// 2. An agent with OpenTelemetry around it. The meter has the same name as the source.
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(
model: "gpt-5.6-luna",
instructions: "You write one haiku about the topic you're given. Reply with the haiku only.",
name: "HaikuWriter")
.AsBuilder()
.UseOpenTelemetry(sourceName: "HaikuWriter")
.Build();
// 3. Three runs, with the token counts each run reports, to compare with the dashboard.
foreach (string topic in new[] { "monsoon rain", "morning chai", "a quiet library" })
{
AgentResponse response = await agent.RunAsync(topic);
Console.WriteLine($"{topic}: {response.Usage?.InputTokenCount} input tokens, {response.Usage?.OutputTokenCount} output tokens");
}

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Adding only a tracer provider | No metrics: OpenTelemetry's guide lists a meter not added to the MeterProvider as a cause of missing metrics | Add a meter provider with AddMeter |
A different name in AddMeter than in UseOpenTelemetry | The provider doesn't subscribe to the agent's meter | Use the same name in both |
| Reading P50 or P90 as a token count | They're bucket boundaries: 64 and 256 here, for runs of 31 and 118 to 150 tokens | Take exact counts from Usage or the chat spans |
| Letting the app end without disposing the meter provider | The last measurements might get dropped | Keep the using |
FAQ
Can I see the input and output tokens separately?
Yes, with the filters. Aspire's documentation: Under the chart, there's a list of filters you can apply to focus on the data that interests you. The gen_ai.token.type row holds both values, input and output, and with both selected, as here, the percentiles mix the two.
Will the metric keep its name?
gen_ai.client.token.usage is the name in the conventions Microsoft.Extensions.AI 10.11.0 follows, version 1.41. The GenAI conventions have since moved to their own repository, where token usage is split into new counters and per-operation histograms, named gen_ai.client.inference.usage.* and gen_ai.client.inference.operation.*. Check the instrument list again after you upgrade.
Conclusion
To see a Microsoft Agent Framework agent's token usage in the Aspire Dashboard, wrap the agent with UseOpenTelemetry(sourceName: ...) and add a meter provider with AddMeter and the same name. The metric is gen_ai.client.token.usage, a histogram, and the dashboard shows its P50, P90 and P99 as bucket boundaries: 64 and 256 for HaikuWriter's runs of 31 input and 118 to 150 output tokens. Read the shape from the dashboard, and the exact counts from Usage or the chat spans.
Additional Resources
- Agent Framework observability —
UseOpenTelemetry, source names and exporting metrics. - Semantic conventions for generative AI metrics, version 1.41.0 —
gen_ai.client.token.usageand its bucket boundaries. - Explore the Aspire Dashboard — the Metrics page, its graph, table and filters.
- ChartDataCalculator.cs in Aspire 17.0.0 — how the dashboard turns bucket counts into P50, P90 and P99.
- OpenTelemetry .NET metrics best practices — the meter provider's lifetime and missing metrics.
