Skip to content

feat: add cache_creation_input_tokens to usage metrics#28

Open
nuthalapativarun wants to merge 1 commit into
microsoft:mainfrom
nuthalapativarun:feat/cache-creation-input-tokens
Open

feat: add cache_creation_input_tokens to usage metrics#28
nuthalapativarun wants to merge 1 commit into
microsoft:mainfrom
nuthalapativarun:feat/cache-creation-input-tokens

Conversation

@nuthalapativarun
Copy link
Copy Markdown

Problem

_usage_from_anthropic_payload() extracts cache_read_input_tokens (read hits) but omits cache_creation_input_tokens (write/creation cost). The key is absent from _USAGE_METRIC_KEYS and _usage_snapshot(), so dashboards and logs always show 0 for the write-side of prompt caching even when caching is active.

Fix

  • Add cache_creation_input_tokens to _USAGE_METRIC_KEYS in base.py
  • Extract the value from the Anthropic response payload in anthropic_model.py
  • Expose it in _usage_snapshot() alongside cached_input_tokens
  • Return 0 in the OpenAI and OpenRouter backends where the field does not apply, keeping all backends consistent with the metric key set

Changes

  • src/webwright/models/base.py — added "cache_creation_input_tokens" to _USAGE_METRIC_KEYS; exposed the key in _usage_snapshot() for both last_request and cumulative_request blocks
  • src/webwright/models/anthropic_model.py — extract usage.get("cache_creation_input_tokens") and include it in the returned dict
  • src/webwright/models/openai_model.py — added "cache_creation_input_tokens": 0 to keep the return dict consistent with _USAGE_METRIC_KEYS
  • src/webwright/models/openrouter_model.py — same zero-fill as OpenAI
  • tests/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 coverage

Not affected

_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.
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.

1 participant