Skip to content

fix(billing): price Anthropic 1-hour cache writes separately - #1201

Open
atirna wants to merge 1 commit into
weave-os:mainfrom
atirna:fix/anthropic-1h-cache-writes
Open

fix(billing): price Anthropic 1-hour cache writes separately#1201
atirna wants to merge 1 commit into
weave-os:mainfrom
atirna:fix/anthropic-1h-cache-writes

Conversation

@atirna

@atirna atirna commented Sep 5, 2026

Copy link
Copy Markdown

Summary

  • Price Anthropic 1-hour cache writes at their 2x input rate while keeping 5-minute writes and aggregate-only payloads at 1.25x.
  • Carry the reported 1-hour split through streaming and non-streaming cost calculation into billing debits.

Why

The aggregate cache-creation count currently prices every write at the 5-minute rate, including the 1-hour tier.

Verification

  • Before, on current main: 10 fresh tokens plus 1M 1-hour cache writes at $5/M calculated to $6.250050 instead of $10.000050.
  • After: go test ./internal/router/catalog ./internal/observability/otel ./internal/translate ./internal/billing -count=1 and go test ./internal/proxy -count=1 pass. The catalog test covers 1-hour, mixed-tier, aggregate-only, and inconsistent usage breakdowns.

Fixes #867

Signed-off-by: Atirna <288419661+atirna@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Thank you so much for this, @atirna — this is a genuinely valuable fix. The 1-hour tier was silently under-billed, and you threaded the split through every surface (extractor, translator sink, handover usage, stream cost writer, billing debit) with careful edge handling (clamping, aggregate-only fallback, the web_search remainder). Really nice work, and make precommit was clean on your branch.

To match a couple of internal conventions I've re-opened it as #1203 with your code intact and you credited as co-author. This is not on you at all — these rules live in our AGENTS.md, keep evolving, and aren't obvious from the outside. The only deltas:

  • Comments describe the invariant, not the task (AGENTS.md → "Concise comments, sparingly": never reference current task/PR/caller). The test comments in cost_test.go referenced issue billing: 1-hour TTL cache writes are billed at the 5-minute 1.25x rate, undercharging by 37.5% #867 and narrated the pre-fix number ("vs $6.25005 before"); they now just state the pricing rule being guarded. Issue links belong in the PR description (Fixes #867 is there).
  • Consistent parameter order: routerCostCalculator / routerResponseCostFromPricing took the 1h count last, while catalog.EffectiveInputCost takes (cacheCreation, cacheCreation1h, cacheRead). I aligned them so the positional ints read the same everywhere.

Everything else — production logic, tests, structure — is exactly as you wrote it. Thanks again, and please keep them coming!

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.

billing: 1-hour TTL cache writes are billed at the 5-minute 1.25x rate, undercharging by 37.5%

1 participant