Skip to content

fix(ccusage): avoid provider-specific Claude pricing fallback - #980

Closed
ryoppippi wants to merge 1 commit into
mainfrom
fix/claude-flat-pricing-match
Closed

fix(ccusage): avoid provider-specific Claude pricing fallback#980
ryoppippi wants to merge 1 commit into
mainfrom
fix/claude-flat-pricing-match

Conversation

@ryoppippi

@ryoppippi ryoppippi commented May 11, 2026

Copy link
Copy Markdown
Member

Summary

  • Restrict substring fallback for first-party Claude model names to first-party LiteLLM entries.
  • Avoid selecting provider-specific Bedrock/Vertex entries for first-party Claude lookups when direct LiteLLM flat entries should be used instead.
  • Keep pricing values sourced from LiteLLM rather than hardcoding Opus/Sonnet prices in ccusage.

Context

This replaces #898 with a narrower fix. Current Anthropic docs and LiteLLM data show 1M flat pricing for Opus 4.7, Opus 4.6, and Sonnet 4.6, but not every 4.5 model is flat. In particular, LiteLLM still has above-200k tier fields for Sonnet 4.5.

Testing

  • pnpm run format
  • pnpm typecheck
  • pnpm run test

Summary by CodeRabbit

  • Bug Fixes
    • Fixed pricing lookup for Claude models to ensure accurate pricing information by properly distinguishing first-party Claude models from provider-specific variants.

Review Change Stack

Restrict substring fallback for first-party Claude model names to first-party LiteLLM entries. This lets current direct LiteLLM flat pricing entries for Opus 4.6, Opus 4.7, and Sonnet 4.6 win without hardcoding their prices in ccusage.

Provider-specific entries such as Bedrock or Vertex can still be matched by exact provider-prefixed names, but first-party Claude lookups no longer fall through to those entries just because their key contains the requested model name.

Add regression coverage for preferring first-party Claude substring matches and returning no pricing when only a provider-specific substring match exists.
@coderabbitai

coderabbitai Bot commented May 11, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR refines model pricing lookup in LiteLLMPricingFetcher by distinguishing first-party Claude models during substring-based matching. A new helper detects claude-* model names, and a filter method restricts pricing entry candidates to first-party Claude keys when applicable, preventing incorrect matches against provider-specific Claude entries. Two test cases verify the filtering behavior.

Changes

Claude First-Party Model Pricing Filtering

Layer / File(s) Summary
Model Classification Helper
packages/internal/src/pricing.ts
New isFirstPartyClaudeModel helper identifies models with claude- prefix.
Entry Filtering Logic
packages/internal/src/pricing.ts
New createSubstringMatchingEntries method filters pricing entries to first-party Claude keys when the requested model is first-party Claude; otherwise returns all entries.
Substring Matching Integration
packages/internal/src/pricing.ts
getModelPricing substring matching iterates over filtered entries from createSubstringMatchingEntries instead of the full pricing map.
Test Coverage
packages/internal/src/pricing.ts
Two Vitest cases verify that substring matching selects first-party Claude pricing entries and returns null when only non-first-party Claude entries exist.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ryoppippi/ccusage#689: Modifies LiteLLMPricingFetcher's model-matching logic with broader normalization and provider-aware matching.
  • ryoppippi/ccusage#886: Adds provider-specific entries and speed-based multipliers to pricing logic in the same file.
  • ryoppippi/ccusage#743: Expands Claude model-name detection with additional prefixes (anthropic.claude-, anthropic/claude-).

Suggested labels

bug

Poem

🐰 First-party Claude now shines so bright,
No impostor entries stealing the night!
With filters precise and tests to delight,
Pricing lookup gets it just right. ✨

🚥 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 directly and clearly describes the main change: preventing provider-specific Claude pricing from being used as a fallback for first-party Claude model lookups, which aligns with the core objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/claude-flat-pricing-match

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.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

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

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
ccusage-guide f7c7f7f May 11 2026, 09:49 PM

@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@980

ccusage

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

@ccusage/codex

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

@ccusage/mcp

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

@ccusage/opencode

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

@ccusage/pi

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

commit: f7c7f7f

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/internal/src/pricing.ts`:
- Around line 501-539: Replace shorthand Claude model names in the two tests
with the repository's canonical Claude 4 names and exact LiteLLM pricing
entries: update the offlineLoader fixture keys (currently
'anthropic.claude-opus-4-6-v1' and 'claude-opus-4-6-20260205') to the matching
LiteLLM canonical names (e.g., use 'anthropic.claude-opus-4-20250514' / the
exact provider-prefixed key used in the repo) and change the getModelPricing
call argument from 'claude-opus-4-6' to the canonical model name (e.g.,
'claude-opus-4-20250514'), keeping the same assertions (expect
input_cost_per_token_above_200k_tokens to be undefined in the first test and
pricing toBeNull in the second). Ensure references are adjusted for
LiteLLMPricingFetcher and getModelPricing so the test names and fixtures exactly
match the repo’s Claude 4 naming rules.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3365d6c1-1613-4928-bcd5-4232d3548850

📥 Commits

Reviewing files that changed from the base of the PR and between 9f31ebc and f7c7f7f.

📒 Files selected for processing (1)
  • packages/internal/src/pricing.ts

Comment on lines +501 to +539
it('prefers first-party Claude substring matches over provider-specific entries', async () => {
using fetcher = new LiteLLMPricingFetcher({
offline: true,
offlineLoader: async () => ({
'anthropic.claude-opus-4-6-v1': {
input_cost_per_token: 5e-6,
output_cost_per_token: 2.5e-5,
input_cost_per_token_above_200k_tokens: 1e-5,
output_cost_per_token_above_200k_tokens: 5e-5,
},
'claude-opus-4-6-20260205': {
input_cost_per_token: 5e-6,
output_cost_per_token: 2.5e-5,
},
}),
});

const pricing = await Result.unwrap(fetcher.getModelPricing('claude-opus-4-6'));

expect(pricing?.input_cost_per_token_above_200k_tokens).toBeUndefined();
});

it('does not use provider-specific substring matches for first-party Claude models', async () => {
using fetcher = new LiteLLMPricingFetcher({
offline: true,
offlineLoader: async () => ({
'anthropic.claude-opus-4-6-v1': {
input_cost_per_token: 5e-6,
output_cost_per_token: 2.5e-5,
input_cost_per_token_above_200k_tokens: 1e-5,
output_cost_per_token_above_200k_tokens: 5e-5,
},
}),
});

const pricing = await Result.unwrap(fetcher.getModelPricing('claude-opus-4-6'));

expect(pricing).toBeNull();
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Align new test model names with repository Claude naming rules.

These new tests use shorthand names (claude-opus-4-6) instead of the repo’s required current Claude 4 names and exact LiteLLM-style entries. Please switch fixtures/queries to the mandated canonical names while keeping the same matching assertions.

As per coding guidelines, “All test files must use current Claude 4 models (claude-sonnet-4-20250514, claude-opus-4-20250514), not outdated Claude 3 models” and “Model names in tests must exactly match LiteLLM's pricing database entries.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/internal/src/pricing.ts` around lines 501 - 539, Replace shorthand
Claude model names in the two tests with the repository's canonical Claude 4
names and exact LiteLLM pricing entries: update the offlineLoader fixture keys
(currently 'anthropic.claude-opus-4-6-v1' and 'claude-opus-4-6-20260205') to the
matching LiteLLM canonical names (e.g., use 'anthropic.claude-opus-4-20250514' /
the exact provider-prefixed key used in the repo) and change the getModelPricing
call argument from 'claude-opus-4-6' to the canonical model name (e.g.,
'claude-opus-4-20250514'), keeping the same assertions (expect
input_cost_per_token_above_200k_tokens to be undefined in the first test and
pricing toBeNull in the second). Ensure references are adjusted for
LiteLLMPricingFetcher and getModelPricing so the test names and fixtures exactly
match the repo’s Claude 4 naming rules.

@ryoppippi

Copy link
Copy Markdown
Member Author

Closing this for now. Current LiteLLM pricing data already includes first-party flat entries for the affected Claude 1M-context models, so carrying extra matching logic in ccusage is not worth the added implementation surface.

@ryoppippi ryoppippi closed this May 11, 2026
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