← Back to Pull requests

Record OpenAI cache write tokens on spans #2637

Closed
unknown closed this pull request without merging on Oct 26, 2026 12:01 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 16:06 UTC
Author

get_openai_usage_attributes reads cached_tokens from prompt_tokens_details (chat completions) or input_tokens_details (Responses), but ignored the cache_write_tokens field that the OpenAI API and the openai SDK (3.35) report next to it. gen_ai.usage.cache_creation.input_tokens, which is defined in llm_providers/semconv.py and allow-listed in the scrubber, was therefore never set on OpenAI spans, and the cache-write count was only visible inside gen_ai.usage.raw.

Both counts are now passed to get_usage_attributes, the same way get_anthropic_usage_attributes does 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_recorded mocks a chat completion (cached_tokens=20, cache_write_tokens=30) and a Responses response (cache_write_tokens=40), and checks gen_ai.usage.cache_creation.input_tokens is 30 and 40 and that the cache read attribute is absent when it is 0. It fails on main with a KeyError and passes with the change. The existing cache tests in test_openai.py still pass; the 5 failures in test_openai.py/test_anthropic.py that 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. -->

Diff: 258 lines (+193 / -65) across 3 files Reviewer: pcbeingused333 Review duration: 6954m
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
0.67
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 67.3% (HIGH) · Higher = larger blast radius
67.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 26.6% · Safe to merge
26.6%