Repository navigation
fix(ai): read OpenAI Agents response span usage when sensitive data is off - #1287
Conversation
dustinbyrne
left a comment
There was a problem hiding this comment.
Looks good — falling back to span_data.usage when response is unset cleanly restores $ai_input_tokens, $ai_output_tokens, $ai_total_tokens, $ai_cache_read_input_tokens, and $ai_cache_reporting_exclusive under trace_include_sensitive_data=False without affecting the full-response path or leaking sensitive content. Verified unit tests, synthetic cached-token smoke runs, and live gpt-6-luna completions (non-streaming and streaming) against sdk-mock-server; changeset looks accurate.
AI-assisted review.
|
thank you @MaiAditya! 🙌 |
SDK performance: posthog-pythonBenchmarks completed. No changes flagged for review. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
💡 Motivation and Context
With
RunConfig(trace_include_sensitive_data=False)(orOPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA=false), the Agents SDK does not attach theResponseobject to response spans, sospan_data.responseisNone. It still records token usage onspan_data.usage, using the same shape as the Responses usage object (input_tokens,output_tokens,input_tokens_details.cached_tokens, ...). The SDK has done this since 0.18, which is our minimum version._handle_response_spanonly read usage fromspan_data.response.usage. When sensitive data is off, every$ai_generationfrom the defaultOpenAIResponsesModelarrived with no input, output, total or cache-read tokens.The fix falls back to
span_data.usagewhen the response is missing._usage_detailalready handles dicts, so cache reads (and$ai_cache_reporting_exclusive=False) come through unchanged. When the response is present, nothing changes.$ai_modelis still empty in this mode, because the SDK does not record the model on the span when sensitive data is off. So these events get token counts but are still not costed. This PR does not change that.💚 How did you test it?
test_response_span_reads_span_usage_without_responsebuilds aResponseSpanDatawith onlyusageset, the way the SDK leaves it. Onmainit fails withKeyError: '$ai_input_tokens'. With this change it passes. All 69 tests inposthog/test/ai/openai_agentspass, and so doesposthog/test/ai(1925 passed, 15 skipped).Runner.run_syncwith a realOpenAIResponsesModelwhose OpenAI client is mocked to return aResponse(100 input, 20 output, 60 cached tokens), withinstrument()registered.trace_include_sensitive_data=True: tokens reported onmainand on this branch.trace_include_sensitive_data=False: onmain,$ai_input_tokens,$ai_output_tokensand$ai_cache_read_input_tokenswere all missing. On this branch they are 100 / 20 / 60.ruff check .andruff format --check .(ruff 0.12.2, as pinned inuv.lock) are clean. mypy reports no new errors inprocessor.py.📝 Checklist
If releasing new changes
.sampo/changesets/openai-agents-response-span-usage.md, written in thesampo addformat)🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Claude Code helped find the gap, by comparing what the Agents SDK writes to
ResponseSpanDatawith what the processor reads, and drafted the fix, test and changeset. I reviewed the change and ran the unit tests and the offline end-to-end check. I kept the fix to the missing-response case and left response spans that carry a fullResponsealone.