Webwright
Webwright copied to clipboard
feat: add cache_creation_input_tokens to usage metrics
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_tokensto_USAGE_METRIC_KEYSinbase.py - Extract the value from the Anthropic response payload in
anthropic_model.py - Expose it in
_usage_snapshot()alongsidecached_input_tokens - Return
0in 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 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 coverage
Not affected
- Prompt caching enable/disable logic — not touched
- PR #15 (caching wiring) — independent; no conflicts