feat(openai-agents): per-run identity via RunConfig trace_metadata - #833
Closed
marco-g-pm wants to merge 1 commit into
Closed
feat(openai-agents): per-run identity via RunConfig trace_metadata#833marco-g-pm wants to merge 1 commit into
marco-g-pm wants to merge 1 commit into
Conversation
The tracing processor is registered once per process, so distinct_id and properties passed to instrument() are process-global — wrong for any server handling multiple users. The only per-run escape hatch was a callable distinct_id reading trace metadata by hand. Read the well-known keys posthog_distinct_id and posthog_properties directly from RunConfig(trace_metadata=...) — the same names the OpenAI/Anthropic wrappers take as per-call kwargs, carried by the Agents SDK's per-run surface. Per-run values win over instrument() defaults, apply to the $ai_trace event and every span/generation in the trace, and are stripped from $ai_trace_metadata. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
marco-g-pm
added a commit
to PostHog/posthog
that referenced
this pull request
Aug 5, 2026
…nstrument() instrument() registers one process-global tracing processor, so the distinct_id and per-conversation properties the example passed there stamp every run in the process — wrong for any server. Show identity and session per run via RunConfig (group_id -> $ai_session_id, trace_metadata posthog_distinct_id / posthog_properties) and scope instrument() to process-constant config. Requires PostHog/posthog-python#833. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Reviews (1): Last reviewed commit: "feat(openai-agents): per-run identity vi..." | Re-trigger Greptile |
Contributor
posthog-python Compliance ReportDate: 2026-08-05 17:55:08 UTC ✅ All Tests Passed!111/111 tests passed Capture_V1 Tests✅ 94/94 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
Contributor
Author
|
claude got ahead of himself |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The tracing processor registers once per process, so
distinct_id/propertiespassed toinstrument()are process-global. For any server handling multiple users, that stamps every user's traces with the same identity. The only per-run escape hatch was passing a callabledistinct_idand threading values through trace metadata by hand — awkward, and the docs don't mention it.Surfaced while testing
wizard ai-observabilityagainst the wizard-workbench test apps: the wizard follows the docs, and on the openai-agents app produced events with no person attribution.Change
The processor now reads two well-known keys directly from
RunConfig(trace_metadata=...):posthog_distinct_id,posthog_properties) — one convention across paths, carried by whatever per-call surface each path has.instrument()defaults.$ai_traceevent and every span/generation in the trace.$ai_trace_metadata.distinct_idstill works as the default.Tests
7 new tests in
TestPerRunMetadata(override precedence, span inheritance, property merge order, control-key stripping, fallback paths). Full openai_agents suite: 60 passed. Ruff clean.Docs follow-up: posthog.com openai-agents page will be updated to show this pattern once this lands.
🤖 Generated with Claude Code