Skip to content

Add cache_write_tokens mapping to UsageExtractor#330

Open
ali-corpo wants to merge 1 commit intopydantic:mainfrom
ali-corpo:patch-1
Open

Add cache_write_tokens mapping to UsageExtractor#330
ali-corpo wants to merge 1 commit intopydantic:mainfrom
ali-corpo:patch-1

Conversation

@ali-corpo
Copy link
Copy Markdown

Add cache_write_tokens mapping to UsageExtractor

Copilot AI review requested due to automatic review settings March 30, 2026 13:39
Copy link
Copy Markdown

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

Adds extraction support for cache-write token counts from OpenAI-compatible usage.prompt_tokens_details payloads.

Changes:

  • Added UsageExtractorMapping for prompt_tokens_details.cache_write_tokenscache_write_tokens across multiple chat-oriented extractors.
  • Enables downstream pricing/usage calculations to see cache-write token counts when present in provider responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 5054 to +5058
path=['prompt_tokens_details', 'cached_tokens'], dest='cache_read_tokens', required=False
),
UsageExtractorMapping(
path=['prompt_tokens_details', 'cache_write_tokens'], dest='cache_write_tokens', required=False
),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_write_tokens is treated as a separately-priced bucket in ModelPrice.calc_price() (it is subtracted from uncached input and priced via cache_write_mtok). OpenAI model entries in this data file do not define cache_write_mtok, so if responses start including usage.prompt_tokens_details.cache_write_tokens with a non-zero value, price calculation will undercharge. Either avoid extracting this field for providers/models without a cache_write_mtok, or ensure OpenAI model pricing defines an appropriate cache_write_mtok (often equal to input_mtok if cache writes are billed at the normal input rate).

Copilot uses AI. Check for mistakes.
Comment on lines +5056 to +5058
UsageExtractorMapping(
path=['prompt_tokens_details', 'cache_write_tokens'], dest='cache_write_tokens', required=False
),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change adds support for extracting usage.prompt_tokens_details.cache_write_tokens, but there isn't currently a regression test asserting the extraction behavior (and downstream pricing impact) for an OpenAI-style response that actually includes this field. Please add/extend an existing OpenAI extraction test (e.g. in tests/test_end_to_end.py or tests/test_extract_usage.py) to cover a non-zero cache_write_tokens value.

Copilot uses AI. Check for mistakes.
Comment on lines 1317 to +1321
path=['prompt_tokens_details', 'cached_tokens'], dest='cache_read_tokens', required=False
),
UsageExtractorMapping(
path=['prompt_tokens_details', 'cache_write_tokens'], dest='cache_write_tokens', required=False
),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deepseek models in this file appear to have cache_read_mtok pricing but no cache_write_mtok. Since cache_write_tokens are priced via cache_write_mtok (and subtracted from uncached input), extracting prompt_tokens_details.cache_write_tokens here can lead to undercharging if the API returns a non-zero value. Consider only mapping this field for providers/models that have a defined cache_write_mtok, or set an explicit cache_write_mtok that matches the provider’s billing semantics.

Copilot uses AI. Check for mistakes.
Comment on lines 10091 to +10095
path=['prompt_tokens_details', 'cached_tokens'], dest='cache_read_tokens', required=False
),
UsageExtractorMapping(
path=['prompt_tokens_details', 'cache_write_tokens'], dest='cache_write_tokens', required=False
),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X AI model pricing in this file includes cache_read_mtok but not cache_write_mtok. Because cache_write_tokens are subtracted from uncached input tokens and priced via cache_write_mtok, extracting prompt_tokens_details.cache_write_tokens can undercharge if present and non-zero. Consider removing this mapping for providers/models without cache_write_mtok, or define cache_write_mtok where appropriate.

Copilot uses AI. Check for mistakes.
Comment on lines 929 to +933
path=['prompt_tokens_details', 'cached_tokens'], dest='cache_read_tokens', required=False
),
UsageExtractorMapping(
path=['prompt_tokens_details', 'cache_write_tokens'], dest='cache_write_tokens', required=False
),
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure’s chat extractor now maps usage.prompt_tokens_details.cache_write_tokens into cache_write_tokens, but Azure/OpenAI-priced models in this file don’t define cache_write_mtok. Since cache_write_tokens are subtracted from uncached input and priced via cache_write_mtok, a non-zero value from the API would undercharge. Consider only extracting this field when a cache_write_mtok is defined (or ensure the pricing model includes the appropriate cache-write rate).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants