Commit de41845
feat(openai_agents): pull cached tokens through into metrics (#364)
## Summary
- Walk `*_tokens_details` sub-objects in `_usage_to_metrics` so the
OpenAI Agents SDK integration picks up cached / reasoning / audio token
counts (e.g. `input_tokens_details.cached_tokens` →
`prompt_cached_tokens`). Mirrors the JS fix in
[braintrust-sdk-javascript#1186](braintrustdata/braintrust-sdk-javascript@a05dc4d).
- Route `_response_log_data` through `_usage_to_metrics` instead of
hardcoding the three `total/input/output` fields, so the Responses API
path benefits from the same extraction.
- `_task_log_data` and `_turn_log_data` already delegated to
`_usage_to_metrics`, so they inherit the fix.
## Why
A customer reported that cached tokens are not showing up in the Python
`BraintrustTracingProcessor`. The narrow 3-field extraction in
`_response_log_data` (Responses API) and `_usage_to_metrics`
(chat-completions / Generation spans) drops
`input_tokens_details.cached_tokens` even though the OpenAI wrapper
(`braintrust/oai.py`'s `_parse_metrics_from_usage`) already handles it
correctly. The JS SDK was patched in December but the Python equivalent
was never written.
## Test plan
- [x] `test_response_span_extracts_cached_tokens_from_usage` — Response
span sees `prompt_cached_tokens`
- [x] `test_response_span_handles_zero_cached_tokens` — zero is
preserved, not dropped
- [x] `test_response_span_handles_missing_cached_tokens` — no
`prompt_cached_tokens` key when details absent
- [x] `test_generation_span_extracts_cached_tokens_from_usage` —
Generation span path
- [x] Existing non-VCR processor tests still pass
---------
Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>1 parent aff7b6a commit de41845
2 files changed
Lines changed: 29 additions & 3 deletions
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
128 | 134 | | |
129 | 135 | | |
130 | 136 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
72 | 80 | | |
73 | 81 | | |
74 | 82 | | |
| |||
86 | 94 | | |
87 | 95 | | |
88 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
89 | 110 | | |
90 | 111 | | |
91 | 112 | | |
| |||
166 | 187 | | |
167 | 188 | | |
168 | 189 | | |
169 | | - | |
170 | | - | |
171 | | - | |
| 190 | + | |
| 191 | + | |
172 | 192 | | |
173 | 193 | | |
174 | 194 | | |
| |||
0 commit comments