Skip to content

fix(ai): report OpenAI cache write tokens - #1268

Merged
turnipdabeets merged 1 commit into
PostHog:mainfrom
alexeichhorn:fix/openai-cache-write-tokens
Nov 17, 2026
Merged

turnipdabeets merged 1 commit into
PostHog:mainfrom
alexeichhorn:fix/openai-cache-write-tokens

Conversation

@alexeichhorn

@alexeichhorn alexeichhorn commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

  • Problem: OpenAI bills prompt-cache writes from GPT-5.6 on: 1.25x the input rate for the default 30-minute cache and 2.5x for the 12-hour cache. It reports them in input_tokens_details.cache_write_tokens (Responses) or prompt_tokens_details.cache_write_tokens (Chat Completions). These tokens are part of input_tokens and are not in cached_tokens. The OpenAI wrapper reads only cached_tokens, so events never carry $ai_cache_creation_input_tokens, and ingestion prices cache writes as plain input tokens even though llm-costs.json has a cache_write_token rate for these models.
  • Solution: Read cache_write_tokens alongside cached_tokens in the converter's usage-field tables and map it to cache_creation_input_tokens. This covers Chat Completions and Responses, streaming and non-streaming, sync and async, because they all go through the same extractors. The shared capture code already emits $ai_cache_creation_input_tokens when the value is above zero. This matches @posthog/ai 8.6.8 (fix(ai): report cache-write tokens for OpenAI-compatible providers posthog-js#4189).

Out of scope

input_tokens_details.cache_write_tokens_details (ttl_30m_tokens / ttl_12h_tokens) is not mapped to $ai_cache_creation_5m_input_tokens / $ai_cache_creation_1h_input_tokens. Ingestion reads those properties only for Anthropic, and OpenAI's TTLs are 30m/12h rather than 5m/1h. The breakdown still reaches the backend in $ai_usage. Until ingestion prices 12-hour writes separately, they are costed at the cache_write_token rate.

💚 How did you test it?

  • Added test_responses_cache_write_tokens and test_chat_cache_write_tokens, each parametrized over stream=False/True. They use real openai SDK types through the wrapped client and assert $ai_input_tokens, $ai_cache_read_input_tokens, and $ai_cache_creation_input_tokens on the captured event. All four cases fail without the converter change.
  • make_response_usage now accepts cache_write_tokens.
  • pytest posthog/test/ai passes, along with ruff format --check, ruff check, mypy with the baseline filter, and python -W error -c "import posthog". In the full suite, only three fork-based tests in test_metrics_autocapture.py fail on macOS, and they fail the same way on main.
  • Not tested against the live OpenAI API.

📝 Checklist

  • All commits are signed (see Commit signing)
  • 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 file (same format as sampo add)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Written with Claude Code. A person scoped the change and directed the work.
  • Modeled on the posthog-js OpenAI cache-write fix: same source fields, same $ai_cache_creation_input_tokens property, and no new public API.
  • Checked PostHog ingestion (input-costs.ts) before deciding against mapping the 30m/12h split. See "Out of scope".

@alexeichhorn
alexeichhorn marked this pull request as ready for review November 17, 2026 12:07
@alexeichhorn
alexeichhorn requested a review from a team as a code owner November 17, 2026 12:07
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T12:12:24.937715Z eb88f7e Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@turnipdabeets
turnipdabeets requested a review from a team November 17, 2026 15:48
@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 115.72 KiB/op 115.72 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,107.6 count/op 1,107.61 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 64.27 µs/op 64.59 µs/op 0.5% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 36.2 MiB 36.23 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 37.92 MiB 37.96 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 37.92 MiB 37.96 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 66.5 µs/op 66.74 µs/op 0.4% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.25 MiB 40.25 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 168.28 KiB/op 168.28 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,100.58 count/op 1,100.57 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 88.03 µs/op 87.86 µs/op 0.2% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.11 MiB 41.03 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 46.17 MiB 46.09 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 46.17 MiB 46.09 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 86.61 µs/op 85.93 µs/op 0.8% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 50.98 MiB 50.92 MiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 119.86 KiB/op 119.85 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,147.49 count/op 1,147.46 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 73.62 µs/op 74.49 µs/op 1.2% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 36.12 MiB 36.17 MiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 36.21 MiB 36.26 MiB 0.2% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 36.21 MiB 36.26 MiB 0.2% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 76.57 µs/op 76.83 µs/op 0.3% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.25 MiB 40.25 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 172.74 KiB/op 172.74 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,140.32 count/op 1,140.33 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 91.8 µs/op 92.11 µs/op 0.3% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 40.91 MiB 40.95 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 40.91 MiB 40.95 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 40.9 MiB 40.95 MiB 0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 90.32 µs/op 91.07 µs/op 0.8% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.29 MiB 41.32 MiB <0.1% more memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 43.39 µs/op 43.27 µs/op 0.3% less time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 43.39 µs/op 43.48 µs/op 0.2% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 43.93 µs/op 43.15 µs/op 1.8% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 45.1 µs/op 43.15 µs/op 4.3% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 226.56 KiB/op 226.56 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,240.44 count/op 2,240.44 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 84.06 µs/op 84.27 µs/op 0.2% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 84.07 µs/op 84.28 µs/op 0.2% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,014.88 count/op 22,014.88 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 781.14 µs/op 779.21 µs/op 0.2% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 781.18 µs/op 779.26 µs/op 0.2% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.69 MiB/op 21.69 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 219,656.62 count/op 219,656.62 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.78 ms/op 7.76 ms/op 0.2% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.78 ms/op 7.76 ms/op 0.2% less time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 42.45 MiB 42.79 MiB 0.8% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 42.71 MiB 43.14 MiB 1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 42.71 MiB 43.14 MiB 1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.11 MiB 43.04 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 41.87 MiB 41.94 MiB 0.2% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 41.94 MiB 41.99 MiB 0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 41.29 MiB 41.65 MiB 0.9% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.28 MiB 42.35 MiB 0.2% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 16.63 KiB/op 16.63 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 164.77 count/op 164.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 10.71 µs/op 10.64 µs/op 0.6% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 10.71 µs/op 10.63 µs/op 0.7% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 25.49 KiB/op 25.49 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 241.28 count/op 241.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 14.08 µs/op 14 µs/op 0.5% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 14.07 µs/op 14 µs/op 0.5% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 40.7 KiB/op 40.7 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 390.27 count/op 390.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 19.81 µs/op 20.02 µs/op 1% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 19.81 µs/op 20.02 µs/op 1.1% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 61.48 KiB/op 61.48 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 586.27 count/op 586.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 28.27 µs/op 27.95 µs/op 1.1% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 28.26 µs/op 27.95 µs/op 1.1% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 60.11 KiB/op 60.11 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 569.77 count/op 569.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 26.87 µs/op 26.91 µs/op 0.2% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 26.87 µs/op 26.91 µs/op 0.2% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.93 KiB/op 19.93 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 194.28 count/op 194.28 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 11.64 µs/op 11.7 µs/op 0.6% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 11.64 µs/op 11.7 µs/op 0.6% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 21.56 KiB/op 21.56 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 199.78 count/op 199.78 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 12.52 µs/op 12.43 µs/op 0.7% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 12.52 µs/op 12.43 µs/op 0.7% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 15.79 KiB/op 15.79 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 154.77 count/op 154.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 10.04 µs/op 10.01 µs/op 0.3% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 10.04 µs/op 10.01 µs/op 0.3% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 16.79 KiB/op 16.79 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 168.77 count/op 168.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 10.94 µs/op 10.96 µs/op 0.2% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 10.94 µs/op 10.96 µs/op 0.2% more time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared f22511891225fb15a4e6a0f889e723e9b5d0ebe4 → eb88f7e61017166d9863e3e1df085eac5924d824.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@turnipdabeets turnipdabeets left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @alexeichhorn!

@turnipdabeets
turnipdabeets merged commit ac6a5df into PostHog:main Nov 17, 2026
55 of 56 checks passed
DanielVisca added a commit that referenced this pull request Nov 17, 2026
The cache_write_tokens tests added in #1268 were written before 8.0 moved AI events to capture_ai, so they read mock_client.capture and fail on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Generated-By: PostHog Desktop
Task-Id: a72b68c8-37a1-47ee-8b69-6c44f59250b2
eli-r-ph pushed a commit that referenced this pull request Nov 18, 2026
test(ai): read cache write token assertions from capture_ai

The cache_write_tokens tests added in #1268 were written before 8.0 moved AI events to capture_ai, so they read mock_client.capture and fail on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Generated-By: PostHog Desktop
Task-Id: a72b68c8-37a1-47ee-8b69-6c44f59250b2
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