Record OpenAI cache write tokens on spans #2637
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.67) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.67 · 258 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.332
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.067
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #2637"
general
weight: 0.35
pcbeingused333
get_openai_usage_attributesreadscached_tokensfromprompt_tokens_details(chat completions) orinput_tokens_details(Responses), but ignored thecache_write_tokensfield that the OpenAI API and theopenaiSDK (3.35) report next to it.gen_ai.usage.cache_creation.input_tokens, which is defined inllm_providers/semconv.pyand allow-listed in the scrubber, was therefore never set on OpenAI spans, and the cache-write count was only visible insidegen_ai.usage.raw.Both counts are now passed to
get_usage_attributes, the same wayget_anthropic_usage_attributesdoes since #2616. This covers chat completions, the Responses API and streams, since they all go through this function. Counts are only recorded when non-zero, so spans without prompt caching are unchanged.Test:
test_cache_write_tokens_recordedmocks a chat completion (cached_tokens=20,cache_write_tokens=30) and a Responses response (cache_write_tokens=40), and checksgen_ai.usage.cache_creation.input_tokensis 30 and 40 and that the cache read attribute is absent when it is 0. It fails on main with aKeyErrorand passes with the change. The existing cache tests intest_openai.pystill pass; the 5 failures intest_openai.py/test_anthropic.pythat occur in my environment are identical on main.This change was made with Claude Code (AI-assisted); the test above was run before opening the PR.
🤖 Generated with Claude Code
<!-- This is an auto-generated description by cubic. -->
<a href="https://cubic.dev/pr/pydantic/logfire/pull/2637?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->