Bug Description
PostHog's automatic cost calculation cannot distinguish Anthropic 5-minute and 1-hour prompt-cache writes.
Anthropic reports an aggregate cache-write count plus a TTL breakdown, and supports both TTLs in one request:
{
"cache_creation_input_tokens": 248,
"cache_creation": {
"ephemeral_5m_input_tokens": 148,
"ephemeral_1h_input_tokens": 100
}
}
PostHog currently represents one aggregate count, $ai_cache_creation_input_tokens, and one ModelCost.cache_write_token price. calculateInputCost therefore applies one write rate to all 248 tokens. The configured rate and fallback are the Anthropic 5-minute rate (1.25 × base input price), while 1-hour writes cost 2 ×.
Let p be the base input price. For the synthetic usage above:
- Expected cache-write cost:
148 × 1.25p + 100 × 2p = 385p
- Current calculation:
248 × 1.25p = 310p
- Difference:
75p, or 19.48% of the correct cache-write cost
For a request containing only 1-hour writes, the cache-write component is under-reported by 37.5%.
How to reproduce
- Use the Anthropic usage payload above.
- Represent it with the current PostHog event property:
$ai_cache_creation_input_tokens: 248.
- Run automatic model-based cost calculation.
- Compare the resulting cache-write component (
310p) with Anthropic's TTL-specific calculation (385p).
The breakdown cannot be represented by the current ModelCost or custom-pricing properties, so a single custom cache-write price cannot make mixed-TTL requests exact.
Additional context
Anthropic documents the response fields, mixed-TTL requests, and the 1.25 × / 2 × pricing multipliers in its prompt-caching documentation.
One backward-compatible direction is to keep $ai_cache_creation_input_tokens for existing events and add TTL-specific counts and rates. Existing aggregate-only events would retain today's behavior; events with the breakdown would price each bucket separately. I am happy to contribute the implementation, focused regression tests for 5-minute, 1-hour, mixed, and legacy events, and the corresponding documentation after confirming the preferred property names.
Debug info
Source-level reproduction against PostHog master at a6ae50156b3535f6f7360d915d332f48b55b63cc.
Synthetic provider usage only; no project data is required.
Bug Description
PostHog's automatic cost calculation cannot distinguish Anthropic 5-minute and 1-hour prompt-cache writes.
Anthropic reports an aggregate cache-write count plus a TTL breakdown, and supports both TTLs in one request:
{ "cache_creation_input_tokens": 248, "cache_creation": { "ephemeral_5m_input_tokens": 148, "ephemeral_1h_input_tokens": 100 } }PostHog currently represents one aggregate count,
$ai_cache_creation_input_tokens, and oneModelCost.cache_write_tokenprice.calculateInputCosttherefore applies one write rate to all 248 tokens. The configured rate and fallback are the Anthropic 5-minute rate (1.25 ×base input price), while 1-hour writes cost2 ×.Let
pbe the base input price. For the synthetic usage above:148 × 1.25p + 100 × 2p = 385p248 × 1.25p = 310p75p, or19.48%of the correct cache-write costFor a request containing only 1-hour writes, the cache-write component is under-reported by
37.5%.How to reproduce
$ai_cache_creation_input_tokens: 248.310p) with Anthropic's TTL-specific calculation (385p).The breakdown cannot be represented by the current
ModelCostor custom-pricing properties, so a single custom cache-write price cannot make mixed-TTL requests exact.Additional context
Anthropic documents the response fields, mixed-TTL requests, and the
1.25 ×/2 ×pricing multipliers in its prompt-caching documentation.One backward-compatible direction is to keep
$ai_cache_creation_input_tokensfor existing events and add TTL-specific counts and rates. Existing aggregate-only events would retain today's behavior; events with the breakdown would price each bucket separately. I am happy to contribute the implementation, focused regression tests for 5-minute, 1-hour, mixed, and legacy events, and the corresponding documentation after confirming the preferred property names.Debug info
Source-level reproduction against PostHog master at a6ae50156b3535f6f7360d915d332f48b55b63cc. Synthetic provider usage only; no project data is required.