Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Dec 10, 2025

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

Cached tokens are currently reported by Anthropic, Gemini, OpenAI, and AWS. Reasoning tokens are currently reported by OpenAI and Gemini.
@stephentoub stephentoub requested a review from a team as a code owner December 10, 2025 03:35
Copilot AI review requested due to automatic review settings December 10, 2025 03:35
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Dec 10, 2025
Copy link
Contributor

Copilot AI left a 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 CachedInputTokenCount and ReasoningTokenCount nullable long properties to UsageDetails class
  • 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

@stephentoub stephentoub merged commit 32f5695 into dotnet:main Dec 12, 2025
12 of 13 checks passed
@stephentoub stephentoub deleted the tokencounts branch December 12, 2025 02:48
stephentoub added a commit to ericstj/extensions that referenced this pull request Dec 12, 2025
Cached tokens are currently reported by Anthropic, Gemini, OpenAI, and AWS. Reasoning tokens are currently reported by OpenAI and Gemini.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consistent key name for cached tokens

3 participants