Skip to content

Anthropic 1-hour prompt-cache writes use the 5-minute rate in auto-costing #71306

Description

@gouveags

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

  1. Use the Anthropic usage payload above.
  2. Represent it with the current PostHog event property: $ai_cache_creation_input_tokens: 248.
  3. Run automatic model-based cost calculation.
  4. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions