Skip to content

fix(ccusage): add flat pricing for Opus 4.6 / Sonnet 4.6 to fix inflated costs - #898

Closed
MumuTW wants to merge 1 commit into
ccusage:mainfrom
MumuTW:fix/opus-1m-pricing
Closed

fix(ccusage): add flat pricing for Opus 4.6 / Sonnet 4.6 to fix inflated costs#898
MumuTW wants to merge 1 commit into
ccusage:mainfrom
MumuTW:fix/opus-1m-pricing

Conversation

@MumuTW

@MumuTW MumuTW commented Mar 18, 2026

Copy link
Copy Markdown

Summary

  • Fixes inflated cost calculation for Claude Opus 4.6 since Claude Code enabled 1M context in v2.1.75
  • Claude Opus 4.6 and Sonnet 4.6 use flat pricing across the full 1M context window — no premium for requests exceeding 200k input tokens (docs)
  • LiteLLM's dataset only has Bedrock entries (anthropic.claude-opus-4-6-v1) which include *_above_200k_tokens tiered pricing fields; when ccusage falls back to substring matching it picks up the Bedrock entry and incorrectly applies tiered pricing
  • Fix: inject entries keyed by the exact Anthropic API model names (claude-opus-4-6, claude-sonnet-4-6) with correct flat pricing so direct lookup succeeds before the substring fallback fires

Test plan

  • All 252 existing ccusage tests pass
  • All 9 internal pricing tests pass
  • Verify with a real Claude Code session that Opus 4.6 costs are no longer inflated

Fixes #891

Summary by CodeRabbit

  • New Features
    • Added support for injecting first-party flat pricing entries for specific Anthropic models so direct API lookups use precise flat rates.
  • Bug Fixes
    • Improved cost calculation accuracy for Claude Opus/Sonnet models when using direct Anthropic pricing.
  • Tests
    • Added tests verifying flat-pricing calculations and override behavior.

…ted costs

Claude Opus 4.6 and Sonnet 4.6 use flat pricing across the full 1M
context window — there is no premium for requests exceeding 200k input
tokens. However, LiteLLM's dataset only contains Bedrock entries
(e.g. `anthropic.claude-opus-4-6-v1`) which include
`*_above_200k_tokens` fields for Bedrock-specific tiered pricing.

When ccusage looks up pricing for `claude-opus-4-6`, the direct lookup
finds no match and the substring fallback picks up the Bedrock entry,
incorrectly applying tiered pricing and inflating costs.

Fix by injecting entries keyed by the exact Anthropic API model names
(`claude-opus-4-6`, `claude-sonnet-4-6`) with correct flat pricing.
Direct lookup now succeeds before the substring fallback fires.

Fixes #891
@coderabbitai

coderabbitai Bot commented Mar 18, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fd369978-7f61-4f24-9b29-c997972bc86a

📥 Commits

Reviewing files that changed from the base of the PR and between b2de7c8 and fdec0da.

📒 Files selected for processing (2)
  • apps/ccusage/src/_pricing-fetcher.ts
  • packages/internal/src/pricing.ts

📝 Walkthrough

Walkthrough

Introduces first‑party flat pricing entries for specific Anthropic/Claude API models and threads them into the LiteLLM pricing pipeline so direct-model lookups use the injected overrides before existing Bedrock substring-based matching.

Changes

Claude flat-pricing overrides

Layer / File(s) Summary
Flat pricing data
apps/ccusage/src/_pricing-fetcher.ts
Adds CLAUDE_FIRST_PARTY_FLAT_PRICING map with explicit LiteLLMModelPricing entries (input/output token costs, cache create/read costs, max token limits) for direct Anthropic models like claude-opus-4-6 and claude-sonnet-4-6.
PricingFetcher wiring
apps/ccusage/src/_pricing-fetcher.ts
Updates PricingFetcher construction to pass pricingOverrides: CLAUDE_FIRST_PARTY_FLAT_PRICING into the underlying LiteLLMPricingFetcher.
Override support in loader
packages/internal/src/pricing.ts
Adds pricingOverrides?: Record<string, LiteLLMModelPricing> to LiteLLMPricingFetcherOptions; stores the overrides and applies them to loaded/fetched pricing maps via applyPricingOverrides before caching/usage.
Prefetch / macro adjustment
apps/ccusage/src/_macro.ts
Merges the flat-pricing entries into the filtered dataset used by prefetchClaudePricing so direct API names resolve before Bedrock substring fallback.
Tests
apps/ccusage/src/_pricing-fetcher.ts (Vitest), packages/internal/src/pricing.ts (tests)
Adds unit tests confirming: cost calculation for claude-opus-4-6 uses the flat pricing (including cache creation/read cost), and that pricingOverrides are applied before substring matching with absent tiered fields remaining undefined.

Sequence Diagram

sequenceDiagram
  participant Macro as apps/ccusage/_macro
  participant Fetcher as PricingFetcher
  participant Lite as LiteLLMPricingFetcher
  participant Cache as Pricing Cache/Store

  Macro->>Fetcher: request pricing (prefetchClaudePricing)
  Fetcher->>Lite: construct with pricingOverrides (CLAUDE_FIRST_PARTY_FLAT_PRICING)
  Lite->>Cache: load or fetch pricing map
  Lite->>Lite: applyPricingOverrides(pricingOverrides) -> merged map
  Lite->>Cache: cache merged pricing
  Lite-->>Fetcher: return pricing (override entries first)
  Fetcher-->>Macro: deliver pricing (direct Claude entries present)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • #891: Inflated cost calculation since Opus 1M context enabled in 2.1.75 — this PR injects explicit 1M-context flat pricing for Opus models which may address the inflated-cost symptom described.

Possibly related PRs

Suggested reviewers

  • ryoppippi

Poem

🐇 Hop hop, I patched the fee,

Flat tokens set for Claude to be,
Overrides land, no fuzzy match,
Cost math sings — no pricing scratch. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(ccusage): add flat pricing for Opus 4.6 / Sonnet 4.6 to fix inflated costs' clearly and concisely summarizes the main change: adding flat pricing entries for specific Claude models to fix inflated cost calculations.
Linked Issues check ✅ Passed The pull request fully addresses issue #891's objective by implementing flat pricing for Claude Opus 4.6 and Sonnet 4.6 models to resolve inflated cost calculations caused by the 1M context enablement in v2.1.75.
Out of Scope Changes check ✅ Passed All code changes are directly scoped to resolving the inflated cost issue: adding pricingOverrides infrastructure to LiteLLMPricingFetcher, implementing flat pricing entries for targeted Claude models, and adding relevant test coverage.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented May 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

@ccusage/amp

npm i https://pkg.pr.new/ryoppippi/ccusage/@ccusage/amp@898

ccusage

npm i https://pkg.pr.new/ryoppippi/ccusage@898

@ccusage/codex

npm i https://pkg.pr.new/ryoppippi/ccusage/@ccusage/codex@898

@ccusage/mcp

npm i https://pkg.pr.new/ryoppippi/ccusage/@ccusage/mcp@898

@ccusage/opencode

npm i https://pkg.pr.new/ryoppippi/ccusage/@ccusage/opencode@898

@ccusage/pi

npm i https://pkg.pr.new/ryoppippi/ccusage/@ccusage/pi@898

commit: fdec0da

@ryoppippi
ryoppippi force-pushed the fix/opus-1m-pricing branch from fdec0da to b2de7c8 Compare May 11, 2026 21:46
@ryoppippi

Copy link
Copy Markdown
Member

Thank you for opening this and for digging into the inflated Opus 4.6 pricing issue.

After checking the current Anthropic pricing docs and the latest LiteLLM pricing dataset, I think we should not merge this as-is. LiteLLM now has first-party claude-opus-4-6, claude-opus-4-7, and claude-sonnet-4-6 entries with flat 1M-context pricing, so ccusage should avoid hardcoding those prices locally.

Also, this does not apply to every 4.5 model: claude-sonnet-4-5 still has above-200k tier fields in LiteLLM, while the 1M flat long-context pricing is currently documented for Opus 4.7, Opus 4.6, and Sonnet 4.6.

I will close this in favour of a narrower PR that relies on LiteLLM first-party flat entries and avoids falling through to provider-specific substring matches where that would select the wrong pricing. Sorry for the churn here, and thanks again for catching the underlying issue.

@ryoppippi

Copy link
Copy Markdown
Member

Closing this in favour of a narrower follow-up PR that relies on LiteLLM first-party flat pricing entries instead of hardcoding model prices in ccusage.

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.

Inflated cost calculation since Opus 1M context enabled in 2.1.75

2 participants