Propagate CachedInputTokenCount in OpenTelemetry telemetry#7234
Merged
stephentoub merged 4 commits intomainfrom Jan 27, 2026
Merged
Propagate CachedInputTokenCount in OpenTelemetry telemetry#7234stephentoub merged 4 commits intomainfrom
stephentoub merged 4 commits intomainfrom
Conversation
…lient Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update OpenTelemetryChatClient for new CachedInputTokenCount
Propagate CachedInputTokenCount in OpenTelemetry telemetry
Jan 27, 2026
stephentoub
approved these changes
Jan 27, 2026
stephentoub
reviewed
Jan 27, 2026
stephentoub
reviewed
Jan 27, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request adds support for tracking cached input tokens in OpenTelemetry telemetry following the OpenTelemetry semantic conventions. The changes propagate UsageDetails.CachedInputTokenCount through both histogram metrics and activity span tags in the OpenTelemetryChatClient.
Changes:
- Added OpenTelemetry constants for cache_read token type and corresponding semantic convention attribute name
- Updated OpenTelemetryChatClient to record cached input tokens in histogram metrics and activity span tags
- Enhanced tests to verify the new cached token telemetry functionality
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs | Added TokenTypeCacheRead ("cache_read") constant and CacheReadInputTokens ("gen_ai.usage.cache_read_input_tokens") attribute name following OpenTelemetry semantic conventions |
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs | Added histogram recording for cached input tokens with token type tag and activity span tag using the new semantic convention attribute, following the same pattern as input/output tokens |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs | Added CachedInputTokenCount = 5 to test usage details and corresponding assertion for the activity span tag in both streaming and non-streaming test scenarios |
…ic conventions Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
auto-merge was automatically disabled
January 27, 2026 14:19
Head branch was pushed to by a user without write access
…defined in semantic conventions Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
stephentoub
approved these changes
Jan 27, 2026
tarekgh
approved these changes
Jan 27, 2026
This was referenced Feb 16, 2026
Merged
Bump Microsoft.Extensions.Http.Resilience from 10.0.0 to 10.3.0
dotnet-presentations/ai-workshop#339
Closed
Bump Microsoft.Extensions.Http.Resilience from 10.2.0 to 10.3.0
dotnet-presentations/ai-workshop#342
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OpenTelemetryConsts.csto add new constant:CacheReadInputTokens= "gen_ai.usage.cache_read.input_tokens" for activity span tagOpenTelemetryChatClient.csto:CachedInputTokenCountas an activity span tag usinggen_ai.usage.cache_read.input_tokensOpenTelemetryChatClientTests.cs:CachedInputTokenCount = 5to test usage details for both sync and streaming responsesgen_ai.usage.cache_read.input_tokens= 5 in activity tagsSummary
This PR adds support for propagating
CachedInputTokenCountthrough OpenTelemetry telemetry as an activity span tag usinggen_ai.usage.cache_read.input_tokensas defined in the OpenTelemetry semantic conventions registry.Note: The histogram metric
gen_ai.client.token.usageonly supports "input" and "output" token types per the semantic conventions, so cached tokens are only recorded as a span attribute, not as a separate histogram entry.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Microsoft Reviewers: Open in CodeFlow