Skip to content

feat(openai-agents): per-run identity via RunConfig trace_metadata - #833

Closed
marco-g-pm wants to merge 1 commit into
mainfrom
marco/agents-per-run-identity
Closed

feat(openai-agents): per-run identity via RunConfig trace_metadata#833
marco-g-pm wants to merge 1 commit into
mainfrom
marco/agents-per-run-identity

Conversation

@marco-g-pm

Copy link
Copy Markdown
Contributor

Problem

The tracing processor registers once per process, so distinct_id / properties passed to instrument() 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 callable distinct_id and threading values through trace metadata by hand — awkward, and the docs don't mention it.

Surfaced while testing wizard ai-observability against 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=...):

instrument()  # process-constant config only

result = Runner.run_sync(
    agent,
    prompt,
    run_config=RunConfig(
        group_id=conversation_id,  # already maps to $ai_session_id
        trace_metadata={
            "posthog_distinct_id": user_id,
            "posthog_properties": {"plan": "scale"},
        },
    ),
)
  • Key names match the per-call kwargs the OpenAI/Anthropic wrappers already use (posthog_distinct_id, posthog_properties) — one convention across paths, carried by whatever per-call surface each path has.
  • Per-run values win over instrument() defaults.
  • Applied to the $ai_trace event and every span/generation in the trace.
  • Control keys are stripped from $ai_trace_metadata.
  • No behavior change when the keys are absent; static/callable distinct_id still 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

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
marco-g-pm requested a review from a team as a code owner August 5, 2026 17:50
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>
@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(openai-agents): per-run identity vi..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-08-05 17:55:08 UTC
Duration: 256429ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 518ms
Endpoint And Method.Does Not Use Legacy Endpoints 511ms
Required Headers.Has Authorization Bearer Header 511ms
Required Headers.Has Content Type Json 511ms
Required Headers.Has Posthog Sdk Info Format 511ms
Required Headers.Has Posthog Attempt Header 510ms
Required Headers.Has Posthog Request Id 511ms
Required Headers.Has Posthog Request Timestamp 511ms
Required Headers.Has User Agent 511ms
Body Format.Body Has Created At And Batch 510ms
Body Format.No Api Key In Body 511ms
Body Format.No Sent At In Body 510ms
Event Format.Event Has Required Root Fields 511ms
Event Format.Event Uuid Is Valid 511ms
Event Format.Event Timestamp Is Rfc3339 511ms
Event Format.Distinct Id Is String 510ms
Event Format.Distinct Id At Root Not Properties 511ms
Event Format.Custom Properties Preserved 511ms
Event Format.Set Properties Preserved 511ms
Event Format.Set Once Properties Preserved 510ms
Event Format.Groups Properties Preserved 512ms
Event Format.Sdk Generates Uuid If Not Provided 511ms
Event Format.Event Has Required Root Fields Batch 514ms
Event Format.Event Uuid Is Valid Batch 513ms
Event Format.Event Timestamp Is Rfc3339 Batch 514ms
Event Format.Distinct Id Is String Batch 514ms
Event Format.Distinct Id At Root Not Properties Batch 514ms
Event Format.Custom Properties Preserved Batch 513ms
Event Format.Set Properties Preserved Batch 514ms
Event Format.Set Once Properties Preserved Batch 515ms
Event Format.Groups Properties Preserved Batch 514ms
Event Format.Sdk Generates Uuid If Not Provided Batch 514ms
Batch Behavior.Multiple Events In Single Batch 518ms
Batch Behavior.Batch Envelope Smoke 516ms
Batch Behavior.Flush With No Events Sends Nothing 507ms
Batch Behavior.Flush At Triggers Batch 1011ms
Batch Behavior.Created At Reflects Batch Creation Time 512ms
Deduplication.Generates Unique Uuids 518ms
Deduplication.Different Events Same Content Different Uuids 513ms
Deduplication.Preserves Uuid On Retry 6517ms
Deduplication.Preserves Timestamp On Retry 6520ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6525ms
Deduplication.No Duplicate Events In Batch 518ms
Header Behavior On Retry.Attempt Header Starts At One 511ms
Header Behavior On Retry.Attempt Header Increments On Retry 13523ms
Header Behavior On Retry.Request Id Preserved On Retry 6521ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3020ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6518ms
Response Format Validation.Success Response Has Uuid Keyed Results 512ms
Response Format Validation.Success Response Has Ok For Each Event 515ms
Response Format Validation.Success No Retry After When All Ok 513ms
Response Format Validation.Success Retry After Present When Retry Events 1516ms
Response Format Validation.Success No Retry After When Drop Only 513ms
Response Format Validation.Response Echoes Request Id 511ms
Retry Behavior.Retries On 408 6518ms
Retry Behavior.Retries On 500 6516ms
Retry Behavior.Retries On 503 8523ms
Retry Behavior.Retries On 504 6520ms
Retry Behavior.Retryable Errors Have Retry After 3516ms
Retry Behavior.Respects Retry After On Retryable Error 11524ms
Retry Behavior.Does Not Retry On 400 2514ms
Retry Behavior.Does Not Retry On 401 2513ms
Retry Behavior.Does Not Retry On 402 2513ms
Retry Behavior.Does Not Retry On 413 2514ms
Retry Behavior.Does Not Retry On 415 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After 2515ms
Retry Behavior.Implements Backoff 22525ms
Retry Behavior.Max Retries Respected 22527ms
Partial Batch Handling.Handles 200 Full Success 2513ms
Partial Batch Handling.Handles 200 With All Ok 3519ms
Partial Batch Handling.Does Not Retry Dropped Events 3514ms
Partial Batch Handling.Does Not Retry Limited Events 3516ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6518ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6521ms
Partial Batch Handling.Retries Only Retry Events From Partial 6523ms
Partial Batch Handling.Partial Retry Preserves Uuids 6519ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6521ms
Partial Batch Handling.Partial Retry Request Id Preserved 6518ms
Partial Batch Handling.Respects Retry After On Partial 8522ms
Partial Batch Handling.Unknown Result Treated As Terminal 3516ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3519ms
Compression.Sends Gzip Content Encoding 511ms
Compression.No Content Encoding When Disabled 511ms
Compression.Compressed Body Is Decompressible 510ms
Error Handling.Does Not Retry On Unknown 4Xx 2514ms
Event Options.Cookieless Mode Override 511ms
Event Options.Disable Skew Correction Override 510ms
Event Options.Process Person Profile Override 512ms
Event Options.Product Tour Id Override 510ms
Event Options.Unset Options Omitted 511ms
Event Options.Options Override In Batch 514ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 510ms
Geoip And Historical Migration.Historical Migration Set In Body 510ms
Geoip And Historical Migration.Historical Migration Absent By Default 511ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 12ms
Request Payload.Flags Request Uses V2 Query Param 10ms
Request Payload.Flags Request Hits Flags Path Not Decide 10ms
Request Payload.Flags Request Omits Authorization Header 11ms
Request Payload.Token In Flags Body Matches Init 10ms
Request Payload.Groups Round Trip 11ms
Request Payload.Groups Default To Empty Object 10ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10ms
Request Payload.Disable Geoip Omitted Defaults To False 11ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10ms
Request Lifecycle.No Flags Request On Init Alone 4ms
Request Lifecycle.No Flags Request On Normal Capture 510ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 16ms
Request Lifecycle.Mock Response Value Is Returned To Caller 12ms
Retry Behavior.Retries Flags On 502 315ms
Retry Behavior.Retries Flags On 504 314ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 513ms

@marco-g-pm
marco-g-pm removed the request for review from a team August 5, 2026 17:55
@marco-g-pm

Copy link
Copy Markdown
Contributor Author

claude got ahead of himself

@marco-g-pm marco-g-pm closed this Aug 5, 2026
@marco-g-pm
marco-g-pm deleted the marco/agents-per-run-identity branch August 5, 2026 17:56
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.

1 participant