Skip to content

fix(ai): omit aux token zeros when a stream reported no usage - #918

Merged
bernatixer merged 1 commit into
mainfrom
posthog/ai-omit-unreported-aux-usage
Sep 4, 2026
Merged

fix(ai): omit aux token zeros when a stream reported no usage#918
bernatixer merged 1 commit into
mainfrom
posthog/ai-omit-unreported-aux-usage

Conversation

@bernatixer

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

  • A stream interrupted before the provider reported any usage shows a known $0.00 cost instead of an unknown cost, for anyone on the OpenAI or Anthropic streaming wrappers.
  • #906 stopped fabricating the main token counts on that path, but capture_streaming_event still defaults auxiliary fields to zero: $ai_cache_read_input_tokens and $ai_reasoning_tokens for OpenAI, both cache fields for Anthropic.
  • Cost processing reads a reported 0 as a known count, so the fabricated zeros price the generation at $0.00. Absent means unknown; 0 is a report of nothing.
  • The JS SDK omits all usage fields on this path since posthog-js#4664. This aligns the Python SDK.

💚 How did you test it?

  • Reproduced against the released 7.45.3 first: an OpenAI-compatible SSE stream aborted after two chunks produced an event with $ai_cache_read_input_tokens: 0 and $ai_reasoning_tokens: 0 while the main counts were absent.
  • Two new cases in test_token_reporting.py pin the empty-usage path for openai and anthropic. Both fail on main with the fabricated zeros and pass with the fix.
  • Two more cases pin the historical zero-defaults when usage was reported. They pass on main and with the fix, so completed streams keep their current shape.
  • The full posthog/test/ai suite shows no new failures. Nine failures exist on main in this environment with and without the change; they are provider-key integration tests.
  • Not run: the provider integration tests, because this environment has no provider API keys.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Added a changeset under .sampo/changesets/ (hand-written in the sampo add format)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Written by Claude Code in a PostHog Desktop session directed by @bernatixer. The gap surfaced during post-release end-to-end verification of #906: the interrupted-stream case produced the auxiliary zeros while correctly omitting the main counts. The fix gates the existing zero-defaults on reported usage instead of removing them, because the code comments defend them as backward compatibility for streams that did report usage.

Skills invoked: writing-tests, writing-pr-descriptions.


Created with PostHog Desktop

capture_streaming_event defaults auxiliary token fields to zero: $ai_cache_read_input_tokens and $ai_reasoning_tokens for OpenAI, both cache fields for Anthropic. A stream interrupted before the provider reported any usage therefore sent fabricated zeros, and cost processing reads a reported zero as a known count, pricing the generation at a known $0.00 instead of leaving it unknown. #906 fixed the main token counts on this path but missed these fields.

The zero-defaults now apply only when the provider reported usage at all (input or output tokens present). Completed and partially reported streams keep the historical zeros, so nothing changes for them. This matches the JS SDK, which omits all usage fields on interrupted streams since PostHog/posthog-js#4664.

Generated-By: PostHog Desktop
Task-Id: ec0cc27f-fc40-4994-b153-2bfe74de1edf
@bernatixer bernatixer self-assigned this Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-03 10:44:30 UTC
Duration: 256510ms

✅ 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 519ms
Endpoint And Method.Does Not Use Legacy Endpoints 512ms
Required Headers.Has Authorization Bearer Header 510ms
Required Headers.Has Content Type Json 511ms
Required Headers.Has Posthog Sdk Info Format 512ms
Required Headers.Has Posthog Attempt Header 511ms
Required Headers.Has Posthog Request Id 512ms
Required Headers.Has Posthog Request Timestamp 511ms
Required Headers.Has User Agent 512ms
Body Format.Body Has Created At And Batch 511ms
Body Format.No Api Key In Body 514ms
Body Format.No Sent At In Body 511ms
Event Format.Event Has Required Root Fields 511ms
Event Format.Event Uuid Is Valid 512ms
Event Format.Event Timestamp Is Rfc3339 512ms
Event Format.Distinct Id Is String 511ms
Event Format.Distinct Id At Root Not Properties 510ms
Event Format.Custom Properties Preserved 512ms
Event Format.Set Properties Preserved 512ms
Event Format.Set Once Properties Preserved 511ms
Event Format.Groups Properties Preserved 512ms
Event Format.Sdk Generates Uuid If Not Provided 511ms
Event Format.Event Has Required Root Fields Batch 516ms
Event Format.Event Uuid Is Valid Batch 515ms
Event Format.Event Timestamp Is Rfc3339 Batch 515ms
Event Format.Distinct Id Is String Batch 514ms
Event Format.Distinct Id At Root Not Properties Batch 515ms
Event Format.Custom Properties Preserved Batch 515ms
Event Format.Set Properties Preserved Batch 515ms
Event Format.Set Once Properties Preserved Batch 515ms
Event Format.Groups Properties Preserved Batch 515ms
Event Format.Sdk Generates Uuid If Not Provided Batch 514ms
Batch Behavior.Multiple Events In Single Batch 519ms
Batch Behavior.Batch Envelope Smoke 517ms
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 519ms
Deduplication.Different Events Same Content Different Uuids 513ms
Deduplication.Preserves Uuid On Retry 6517ms
Deduplication.Preserves Timestamp On Retry 6516ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6526ms
Deduplication.No Duplicate Events In Batch 520ms
Header Behavior On Retry.Attempt Header Starts At One 511ms
Header Behavior On Retry.Attempt Header Increments On Retry 13521ms
Header Behavior On Retry.Request Id Preserved On Retry 6517ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3021ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6517ms
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 1517ms
Response Format Validation.Success No Retry After When Drop Only 513ms
Response Format Validation.Response Echoes Request Id 511ms
Retry Behavior.Retries On 408 6515ms
Retry Behavior.Retries On 500 6520ms
Retry Behavior.Retries On 503 8524ms
Retry Behavior.Retries On 504 6521ms
Retry Behavior.Retryable Errors Have Retry After 3517ms
Retry Behavior.Respects Retry After On Retryable Error 11519ms
Retry Behavior.Does Not Retry On 400 2512ms
Retry Behavior.Does Not Retry On 401 2513ms
Retry Behavior.Does Not Retry On 402 2513ms
Retry Behavior.Does Not Retry On 413 2515ms
Retry Behavior.Does Not Retry On 415 2512ms
Retry Behavior.Non Retryable Errors Have No Retry After 2514ms
Retry Behavior.Implements Backoff 22527ms
Retry Behavior.Max Retries Respected 22534ms
Partial Batch Handling.Handles 200 Full Success 2513ms
Partial Batch Handling.Handles 200 With All Ok 3515ms
Partial Batch Handling.Does Not Retry Dropped Events 3514ms
Partial Batch Handling.Does Not Retry Limited Events 3514ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6519ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6523ms
Partial Batch Handling.Retries Only Retry Events From Partial 6523ms
Partial Batch Handling.Partial Retry Preserves Uuids 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6523ms
Partial Batch Handling.Partial Retry Request Id Preserved 6522ms
Partial Batch Handling.Respects Retry After On Partial 8522ms
Partial Batch Handling.Unknown Result Treated As Terminal 3517ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3520ms
Compression.Sends Gzip Content Encoding 513ms
Compression.No Content Encoding When Disabled 510ms
Compression.Compressed Body Is Decompressible 511ms
Error Handling.Does Not Retry On Unknown 4Xx 2514ms
Event Options.Cookieless Mode Override 512ms
Event Options.Disable Skew Correction Override 511ms
Event Options.Process Person Profile Override 511ms
Event Options.Product Tour Id Override 511ms
Event Options.Unset Options Omitted 511ms
Event Options.Options Override In Batch 515ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 511ms
Geoip And Historical Migration.Historical Migration Set In Body 511ms
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 11ms
Request Payload.Flags Request Uses V2 Query Param 10ms
Request Payload.Flags Request Hits Flags Path Not Decide 11ms
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 12ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 11ms
Request Payload.Disable Geoip Omitted Defaults To False 12ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 11ms
Request Lifecycle.No Flags Request On Init Alone 4ms
Request Lifecycle.No Flags Request On Normal Capture 511ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 15ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10ms
Retry Behavior.Retries Flags On 502 314ms
Retry Behavior.Retries Flags On 504 314ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 512ms

@bernatixer
bernatixer requested a review from a team September 3, 2026 10:45
@bernatixer
bernatixer marked this pull request as ready for review September 3, 2026 10:46
@bernatixer
bernatixer requested a review from a team as a code owner September 3, 2026 10:46
@bernatixer
bernatixer merged commit 86ad9e7 into main Sep 4, 2026
45 checks passed
@bernatixer
bernatixer deleted the posthog/ai-omit-unreported-aux-usage branch September 4, 2026 07:10
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.

2 participants