Skip to content

bug(cost): cache reads are 88.8% of session cost and grow unbounded without compaction #159

Closed

Description

@devswha

Cache reads cost 88.8% of a long autonomous session and grow linearly per turn with no compaction, so total session cost is quadratic in turn count.

Evidence

App session 01a0aaca-e1c4-74fb-8a41-a85d5fd35954, anthropic/claude-opus-5, 681 billed turns from one user message:

total $140.95
  input       tok        1,362   $  0.01   ( 0.0% of cost)
  output      tok      270,351   $  6.76   ( 4.8%)
  cacheRead   tok  250,354,579   $125.18   (88.8%)   <---
  cacheWrite  tok    1,440,630   $  9.00   ( 6.4%)

Per-turn cacheRead distribution:

n=681  min=0  p50=372,687  p90=591,572  max=643,682  mean=367,628
turns with cacheRead > 200,000:  543  (80%)

It grows monotonically and never resets:

turns   0- 67   mean cacheRead =  96,013
turns  68-135   mean cacheRead = 171,231
turns 136-203   mean cacheRead = 231,823
turns 204-271   mean cacheRead = 279,001
turns 272-339   mean cacheRead = 347,362
turns 340-407   mean cacheRead = 401,708
turns 408-475   mean cacheRead = 459,981
turns 476-543   mean cacheRead = 513,873
turns 544-611   mean cacheRead = 554,887
turns 612-679   mean cacheRead = 616,335
turn  680       cacheRead      = 643,682

96K → 644K, a clean 6.7x linear ramp across the run. The transcript contains no compaction, prune or context-reset record in 681 turns — record types present are only session, model_change, thinking_level_change, service_tier_change, configured_model_chain, custom, message.

usage.cttl reports ephemeral1h.

Why this is a problem

Each turn re-reads the entire accumulated prefix. Summed over n turns that is O(n²) tokens, and cache reads are billed. The session paid $125.18 just to re-read its own history, against $6.76 of actual output.

The user issued one prompt. Nothing in the app warned that the run would cost $141, and nothing capped it.

For comparison, a 43-turn interactive session in the same repo on the same model:

CLI  43 turns  cacheRead 2,097,010  total $3.10

Same work shape, two orders of magnitude less cache read, because it never grew a 600K-token prefix.

Expected

  • Compact or window the context for long autonomous runs so per-turn cacheRead plateaus instead of ramping.
  • Surface accumulated session cost in the UI while the run is live.
  • Support a per-session spend ceiling that pauses the run and asks, rather than discovering the number afterwards in the transcript.

Note

This is the cost half of the problem. The pinned thinkingLevel: xhigh / serviceTier: priority visibility gap is filed separately.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions