Skip to content

fix(cost): tier cache-write on the same threshold as input cost - #5779

Open
Taranum01 wants to merge 1 commit into
Helicone:mainfrom
Taranum01:fix/5766-cache-write-tier
Open

fix(cost): tier cache-write on the same threshold as input cost#5779
Taranum01 wants to merge 1 commit into
Helicone:mainfrom
Taranum01:fix/5766-cache-write-tier

Conversation

@Taranum01

Copy link
Copy Markdown

Fixes #5766

packages/cost/models/calculate-cost.ts priced cacheWrite5mCost and cacheWrite1hCost directly off the base pricing tier (preprocessedPricing[0]), while inputCost and outputCost routed through getPricingTier(... getThresholdValue(...)) to pick the correct tier.

For Anthropic's long-context threshold (> 200k tokens), cache writes count toward the same total-prompt that drives the inputCost threshold. Pricing writes at the base tier undercharges them by ~2x on requests past 200k.

Select the same tier for cache writes that inputCost picks, keeping the cacheMultipliers lookup scoped to the chosen tier so a provider that varies write5m/write1h per tier still computes the correct multiplier.

Tests:

  • Updated packages/__tests__/cost/modelCostFromRegistry.test.ts:
    • should use higher tier pricing for Claude Sonnet 4 over 200K tokens: assert the cache-write cost at the long-context rate (5000 * 0.000006 * 1.25) instead of the previous base-tier value (5000 * 0.000003 * 1.25).

Fixes Helicone#5766

`packages/cost/models/calculate-cost.ts` priced `cacheWrite5mCost`
and `cacheWrite1hCost` directly off the base pricing tier
(`preprocessedPricing[0]`), while `inputCost` and `outputCost` routed
through `getPricingTier(... getThresholdValue(...))` to pick the
correct tier.

For Anthropic's long-context threshold (`> 200k` tokens), cache
writes count toward the same total-prompt that drives the inputCost
threshold. Pricing writes at the base tier undercharges them by ~2x
on requests past 200k.

Select the same tier for cache writes that inputCost picks, keeping
the `cacheMultipliers` lookup scoped to the chosen tier so a
provider that varies `write5m`/`write1h` per tier still computes
the correct multiplier.

Update `packages/__tests__/cost/modelCostFromRegistry.test.ts`:
- `should use higher tier pricing for Claude Sonnet 4 over 200K tokens`:
  assert the cache-write cost at the long-context rate
  (`5000 * 0.000006 * 1.25`) instead of the previous base-tier
  value (`5000 * 0.000003 * 1.25`).

Validated the fix numerically (250k-token input, 5k cache writes):
the post-fix long-context cost is exactly 2x the base-tier value, which
matches the issue's expectation.
@vercel

vercel Bot commented Aug 17, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Helicone Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

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-write cost ignores long-context pricing tier (uses base tier, ~2x undercharge on >200k Anthropic requests)

1 participant