Skip to content

Price Claude cache writes by 5m/1h token split in calculate mode#1190

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-cache-creation-cost
Draft

Price Claude cache writes by 5m/1h token split in calculate mode#1190
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-cache-creation-cost

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 29, 2026

calculate mode was undercounting Claude cache creation cost by pricing all cache_creation_input_tokens at the single LiteLLM cache-write rate (effectively 5m behavior). Claude JSONL records already provide a 5m/1h split, so 1h writes were not being charged at Anthropic’s 2× input rate.

  • Cost pipeline: add split-aware cache creation pricing

    • Introduced split-aware cost inputs in rust/crates/ccusage/src/cost.rs.
    • Added a dedicated path that prices:
      • ephemeral_5m_input_tokens at existing cache_create rate
      • ephemeral_1h_input_tokens at input * 2.0 (and input_above_200k * 2.0 when present)
      • any remainder at legacy cache_create rate (fallback compatibility).
  • Claude loaders: parse and pass cache split from JSONL

    • rust/crates/ccusage/src/adapter/claude/mod.rs now extracts usage.cache_creation.{ephemeral_5m_input_tokens,ephemeral_1h_input_tokens} from usage lines (including agent-progress envelope shape).
    • rust/crates/ccusage/src/adapter/claude/daily.rs now uses the same extraction path and forwards split data into cost calculation.
  • Public wiring and focused coverage

    • Exported the new split-aware cost helpers via rust/crates/ccusage/src/main.rs.
    • Added focused tests for:
      • 1h split pricing behavior vs legacy fallback
      • cache split parsing for both direct usage and agent-progress JSONL line shapes.
// Split-aware cache creation pricing
tiered_cost(ephemeral_5m, pricing.cache_create, pricing.cache_create_above_200k)
+ tiered_cost(
    ephemeral_1h,
    pricing.input * 2.0,
    pricing.input_above_200k.map(|v| v * 2.0),
)
+ tiered_cost(fallback_tokens, pricing.cache_create, pricing.cache_create_above_200k)

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 29, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
ccusage-guide 7ba3468 Commit Preview URL

Branch Preview URL
May 29 2026, 11:31 PM

Copilot AI changed the title [WIP] Fix underestimated cache creation cost for 1-hour cache writes Price Claude cache writes by 5m/1h token split in calculate mode May 29, 2026
Copilot AI requested a review from ryoppippi May 29, 2026 23:31
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.

Cache creation cost underestimated: 1-hour cache writes priced at 5m rate (1.25x) instead of 2x

2 participants