Skip to content

fix(opencode): resolve provider model pricing aliases#983

Merged
ryoppippi merged 1 commit into
mainfrom
fix/opencode-provider-model-pricing
May 11, 2026
Merged

fix(opencode): resolve provider model pricing aliases#983
ryoppippi merged 1 commit into
mainfrom
fix/opencode-provider-model-pricing

Conversation

@ryoppippi
Copy link
Copy Markdown
Owner

@ryoppippi ryoppippi commented May 11, 2026

Replaces one-off OpenCode model aliases with provider-aware pricing resolution.

OpenCode records providerID and modelID from its models.dev-backed model metadata. This preserves providerID in loaded entries, normalizes Claude minor-version IDs such as claude-opus-4.7 and claude-opus-41 algorithmically, and tries pricing candidates in an order that avoids zero-cost provider metadata when a canonical LiteLLM model price exists.

This supersedes #868.

Testing:

  • pnpm --filter @ccusage/opencode test
  • pnpm --filter @ccusage/opencode typecheck
  • pnpm run format
  • pnpm typecheck
  • pnpm run test

Summary by CodeRabbit

  • Bug Fixes

    • Improved cost calculation to try multiple normalized model name variants and provider-prefixed candidates, returning accurate nonzero pricing when available.
  • Data

    • Usage entries now include a provider identifier (defaults to "unknown" when missing).
  • Tests

    • Added tests covering model name normalization patterns, provider handling, and candidate selection ordering.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 62ea1d5d-063e-442f-b372-7b63fdc83165

📥 Commits

Reviewing files that changed from the base of the PR and between 03c15eb and ba407ff.

📒 Files selected for processing (2)
  • apps/opencode/src/cost-utils.ts
  • apps/opencode/src/data-loader.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/opencode/src/data-loader.ts
  • apps/opencode/src/cost-utils.ts

📝 Walkthrough

Walkthrough

The pull request adds a required providerID to OpenCode usage entries and uses it in cost calculation: cost-utils builds a deduplicated, normalized list of model/provider candidate names (including Claude naming variants and provider-prefixed forms) and iterates candidates to return the first positive LiteLLM pricing result; tests cover normalization and ordering.

Changes

Provider-aware cost calculation

Layer / File(s) Summary
Data shape
apps/opencode/src/data-loader.ts
LoadedUsageEntry type now includes required providerID: string.
Message conversion
apps/opencode/src/data-loader.ts
convertOpenCodeMessageToUsageEntry extracts providerID from the message or defaults to 'unknown'; tests assert providerID in full-message and missing-fields cases.
Normalization helpers
apps/opencode/src/cost-utils.ts
Adds helpers to normalize provider IDs (replace - with _), normalize Claude model-name variants via regex (dot-notation, compact minor versions, generation-number form), and build a deduplicated candidate list including base/normalized names and optional providerPrefix/model variants.
Cost calculation
apps/opencode/src/cost-utils.ts
calculateCostForEntry iterates the generated model/provider candidates and returns the first successful calculateCostFromTokens result with value > 0; returns 0 if none match.
Validation tests
apps/opencode/src/cost-utils.ts
Guarded Vitest tests validate Claude dot-notation, compact minor-version, future-generation normalizations, and provider-prefixed candidate selection ordering.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ryoppippi/ccusage#743: Expands Claude model-name detection to include anthropic provider prefixes, related to the normalization and provider-prefixed lookup logic here.

Poem

🐇 I hop through models, one by one,
Normalize Claude till the work is done —
Provider prefixes, dots, and small-times too,
Try each candidate until pricing comes through.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% 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 PR title directly relates to the main objective: resolving provider model pricing aliases by implementing provider-aware pricing resolution with normalization and candidate lookup ordering.
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/opencode-provider-model-pricing

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
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

Open in StackBlitz

@ccusage/amp

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

ccusage

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

@ccusage/codex

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

@ccusage/mcp

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

@ccusage/opencode

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

@ccusage/pi

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

commit: ba407ff

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 11, 2026

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 ba407ff May 11 2026, 10:11 PM

Preserve OpenCode provider IDs in loaded usage entries and try provider-aware pricing candidates when calculating costs.

Normalize Claude minor-version model IDs such as claude-opus-4.7 and claude-opus-41 algorithmically so new variants do not require one-off aliases.
@ryoppippi ryoppippi force-pushed the fix/opencode-provider-model-pricing branch from 03c15eb to ba407ff Compare May 11, 2026 22:10
@ryoppippi ryoppippi merged commit e663837 into main May 11, 2026
17 checks passed
@ryoppippi ryoppippi deleted the fix/opencode-provider-model-pricing branch May 11, 2026 22:18
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