Skip to content

fix(ai): read OpenAI Agents response span usage when sensitive data is off - #1287

Merged
dustinbyrne merged 1 commit into
PostHog:mainfrom
MaiAditya:fix/agents-response-span-usage-without-sensitive-data
Nov 23, 2026
Merged

dustinbyrne merged 1 commit into
PostHog:mainfrom
MaiAditya:fix/agents-response-span-usage-without-sensitive-data

Conversation

@MaiAditya

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

With RunConfig(trace_include_sensitive_data=False) (or OPENAI_AGENTS_TRACE_INCLUDE_SENSITIVE_DATA=false), the Agents SDK does not attach the Response object to response spans, so span_data.response is None. It still records token usage on span_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_span only read usage from span_data.response.usage. When sensitive data is off, every $ai_generation from the default OpenAIResponsesModel arrived with no input, output, total or cache-read tokens.

The fix falls back to span_data.usage when the response is missing. _usage_detail already handles dicts, so cache reads (and $ai_cache_reporting_exclusive=False) come through unchanged. When the response is present, nothing changes.

$ai_model is 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?

  • Unit: test_response_span_reads_span_usage_without_response builds a ResponseSpanData with only usage set, the way the SDK leaves it. On main it fails with KeyError: '$ai_input_tokens'. With this change it passes. All 69 tests in posthog/test/ai/openai_agents pass, and so does posthog/test/ai (1925 passed, 15 skipped).
  • End to end, offline: ran Runner.run_sync with a real OpenAIResponsesModel whose OpenAI client is mocked to return a Response (100 input, 20 output, 60 cached tokens), with instrument() registered.
    • trace_include_sensitive_data=True: tokens reported on main and on this branch.
    • trace_include_sensitive_data=False: on main, $ai_input_tokens, $ai_output_tokens and $ai_cache_read_input_tokens were all missing. On this branch they are 100 / 20 / 60.
  • ruff check . and ruff format --check . (ruff 0.12.2, as pinned in uv.lock) are clean. mypy reports no new errors in processor.py.
  • I did not test against the live OpenAI API.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Added a changeset file (.sampo/changesets/openai-agents-response-span-usage.md, written in the sampo add format)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code helped find the gap, by comparing what the Agents SDK writes to ResponseSpanData with 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 full Response alone.

@MaiAditya
MaiAditya requested a review from a team as a code owner November 20, 2026 23:24

@dustinbyrne dustinbyrne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dustinbyrne

Copy link
Copy Markdown
Contributor

thank you @MaiAditya! 🙌

@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks 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
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op No observed change Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.51 count/op 1,345.51 count/op No observed change Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 77.01 µs/op 76.79 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.59 MiB 37.72 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.04 MiB 40.17 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.04 MiB 40.17 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 80.63 µs/op 77.66 µs/op 3.7% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 41.24 MiB 41.24 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.75 KiB/op 185.75 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.51 count/op 1,337.51 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 100.53 µs/op 100.67 µs/op 0.1% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.29 MiB 41.21 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.08 MiB 47 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.08 MiB 47 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 98.09 µs/op 98.17 µs/op <0.1% more time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.54 MiB 51.4 MiB 0.3% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.73 KiB/op 138.73 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.28 count/op 1,390.29 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 87.67 µs/op 88.18 µs/op 0.6% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.61 MiB 37.55 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.78 MiB 37.71 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.78 MiB 37.71 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 89.76 µs/op 90.16 µs/op 0.4% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.24 MiB 41.24 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.9 KiB/op 190.9 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.19 count/op 1,382.19 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 105.76 µs/op 104.96 µs/op 0.8% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.08 MiB 41.05 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.54 MiB 41.49 MiB 0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.14 MiB 41.1 MiB 0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 103.29 µs/op 103.19 µs/op <0.1% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.5 MiB 41.47 MiB <0.1% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 57.02 µs/op 56.94 µs/op 0.2% less time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 57.02 µs/op 56.94 µs/op 0.1% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 56.63 µs/op 56.81 µs/op 0.3% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 56.63 µs/op 56.8 µs/op 0.3% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 225.43 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,279.42 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 82.83 µs/op 82.06 µs/op 0.9% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 82.83 µs/op 82.06 µs/op 0.9% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,413.69 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 775.66 µs/op 768.71 µs/op 0.9% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 775.69 µs/op 768.69 µs/op 0.9% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 21.63 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 223,655.44 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.64 ms/op 7.63 ms/op 0.2% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.64 ms/op 7.63 ms/op 0.2% less time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 43.97 MiB 43.97 MiB <0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.23 MiB 44.27 MiB <0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.23 MiB 44.27 MiB <0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 44.05 MiB 43.99 MiB 0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 43.12 MiB 43.11 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 43.14 MiB 43.12 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 43.14 MiB 43.12 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.89 MiB 43.39 MiB 1.1% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 15.74 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 162.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 9.19 µs/op 9.17 µs/op 0.1% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 9.19 µs/op 9.17 µs/op 0.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 24.6 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 239.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 12.95 µs/op 13 µs/op 0.4% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 12.95 µs/op 13 µs/op 0.4% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 39.81 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 388.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 18.35 µs/op 18.47 µs/op 0.6% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 18.35 µs/op 18.47 µs/op 0.6% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 60.59 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 584.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 26.4 µs/op 26.44 µs/op 0.2% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 26.4 µs/op 26.44 µs/op 0.2% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.22 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 567.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 25.01 µs/op 24.96 µs/op 0.2% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 25.01 µs/op 24.96 µs/op 0.2% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.04 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 192.27 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 10.35 µs/op 10.38 µs/op 0.3% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 10.35 µs/op 10.38 µs/op 0.3% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 20.66 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 197.77 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 10.92 µs/op 10.86 µs/op 0.5% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 10.92 µs/op 10.86 µs/op 0.5% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 14.89 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 152.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 8.64 µs/op 8.68 µs/op 0.5% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 8.64 µs/op 8.68 µs/op 0.5% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 15.9 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 166.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 9.59 µs/op 9.62 µs/op 0.3% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 9.59 µs/op 9.62 µs/op 0.3% more time Not flagged

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 84fc9e06bd898fe090e7443138e32010b9aa91db → ea7f1d9d38b9b2db60e7ed1daef90195b1d5d75c.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@dustinbyrne
dustinbyrne merged commit d390aae into PostHog:main Nov 23, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants