feat: add cache_creation_input_tokens to usage metrics#28
Open
nuthalapativarun wants to merge 1 commit into
Open
feat: add cache_creation_input_tokens to usage metrics#28nuthalapativarun wants to merge 1 commit into
nuthalapativarun wants to merge 1 commit into
Conversation
_usage_from_anthropic_payload() extracted cache_read_input_tokens but omitted cache_creation_input_tokens, so dashboards always showed 0 for the write-side of prompt caching. Add the key to _USAGE_METRIC_KEYS and _usage_snapshot(), extract it in the Anthropic backend, and return 0 in OpenAI/OpenRouter backends where the field does not apply.
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.
Problem
_usage_from_anthropic_payload()extractscache_read_input_tokens(read hits) but omitscache_creation_input_tokens(write/creation cost). The key is absent from_USAGE_METRIC_KEYSand_usage_snapshot(), so dashboards and logs always show 0 for the write-side of prompt caching even when caching is active.Fix
cache_creation_input_tokensto_USAGE_METRIC_KEYSinbase.pyanthropic_model.py_usage_snapshot()alongsidecached_input_tokens0in the OpenAI and OpenRouter backends where the field does not apply, keeping all backends consistent with the metric key setChanges
src/webwright/models/base.py— added"cache_creation_input_tokens"to_USAGE_METRIC_KEYS; exposed the key in_usage_snapshot()for bothlast_requestandcumulative_requestblockssrc/webwright/models/anthropic_model.py— extractusage.get("cache_creation_input_tokens")and include it in the returned dictsrc/webwright/models/openai_model.py— added"cache_creation_input_tokens": 0to keep the return dict consistent with_USAGE_METRIC_KEYSsrc/webwright/models/openrouter_model.py— same zero-fill as OpenAItests/unit/test_cache_creation_tokens.py— four unit tests: key presence in_USAGE_METRIC_KEYS, extraction from a full payload, default-to-zero when absent, and full key-set coverageNot affected