Skip to content

feat(llma): add $ai_tokens_source property to detect token value overrides#444

Merged
andrewm4894 merged 5 commits intomasterfrom
andy/ai-tokens-source
Feb 25, 2026
Merged

feat(llma): add $ai_tokens_source property to detect token value overrides#444
andrewm4894 merged 5 commits intomasterfrom
andy/ai-tokens-source

Conversation

@andrewm4894
Copy link
Member

Summary

  • Adds $ai_tokens_source property ("sdk" or "passthrough") to all $ai_generation events across sync, async, and streaming capture paths
  • When a user overrides any token property ($ai_input_tokens, $ai_output_tokens, $ai_total_tokens, etc.) via posthog_properties, the property is set to "passthrough" -- otherwise "sdk"
  • Enables diagnosing cost calculation discrepancies caused by externally injected token values that differ from what the SDK computes from the provider response

Test plan

  • Unit tests for _get_tokens_source helper (10 parameterized cases covering None, empty, unrelated, and each token key)
  • Integration test for passthrough detection in Anthropic sync path
  • Assertion added to existing Anthropic sync and streaming tests for "sdk" value
  • Full AI test suite passes (276 passed, 8 skipped)

When users pass token properties (e.g. $ai_input_tokens) via
posthog_properties, these override the SDK-computed values. This new
$ai_tokens_source property ("sdk" or "passthrough") lets us distinguish
whether token values came from the SDK or were externally injected,
which is critical for diagnosing cost calculation discrepancies.
@andrewm4894 andrewm4894 self-assigned this Feb 25, 2026
@andrewm4894 andrewm4894 changed the title feat: add $ai_tokens_source property to detect token value overrides feat(llma): add $ai_tokens_source property to detect token value overrides Feb 25, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ce72100053

ℹ️ 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".

@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

posthog-python Compliance Report

Date: 2026-02-25 12:32:53 UTC
Duration: 159357ms

✅ All Tests Passed!

29/29 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 516ms
Format Validation.Event Has Uuid 1507ms
Format Validation.Event Has Lib Properties 1507ms
Format Validation.Distinct Id Is String 1506ms
Format Validation.Token Is Present 1507ms
Format Validation.Custom Properties Preserved 1506ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9518ms
Retry Behavior.Does Not Retry On 400 3505ms
Retry Behavior.Does Not Retry On 401 3507ms
Retry Behavior.Respects Retry After Header 9514ms
Retry Behavior.Implements Backoff 23527ms
Retry Behavior.Retries On 500 7500ms
Retry Behavior.Retries On 502 7513ms
Retry Behavior.Retries On 504 7513ms
Retry Behavior.Max Retries Respected 23516ms
Deduplication.Generates Unique Uuids 1509ms
Deduplication.Preserves Uuid On Retry 7510ms
Deduplication.Preserves Uuid And Timestamp On Retry 14524ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7507ms
Deduplication.No Duplicate Events In Batch 1504ms
Deduplication.Different Events Have Different Uuids 1506ms
Compression.Sends Gzip When Enabled 1507ms
Batch Format.Uses Proper Batch Structure 1506ms
Batch Format.Flush With No Events Sends Nothing 1005ms
Batch Format.Multiple Events Batched Together 1505ms
Error Handling.Does Not Retry On 403 3508ms
Error Handling.Does Not Retry On 413 3507ms
Error Handling.Retries On 408 7512ms

andrewm4894 added a commit to PostHog/posthog-js that referenced this pull request Feb 25, 2026
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).
@andrewm4894 andrewm4894 requested a review from a team February 25, 2026 12:28
andrewm4894 added a commit to PostHog/posthog-js that referenced this pull request Feb 25, 2026
…des (#3147)

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).
@andrewm4894 andrewm4894 merged commit 150e24b into master Feb 25, 2026
21 checks passed
@andrewm4894 andrewm4894 deleted the andy/ai-tokens-source branch February 25, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants