Repository navigation
docs(capture-ai): specify how AI instrumentation attributes events to the active context - #125
Merged
Merged
Conversation
posthog-js (@posthog/ai), posthog-python and posthog-go all shipped the same behavior for the OpenTelemetry AI span processor: the active PostHog context is stamped onto the AI spans it exports. Record the attribution contract — the wire keys, the precedence order, the already-names-a-user carve-out, and the rule that the live span is never mutated. Proposed, applied and archived on this branch per AGENTS.md. Generated-By: PostHog Desktop Task-Id: d8d19349-0ed9-4718-adc6-359209e5c7f6
…ope posthog-js to the client option posthog-js reads the context only when PostHogSpanProcessor is given client, and resolves context properties at span end, so in-place mutation visibility is platform-specific. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
turnipdabeets
marked this pull request as ready for review
October 20, 2026 19:47
marandaneto
approved these changes
Oct 21, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
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.
Why
A server that traces LLM calls for many users through the PostHog OpenTelemetry span processor used to send every AI event under one person, or none — the only way to set the user was a process-wide resource attribute. Three SDKs fixed that the same way, in the same week, with the same semantics.
capture-aiis the spec that governs what PostHog's AI instrumentation emits, and it said nothing about attribution, so the contract existed in three implementations and nowhere in writing.Source changes
@posthog/aiPostHogSpanProcessorgains aclientoption and readswithContext/enterContextPostHogSpanProcessorreadsnew_context()/identify_context/tagotelSpanProcessorreadsRequestContextFromContextAll three: snapshot the context in
onStart, merge it into a copy of the span inonEnd, and leave the live span alone. Each one's first version wrote onto the live span and was changed after review, because that leaks the person and session to every other processor and exporter on the provider.What changed in the spec
New
capture-airequirement AI instrumentation attributes events to the active context (proposed, applied and archived on this branch per AGENTS.md), conditioned on the SDK having both an OTel AI bridge and a context primitive:posthog.distinct_id, context session →$session_id(the same key ascapture, so AI events join replays).$ai_session_idexplicitly stays free for the instrumentation's conversation id.posthog.distinct_id,user.id, or theai.telemetry/ai.settingsdistinct-id keys) keeps it.One sentence added to
Applicabilitynoting this requirement binds any SDK shipping the OTel AI bridge, which now includes posthog-go, whether or not it exposescapture_ai.openspec validate --specs --strictgoes from 155 to 156 warnings: the new requirement trips the same pre-existing "requirement text is very long" advisory as 155 others. No errors introduced.Uncertain / worth a look
tracescovers PostHog's own OTel-free tracing API and uses the logs wire keys (posthogDistinctId,sessionId); the AI bridge is OTel-native and uses the ingestion keys.capture-aiwas the closest fit, but a dedicated capability for the whole AI bridge (span-to-event mapping, redaction, exporter) may be the better long-term home — happy to move it.Not included, flagged for human review
metricscapability onmain— still open in Add metrics capability spec #30).Created with PostHog Desktop
🤖 Generated with Claude Code