Skip to content

fix(extra-usage): correct unit assumptions for usage API response#388

Merged
sirmalloc merged 4 commits into
sirmalloc:mainfrom
roribio:fix/extra-usage-api-units
May 20, 2026
Merged

fix(extra-usage): correct unit assumptions for usage API response#388
sirmalloc merged 4 commits into
sirmalloc:mainfrom
roribio:fix/extra-usage-api-units

Conversation

@roribio
Copy link
Copy Markdown
Contributor

@roribio roribio commented May 18, 2026

Summary

Fixes #387.

  • ExtraUsageRemaining: extraUsageUsed is in cents (not dollars), so divide by 100 before subtracting from the limit. Previously the widget subtracted a cents value from a dollars value, producing an inflated "Overage Left" amount.
  • ExtraUsageUtilization: extraUsageUtilization is already a percentage (0–100), not a fraction. Removed the erroneous * 100 that caused the value to clamp to 100% almost immediately.

Test plan

  • Verify "Overage Left" shows a sane dollar value against a real pay-as-you-go account
  • Verify "Overage Utilization" matches the percentage reported by the Claude usage API
  • bun run lint passes
  • bun test passes

🤖 Generated with Claude Code

roribio and others added 4 commits May 15, 2026 12:12
The Anthropic usage API returns `extra_usage.used_credits` in cents (not
dollars) and `extra_usage.utilization` as a percent 0-100 (not a 0-1
fraction). The current widget code assumed the inverse, which made:

- ExtraUsageUtilization always clamp to 100.0% (multiplying a percent
  by 100 and clamping to [0,100])
- ExtraUsageRemaining always show $0.00 (subtracting cents from dollars
  goes negative, clamped to 0)

Drop the `* 100` in ExtraUsageUtilization and divide `extraUsageUsed`
by 100 in ExtraUsageRemaining so both widgets render real values.

Verified locally against a live API response where util=6.74 and
used=27087/limit=400000 — now renders "Overage: 6.8% | Overage Left:
\$3,728.54" instead of "Overage: 100.0% | Overage Left: \$0.00".
Adjust extraUsageUsed fixtures to cents and extraUsageUtilization
fixtures to 0-100 percentages so they exercise the corrected widget
math.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Update fetch and prefetch fixtures to reflect used credits as cents and extra usage utilization as a 0-100 percentage. Clarify the UsageData comments to match the widget contract.
@sirmalloc sirmalloc merged commit 4b314d5 into sirmalloc:main May 20, 2026
3 checks passed
@sirmalloc
Copy link
Copy Markdown
Owner

Thanks for the contribution here. The live API details you included were helpful.

I pushed a small follow-up commit with minor test/contract tweaks so the source-layer fixtures and UsageData comments match the units described in this PR:

  • extra_usage.used_credits is represented as cents
  • extra_usage.utilization is represented as a 0-100 percentage

The widget changes look good with that contract clarified. This will be included in the next release.

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.

Extra usage widgets use wrong unit assumptions for usage API response

2 participants