fix: show sub-cent costs in context panel instead of rounding to $0.00#600
fix: show sub-cent costs in context panel instead of rounding to $0.00#600VJ-yadav wants to merge 2 commits intoAltimateAI:mainfrom
Conversation
The cost display in the TUI context panel (sidebar and header) used Intl.NumberFormat with USD currency formatting, which rounds to 2 decimal places. This caused any cost below $0.005 to display as "$0.00", making it appear that cost never increases even as tokens accumulate. For typical LLM usage, a single message with 1K input tokens on Claude Sonnet costs ~$0.003, which was invisibly rounded away. After several messages the cost would eventually cross the $0.01 threshold, but for the first many interactions it misleadingly showed $0.00. Add Locale.cost() formatter with tiered precision: - $0 exactly -> "$0.00" - < $0.10 -> "$0.003" (up to 4 decimals, trailing zeros stripped) - >= $0.10 -> "$0.12" (standard 2 decimal places) Apply the formatter consistently across all cost displays: TUI sidebar, TUI header, stats command, trace list, and trajectory table. Fixes AltimateAI#585 Co-Authored-By: Vijay Yadav <vijay@studentsucceed.com>
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 4 minutes and 45 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughA new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/opencode/src/util/locale.ts`:
- Around line 101-111: The special sub-$0.10 formatting branch currently
triggers for any amount < 0.10, incorrectly handling negative values; update the
conditional in the block that checks amount (the "if (amount < 0.10)" branch) to
only apply to positive sub-cent amounts (e.g., amount > 0 && amount < 0.10) so
negative amounts fall through to the standard currency/Intl formatting path;
verify behavior for zero if desired and adjust condition to include/exclude 0
accordingly.
🪄 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: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 13fd3048-acff-4f9c-828e-8c49eb0ad3f3
📒 Files selected for processing (8)
packages/opencode/src/cli/cmd/stats.tspackages/opencode/src/cli/cmd/trace.tspackages/opencode/src/cli/cmd/trajectory.tspackages/opencode/src/cli/cmd/tui/routes/session/header.tsxpackages/opencode/src/cli/cmd/tui/routes/session/sidebar.tsxpackages/opencode/src/util/locale.tspackages/opencode/test/session/compaction.test.tspackages/opencode/test/util/locale.test.ts
Address CodeRabbit review: negative amounts were routed through the sub-cent branch instead of Intl.NumberFormat. Changed condition from `amount < 0.10` to `amount > 0 && amount < 0.10`. Added test for negative amounts. Co-Authored-By: Vijay Yadav <vijay@studentsucceed.com>
|
Working as expected. Check the issue details. |
Summary
Intl.NumberFormatUSD currency formatting which rounds to 2 decimal places, causing any cost below $0.005 to show as "$0.00" — making it appear cost never increases even as tokens accumulateLocale.cost()formatter with tiered precision: 4 decimal places for sub-10-cent costs (trailing zeros stripped), standard 2 decimal places for larger amountsstatscommand, trace list, and trajectory tableRoot Cause Analysis
A single LLM message with 1K input tokens on Claude Sonnet costs ~$0.003. The standard
Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })rounds this to$0.00. The cost IS being calculated correctly bySession.getUsage()(verified via Decimal.js multiplication of token counts × model pricing from models.dev), but the 2-decimal formatting hid it.The trace viewer (
viewer.tsline 321) already had a localfc()function with the correct 4-decimal behavior — this PR extracts the pattern into a sharedLocale.cost()utility and applies it everywhere.For free models (opencode provider with
apiKey: "public"), cost is genuinely $0.00 since those models havecost: { input: 0, output: 0 }in models.dev. The formatter correctly shows "$0.00" for truly zero costs.Test Plan
Locale.cost()unit tests: zero, sub-cent, under-10-cents, standard amounts, typical session costsSession.getUsage()tests: non-zero cost for small token counts (5K input + 1K output on Claude Sonnet pricing), zero cost for free modelslocale.test.tsandcompaction.test.tsChecklist
$0.003(1K input on Sonnet),$0.03(5K input + 1K output),$0.25(multi-message),$1.23(standard)fc()in viewer.tsFixes #585
Summary by CodeRabbit