feat(ai): add $ai_tokens_source property to detect token value overrides#3147
feat(ai): add $ai_tokens_source property to detect token value overrides#3147andrewm4894 merged 1 commit intomainfrom
Conversation
Adds "sdk" or "passthrough" to $ai_generation events to flag when token properties are externally overridden via posthogProperties. Mirrors the same change in posthog-python (PostHog/posthog-python#444).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7303fa4de8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| $ai_trace_id: traceId, | ||
| $ai_base_url: baseURL, | ||
| ...params.posthogProperties, | ||
| $ai_tokens_source: getTokensSource(params.posthogProperties), |
There was a problem hiding this comment.
Add tokens source on all generation capture paths
This sets $ai_tokens_source only inside sendEventToPosthog, but $ai_generation events emitted by the LangChain callback handler still bypass this helper and call client.capture directly (packages/ai/src/langchain/callbacks.ts, around the $ai_generation capture block), so those events will never include the new property. Any analysis that segments by $ai_tokens_source will therefore silently drop/misclassify LangChain traffic, which undermines the new override-detection signal.
Useful? React with 👍 / 👎.
|
Size Change: +5.44 kB (+0.09%) Total Size: 6.17 MB
ℹ️ View Unchanged
|
Summary
$ai_tokens_sourceproperty ("sdk"or"passthrough") to all$ai_generationevents in@posthog/ai$ai_input_tokens,$ai_output_tokens,$ai_total_tokens, etc.) viaposthogProperties, the property is set to"passthrough"-- otherwise"sdk"Test plan
getTokensSourcehelper (10 parameterized cases covering undefined, empty, unrelated, and each token key)"sdk"value