Skip to content

fix(llmobs): fix input token counting for bedrock prompt caching #13919

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

lievan
Copy link
Contributor

@lievan lievan commented Jul 8, 2025

When prompt caching is used in bedrock, 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 21:00
Copy link
Contributor

github-actions bot commented Jul 8, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/fix-bedrock-input-tokens-unified-calculation-b2c3d4e5f6g7h8i9.yaml  @DataDog/apm-python
ddtrace/llmobs/_integrations/bedrock.py                                 @DataDog/ml-observability
ddtrace/llmobs/_integrations/bedrock_utils.py                           @DataDog/ml-observability
tests/contrib/botocore/test_bedrock_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: 279 ± 3 ms.

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

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

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.960 ms (0.70%)
ddtrace.bootstrap.sitecustomize 1.281 ms (0.46%)
ddtrace.bootstrap.preload 1.281 ms (0.46%)
ddtrace.internal.remoteconfig.client 0.649 ms (0.23%)
ddtrace 0.678 ms (0.24%)
ddtrace.internal._unpatched 0.031 ms (0.01%)
json 0.031 ms (0.01%)
json.decoder 0.031 ms (0.01%)
re 0.031 ms (0.01%)
enum 0.031 ms (0.01%)
types 0.031 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jul 8, 2025

Benchmarks

Benchmark execution time: 2025-07-15 03:56:37

Comparing candidate commit f2db3db in PR branch evan.li/fix-bedrock-tokens with baseline commit 573a530 in branch main.

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

scenario:iastaspects-lstrip_aspect

  • 🟥 execution_time [+736.422ns; +823.072ns] or [+7.031%; +7.859%]

scenario:iastaspects-replace_aspect

  • 🟥 execution_time [+746.774ns; +807.135ns] or [+15.835%; +17.115%]

Copy link
Contributor

@Yun-Kim Yun-Kim left a comment

Choose a reason for hiding this comment

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

Cosmetic nits but LGTM

lievan and others added 2 commits July 14, 2025 14:41
Co-authored-by: Yun Kim <35776586+Yun-Kim@users.noreply.github.com>
…n-b2c3d4e5f6g7h8i9.yaml

Co-authored-by: Yun Kim <35776586+Yun-Kim@users.noreply.github.com>
@lievan lievan enabled auto-merge (squash) July 14, 2025 18:42
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.

4 participants