feat(llmobs): track prompt caching for bedrock converse api
Tracks number of tokens read from and written to the prompt cache for bedrock converse api
https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
Bedrock returns cacheWrite/ReadInputTokenCount or cacheWrite/ReadInputTokens (not exactly sure why there are two names, but I believe they are the same)
We map these to cache_write/read_input_tokens keys in our metrics field.
Checklist
- [x] 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
CODEOWNERS have been resolved as:
releasenotes/notes/bedrock-p-cache-0b73a062953ecfcf.yaml @DataDog/apm-python
tests/contrib/botocore/bedrock_cassettes/bedrock_converse_prompt_caching.yaml @DataDog/ml-observability
tests/contrib/botocore/bedrock_cassettes/bedrock_converse_stream_prompt_caching.yaml @DataDog/ml-observability
ddtrace/contrib/internal/botocore/services/bedrock.py @DataDog/ml-observability
ddtrace/llmobs/_constants.py @DataDog/ml-observability
ddtrace/llmobs/_integrations/bedrock.py @DataDog/ml-observability
ddtrace/llmobs/_integrations/utils.py @DataDog/ml-observability
tests/contrib/botocore/bedrock_utils.py @DataDog/ml-observability
tests/contrib/botocore/test_bedrock_llmobs.py @DataDog/ml-observability
Bootstrap import analysis
Comparison of import times between this PR and base.
Summary
The average import time from this PR is: 283 ± 4 ms.
The average import time from base is: 287 ± 4 ms.
The import time difference between this PR and base is: -3.7 ± 0.2 ms.
Import time breakdown
The following import paths have shrunk:
ddtrace.auto
2.151 ms
(0.76%)
ddtrace.bootstrap.sitecustomize
1.465 ms
(0.52%)
ddtrace.bootstrap.preload
1.465 ms
(0.52%)
ddtrace.internal.remoteconfig.client
0.689 ms
(0.24%)
ddtrace
0.687 ms
(0.24%)
ddtrace.internal._unpatched
0.036 ms
(0.01%)
json
0.036 ms
(0.01%)
json.decoder
0.036 ms
(0.01%)
re
0.036 ms
(0.01%)
enum
0.036 ms
(0.01%)
types
0.036 ms
(0.01%)
Benchmarks
Benchmark execution time: 2025-07-01 19:59:00
Comparing candidate commit 4fed7b97651bc7c440191d1998efc2946b219319 in PR branch evan.li/bedrock-prompt-caching with baseline commit 5fb2a8af26edfab9d0b1cccd04174361aa481473 in branch main.
Found 0 performance improvements and 5 performance regressions! Performance is the same for 542 metrics, 3 unstable metrics.
scenario:iastaspects-replace_aspect
- 🟥
execution_time[+651.659ns; +755.892ns] or [+13.666%; +15.852%]
scenario:iastaspectsospath-ospathbasename_aspect
- 🟥
execution_time[+787.550ns; +859.076ns] or [+18.436%; +20.110%]
scenario:iastaspectsospath-ospathjoin_aspect
- 🟥
execution_time[+923.493ns; +1006.824ns] or [+15.064%; +16.424%]
scenario:iastaspectsospath-ospathnormcase_aspect
- 🟥
execution_time[+386.273ns; +440.924ns] or [+10.978%; +12.531%]
scenario:iastaspectsospath-ospathsplit_aspect
- 🟥
execution_time[+1.016µs; +1.094µs] or [+20.870%; +22.465%]