#8057·QwenPaw

[Bug]: Context meter under-reports usage for Anthropic Messages providers (cache read/write tokens are not counted)

Author: BLUE0818Created Sep 30, 2026Updated Sep 30, 2026
Labelsbug

QwenPaw Version

2.2.2b4

Description

When a provider uses the Anthropic Messages protocol with prompt caching, the context meter next to the chat input shows a much smaller value than the real context size.

In Anthropic's usage, input_tokens counts only the uncached part of the prompt. Cache reads (cache_read_input_tokens) and cache writes (cache_creation_input_tokens) are reported separately, and both are part of the prompt the model processed. The context size of one request is therefore:

input_tokens + cache_read_input_tokens + cache_creation_input_tokens

TokenRecordingModelWrapper._record_usage() in src/qwenpaw/token_usage/model_wrapper.py sets last_prompt_tokens to input_tokens alone. The console channel (src/qwenpaw/app/channels/console/channel.py, around line 440) sends this value as context_usage.estimated_tokens in the live turn_usage event, and the meter divides it by the context window. Once most of a conversation is cached, the meter shows only the few thousand new tokens of each request.

The same file already handles this correctly for the cache hit rate: for Anthropic models, _cache_usage_metrics() uses prompt_tokens + cache_read_tokens + cache_write_tokens as the input denominator. The context value does not get the same adjustment.

OpenAI Chat Completions and Responses are not affected, because their prompt_tokens / input_tokens already include cached tokens.

Related PR(s): #7899 (added last_prompt_tokens and the live per-call context value)

Security considerations: None.

Component(s) Affected

  • Core / Backend (app, agents, config, providers, utils, local_models)
  • Console (frontend web UI)
  • Channels (DingTalk, Feishu, QQ, Discord, iMessage, etc.)
  • Skills
  • CLI
  • Documentation (website)
  • Tests
  • CI/CD
  • Scripts / Deploy

Environment

  • QwenPaw version: 2.2.2b4
  • OS: Windows
  • Install method: pip
  • Python version (if applicable): 3.12.10

Steps to Reproduce

  1. Add a custom provider that uses the Anthropic Messages protocol (AnthropicChatModel) with a Claude model that supports prompt caching.
  2. Hold a multi-turn conversation (tool calls help) until most of each request is served from cache.
  3. Watch the context meter while a reply is streaming, and compare it with the usage returned by the provider.

Actual vs Expected

  • Actual: For a request with input_tokens=3000, cache_read_input_tokens=120000, and cache_creation_input_tokens=5000 on a 200k window, the meter shows about 3k / 200k (1.5%).
  • Expected: The meter shows 128k / 200k (64%).

Logs / Screenshots

Minimal offline reproduction with the installed package (no network request):

python
from types import SimpleNamespace
from agentscope.model import AnthropicChatModel
import qwenpaw.token_usage.model_wrapper as mw
from qwenpaw.token_usage.model_wrapper import TokenRecordingModelWrapper

mw.get_token_usage_manager = lambda: SimpleNamespace(enqueue=lambda event: None)
captured = {}
inner = object.__new__(AnthropicChatModel)
inner.context_size = 200_000
wrapper = object.__new__(TokenRecordingModelWrapper)
for key, value in {
    "_model": inner,
    "_provider_id": "demo",
    "_compact_threshold": None,
    "model": "demo",
}.items():
    object.__setattr__(wrapper, key, value)
wrapper._store_usage = captured.update
wrapper._record_usage(SimpleNamespace(
    input_tokens=3_000,
    output_tokens=500,
    cache_input_tokens=120_000,
    cache_creation_input_tokens=5_000,
    metadata={},
))
print(captured["last_prompt_tokens"], captured["cache_eligible_input_tokens"])

Output:

3000 128000

last_prompt_tokens (used by the context meter) is 3000, while the wrapper's own cache denominator for the same request is 128000.

Additional Notes

A possible fix in _record_usage() is to reuse the value _cache_usage_metrics() already computes:

python
"last_prompt_tokens": cache_eligible if cache_observed else pt,

For Anthropic models this gives input + cache read + cache write. For OpenAI Chat and Responses, cache_eligible equals pt, so their behavior stays the same.

From reading the code: at the end of each turn, _commit_turn_usage() sends another turn_usage event whose context_usage comes from the character-based estimate in snapshot_context_usage_for_state(). The meter may therefore switch between the provider value during streaming and the local estimate after the turn.

We hit the same problem in a local usage-statistics plugin and fixed it by adding cache read and cache write tokens for Anthropic models.