Skip to content

fix(telemetry): include Anthropic cache tokens in total token extraction - #2573

Open
feiiiiii5 wants to merge 2 commits into
rhesis-ai:mainfrom
feiiiiii5:fix/anthropic-cache-token-extraction
Open

fix(telemetry): include Anthropic cache tokens in total token extraction#2573
feiiiiii5 wants to merge 2 commits into
rhesis-ai:mainfrom
feiiiiii5:fix/anthropic-cache-token-extraction

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Description

Fixes #2572

extract_token_usage() had no key names for Anthropic's cache_creation_input_tokens / cache_read_input_tokens fields, so total_tokens silently dropped cached tokens for any Anthropic-backed call. These tokens are billed by the provider and feed into backend high-usage anomaly checks via LLM_TOKENS_TOTAL, so under-reporting has operational impact beyond cost display.

Changes:

  • Extract both cache fields (snake_case + camelCase variants) alongside existing input/output extraction
  • Add cache tokens to the computed total when no explicit total_tokens is provided
  • Behavior unchanged when cache fields are absent or an explicit total exists

Tests: Added test_token_extraction.py covering Anthropic with/without cache, OpenAI format, and None input. Verified manually: Anthropic usage {input:50, output:20, cache_creation:1000, cache_read:4000} now yields (50, 20, 5070) instead of (50, 20, 70).

This contribution was developed with LLM assistance following repo conventions.

extract_token_usage() had no key names for Anthropic's
cache_creation_input_tokens / cache_read_input_tokens fields, so
total_tokens silently dropped cached tokens for Anthropic-backed calls.
These tokens are billed by the provider and feed into backend
high-usage anomaly checks via LLM_TOKENS_TOTAL, so under-reporting has
operational impact beyond cost display.

Extract both cache fields and add them to the computed total when no
explicit total is provided; behavior is unchanged when the fields are
absent or an explicit total_tokens is present.

Fixes rhesis-ai#2572

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
@feiiiiii5
feiiiiii5 force-pushed the fix/anthropic-cache-token-extraction branch from 14f8dd0 to aa7dcb2 Compare August 22, 2026 15:57

@peqy peqy Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improvement: total recomputation currently ignores the “cache-only” case (cache tokens present but input/output are 0), which would still under-report.

Everything else looks solid, and the added tests cover the main intended Anthropic/OpenAI/Gemini/None paths.

Comment thread packages/rhesis/src/rhesis/telemetry/token_extraction.py Outdated

@peqy peqy Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good and tests cover the main Anthropic/OpenAI/Gemini cases.

Only remaining suggestion (see inline thread): consider computing total_tokens when only cache tokens are present (cache-hit scenarios with 0 input/output), e.g. widen the condition to include cache_creation_tokens / cache_read_tokens.

Widen the total-computation condition to include cache tokens per
peqy review feedback: a provider returning only cache hits with zero
input/output would previously report total=0.

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>

@peqy peqy Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Fix correctly includes Anthropic cache tokens in total_tokens (including cache-only cases) and adds solid regression coverage. Ship it.

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.

Anthropic-format token extractor has no cache_creation_input_tokens/cache_read_input_tokens keys, so cost math silently drops cached tokens

1 participant