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

Conversation

lievan
Copy link
Contributor

@lievan lievan commented Jul 8, 2025

When prompt caching is used in anthropic, the number of input tokens returned in the usage field is the number of non cached tokens, not the total number of tokens sent to the model (what we expect in datadog)

This pr fixes this by setting input tokens to the total number of input tokens (cache read + cache write + input tokens)

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@lievan lievan requested review from a team as code owners July 8, 2025 20:23
@lievan lievan requested review from tabgok and sabrenner July 8, 2025 20:23
Copy link
Contributor

github-actions bot commented Jul 8, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/fix-anthropic-input-tokens-unified-calculation-a1b2c3d4e5f6g7h8.yaml  @DataDog/apm-python
ddtrace/llmobs/_integrations/anthropic.py                               @DataDog/ml-observability
tests/contrib/anthropic/test_anthropic_llmobs.py                        @DataDog/ml-observability

Copy link
Contributor

github-actions bot commented Jul 8, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 275 ± 3 ms.

The average import time from base is: 280 ± 3 ms.

The import time difference between this PR and base is: -4.9 ± 0.1 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 2.259 ms (0.82%)
ddtrace.bootstrap.sitecustomize 1.560 ms (0.57%)
ddtrace.bootstrap.preload 1.560 ms (0.57%)
ddtrace.internal.remoteconfig.client 0.704 ms (0.26%)
ddtrace.debugging._import 0.014 ms (0.01%)
ddtrace.debugging._function.discovery 0.014 ms (0.01%)
ddtrace.debugging._function 0.014 ms (0.01%)
ddtrace 0.699 ms (0.25%)
ddtrace.internal._unpatched 0.032 ms (0.01%)
json 0.032 ms (0.01%)
json.decoder 0.032 ms (0.01%)
re 0.032 ms (0.01%)
enum 0.032 ms (0.01%)
types 0.032 ms (0.01%)
ddtrace.settings._config 0.016 ms (0.01%)
ddtrace.internal.schema 0.016 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jul 8, 2025

Benchmarks

Benchmark execution time: 2025-07-15 03:55:42

Comparing candidate commit 14de3cf in PR branch evan.li/fix-ant-tokens with baseline commit c8d1431 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 424 metrics, 2 unstable metrics.

@lievan lievan enabled auto-merge (squash) July 15, 2025 03:05
@lievan lievan merged commit 493db87 into main Jul 15, 2025
462 checks passed
@lievan lievan deleted the evan.li/fix-ant-tokens branch July 15, 2025 03:56
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.

3 participants