-
Notifications
You must be signed in to change notification settings - Fork 850
Augment UsageDetails with cached / reasoning token counts #7122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Cached tokens are currently reported by Anthropic, Gemini, OpenAI, and AWS. Reasoning tokens are currently reported by OpenAI and Gemini.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds first-class support for cached and reasoning token counts to the UsageDetails API by promoting them from AdditionalCounts to dedicated properties. These token counts are commonly reported by major AI providers including Anthropic, Gemini, OpenAI, and AWS, making this promotion valuable for API consistency and cost tracking.
Key Changes
- Added
CachedInputTokenCountandReasoningTokenCountnullable long properties toUsageDetailsclass - Updated OpenAI client implementations to populate these properties directly from provider token usage details
- Migrated existing AdditionalCounts entries for cached/reasoning tokens to the new dedicated properties in OpenAI integrations
- Added comprehensive test coverage for the new properties including serialization, addition operations, and null handling
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI.Abstractions/UsageDetails.cs | Added CachedInputTokenCount and ReasoningTokenCount properties with XML documentation, updated Add() method to sum them, and added debugger display support |
| src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.json | Added API baseline entries marking both new properties as "Stable" |
| src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs | Updated FromOpenAIUsage() to populate new properties from InputTokenDetails.CachedTokenCount and OutputTokenDetails.ReasoningTokenCount, removed these entries from AdditionalCounts |
| src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs | Updated ToUsageDetails() to map InputTokenDetails.CachedTokenCount and OutputTokenDetails.ReasoningTokenCount to new properties |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/UsageDetailsTests.cs | New comprehensive test file covering constructor defaults, property roundtrips, Add() behavior with various combinations including nulls, and JSON serialization |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/UsageContentTests.cs | Updated serialization test to include CachedInputTokenCount and ReasoningTokenCount |
| test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIChatClientTests.cs | Updated multiple test methods to assert new properties are correctly populated and removed corresponding AdditionalCounts assertions |
| test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIResponseClientTests.cs | Updated ResponseWithUsageDetails_ParsesTokenCounts test to verify new properties and assert AdditionalCounts is null when only cached/reasoning tokens are present |
Cached tokens are currently reported by Anthropic, Gemini, OpenAI, and AWS. Reasoning tokens are currently reported by OpenAI and Gemini.
Cached tokens are currently reported by Anthropic, Gemini, OpenAI, and AWS. Reasoning tokens are currently reported by OpenAI and Gemini.
Closes #7101
Microsoft Reviewers: Open in CodeFlow