Webwright icon indicating copy to clipboard operation
Webwright copied to clipboard

feat: add cache_creation_input_tokens to usage metrics

Open nuthalapativarun opened this issue 6 months ago • 0 comments

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

  • Prompt caching enable/disable logic — not touched
  • PR #15 (caching wiring) — independent; no conflicts

nuthalapativarun avatar May 29 '26 06:05 nuthalapativarun