Skip to content

fix(anthropic): emit prompt cache usage attributes - #1450

Open
jstar0 wants to merge 1 commit into
AgentOps-AI:mainfrom
jstar0:fix/1445-anthropic-cache-usage
Open

jstar0 wants to merge 1 commit into
AgentOps-AI:mainfrom
jstar0:fix/1445-anthropic-cache-usage

Conversation

@jstar0

@jstar0 jstar0 commented Sep 9, 2026

Copy link
Copy Markdown

Summary

  • Extract Anthropic prompt-cache read and creation token counts from non-streaming messages and raw streaming metadata.
  • Keep the existing prompt, completion, and total attributes based on Anthropic native input/output values.
  • Cover synchronous and asynchronous stream final-message paths with fixture-based regressions.

Why

Anthropic reports cache-read and cache-creation tokens as fields separate from input_tokens. The instrumentation previously dropped those fields, making cache activity invisible to telemetry consumers. This change exposes both fields independently while preserving the existing native input/output total, so consumers can choose their accounting policy without double-counting cache tokens.

Validation

  • uv run pytest -q tests/unit/instrumentation/anthropic/test_attributes.py tests/unit/instrumentation/anthropic/test_stream_wrapper.py — 22 passed
  • uv run ruff check on the four changed files — passed
  • uv run ruff format --check on the four changed files — passed

Fixes #1445

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 prompt caching: cache tokens not emitted on the Anthropic paths — is session total_tokens systematically low?

1 participant