Skip to content

fix(llmobs): fix input tokens counting for anthropic #13917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions ddtrace/llmobs/_integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,16 @@ def _extract_usage(self, span: Span, usage: Dict[str, Any]):
cache_read_tokens = _get_attr(usage, "cache_read_input_tokens", None)

metrics = {}
if input_tokens is not None:
metrics[INPUT_TOKENS_METRIC_KEY] = input_tokens

# `input_tokens` in the returned usage is the number of non-cached tokens. We normalize it to mean
# the total tokens sent to the model to be consistent with other model providers.
metrics[INPUT_TOKENS_METRIC_KEY] = (input_tokens or 0) + (cache_write_tokens or 0) + (cache_read_tokens or 0)

if output_tokens is not None:
metrics[OUTPUT_TOKENS_METRIC_KEY] = output_tokens
if input_tokens is not None and output_tokens is not None:
metrics[TOTAL_TOKENS_METRIC_KEY] = input_tokens + output_tokens
if INPUT_TOKENS_METRIC_KEY in metrics and output_tokens is not None:
metrics[TOTAL_TOKENS_METRIC_KEY] = metrics[INPUT_TOKENS_METRIC_KEY] + output_tokens

if cache_write_tokens is not None:
metrics[CACHE_WRITE_INPUT_TOKENS_METRIC_KEY] = cache_write_tokens
if cache_read_tokens is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
LLM Observability: Addresses an upstream issue in Anthropic prompt caching, which reports input tokens as the number of non-cached tokens instead of the total tokens sent to the model. With this fix, LLM Observability correctly counts input tokens to include cached read/write prompt tokens.
16 changes: 8 additions & 8 deletions tests/contrib/anthropic/test_anthropic_llmobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,9 @@ def test_completion_prompt_caching(
"max_tokens": 100.0,
},
token_metrics={
"input_tokens": 18,
"input_tokens": 2073,
"output_tokens": 100,
"total_tokens": 118,
"total_tokens": 2173,
"cache_write_input_tokens": 2055,
"cache_read_input_tokens": 0,
},
Expand Down Expand Up @@ -908,9 +908,9 @@ def test_completion_prompt_caching(
"max_tokens": 100.0,
},
token_metrics={
"input_tokens": 11,
"input_tokens": 2066,
"output_tokens": 100,
"total_tokens": 111,
"total_tokens": 2166,
"cache_write_input_tokens": 0,
"cache_read_input_tokens": 2055,
},
Expand Down Expand Up @@ -979,9 +979,9 @@ def test_completion_stream_prompt_caching(
"max_tokens": 100.0,
},
token_metrics={
"input_tokens": 18,
"input_tokens": 1049,
"output_tokens": 100,
"total_tokens": 118,
"total_tokens": 1149,
"cache_write_input_tokens": 1031,
"cache_read_input_tokens": 0,
},
Expand Down Expand Up @@ -1009,9 +1009,9 @@ def test_completion_stream_prompt_caching(
"max_tokens": 100.0,
},
token_metrics={
"input_tokens": 11,
"input_tokens": 1042,
"output_tokens": 100,
"total_tokens": 111,
"total_tokens": 1142,
"cache_write_input_tokens": 0,
"cache_read_input_tokens": 1031,
},
Expand Down
Loading