Skip to content

fix(cost): tier Anthropic cache reads by the whole prompt size - #5730

Open
eeshsaxena wants to merge 1 commit into
Helicone:mainfrom
eeshsaxena:fix/anthropic-cached-input-tier
Open

fix(cost): tier Anthropic cache reads by the whole prompt size#5730
eeshsaxena wants to merge 1 commit into
Helicone:mainfrom
eeshsaxena:fix/anthropic-cached-input-tier

Conversation

@eeshsaxena

Copy link
Copy Markdown

Anthropic cache reads are always priced from the base tier, so a long prompt is billed at half rate.

Problem

getThresholdValueFunction returns 0 for cachedInputCost on the anthropic branch (it falls through to default), so getPricingTier always resolves to tier 0 — even though inputCost and outputCost on the same request use the prompt total.

For claude-sonnet-4-20250514, tier 200000 doubles the input rate (0.000003 -> 0.000006) and inherits the cachedInput: 0.1 multiplier. A 250K prompt with a 240K cache read:

charged correct
cachedInputCost $0.072 $0.144

inputCost on that same request is already tiered correctly, so only the cache read is wrong.

Why this looks like an oversight

vertex, google-ai-studio and xai all list cachedInputCost in their threshold cases; only anthropic omits it. The existing Gemini over-threshold test asserts its cache read at the higher tier (25000 * 0.000004 * 0.1, commented "Total prompt = input + cachedInput = 325000 (over threshold)"), while the Anthropic one asserted the base rate for the same situation. Anthropic's published pricing also tiers cache reads above 200K.

Change

Add cachedInputCost to the anthropic case so it uses the same prompt total as inputCost/outputCost.

I also updated the existing over-threshold assertion, since it had encoded the old behaviour, and added a mostly-cached long prompt as a regression test. I verified both fail against the current code and pass with the change.

Notes

  • Cache writes (cacheWrite5m/1h) still price from basePricing unconditionally for every provider, so they ignore tiers too. That looked like a separate question so I left it alone and noted it in the test.
  • Doesn't overlap fix(cost): apply tier pricing for openai/azure/openrouter/helicone (#5690) #5691, which adds openai/azure/openrouter/helicone cases; this only touches the existing anthropic block.
  • __tests__/cost passes (188). The 3 registrySnapshots failures reproduce on a clean checkout in my environment and are unrelated.

getThresholdValueFunction returns 0 for cachedInputCost on the anthropic
branch, so getPricingTier always resolves to tier 0 and a cache read is
priced at the base input rate no matter how large the prompt is. inputCost
and outputCost on the same request already use the prompt total.

For claude-sonnet-4 a 250K-token prompt with a 240K cache read is billed
$0.072 instead of $0.144 - half rate - because the >200K tier doubles the
input rate the cache multiplier applies to.

vertex, google-ai-studio and xai all include cachedInputCost in their
threshold cases, and the Gemini over-threshold test asserts the higher tier
for its cache read, so anthropic looks like an oversight rather than intent.

Updates the existing over-threshold assertion, which had encoded the old
behaviour, and adds a mostly-cached long prompt as a regression test.
@vercel

vercel Bot commented Jul 23, 2026

Copy link
Copy Markdown

@eeshsaxena 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.

1 participant