Skip to content

fix(billing): bill the lowest graduated tier on zero usage - #5369

Closed
arian-gogani wants to merge 2 commits into
openmeterio:mainfrom
arian-gogani:codex/tiered-zero-usage-order
Closed

arian-gogani wants to merge 2 commits into
openmeterio:mainfrom
arian-gogani:codex/tiered-zero-usage-order

Conversation

@arian-gogani

@arian-gogani arian-gogani commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

GraduatedTiered.TieredPriceCalculator walks tiers through WithSortedTiers(), then, when quantity is zero, bills Tiers[0] from the unsorted slice and labels that range TierIndex: 0.

TieredPrice.Validate() does not require ascending UpToAmount. WithSortedTiers() exists because storage order is not an invariant. The zero-usage fallback was the one direct read of the unsorted slice, so a reversed tier list billed the open-ended tier instead of the lowest tier's flat fee.

Control

TestTieredOrderIndependence uses the same four tiers as the existing graduated fixture.

  • Ascending, zero usage: 100, the first tier's flat fee.
  • Reversed, before this change: 0. Validate() still accepts the reversed list, and Tiers[0] is the open-ended tier, which has a unit price and no flat fee.
  • Reversed, after sorting once and reading sortedPrice.Tiers[0]: 100.

Both orders now bill 100. I ran go test ./openmeter/billing/rating/service/rate/ -run TestTieredOrderIndependence.

Limits

This is the zero-usage path only. I did not run the billing database tests.

Made with Cursor

RetriggerView in GreptileConfidence Score: 5/5

This PR appears safe to merge.

Summary

Fixes zero-usage graduated billing to charge the lowest tier’s flat fee regardless of stored tier order.

  • Graduated-tier billing picks the lowest tier's flat fee when usage is zero.

Reviews (1) · Last reviewed commit: "test: consolidate graduated tier order r..." · Reviewed by Greptile

Summary by CodeRabbit

  • Bug Fixes
    • Corrected flat-price totals for zero usage when pricing tiers are stored in a different order. The total now remains consistent regardless of tier order.

@arian-gogani
arian-gogani requested a review from a team as a code owner October 26, 2026 17:39
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between aab3e5b and 22e9f32.

Files selected for processing: 2
  • openmeter/billing/rating/service/rate/tieredgraduated.go
  • openmeter/billing/rating/service/rate/tieredgraduated_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

The calculator now uses sorted tiers when selecting the tier for zero usage. A regression test reverses the tier order and checks that the total remains 100.0.

Changes: Graduated pricing

Layer / File(s) Summary
Sorted tier selection and regression test
openmeter/billing/rating/service/rate/tieredgraduated.go, openmeter/billing/rating/service/rate/tieredgraduated_test.go
Range construction and zero-quantity selection use the sorted tiers. The test reverses the tier order and checks that zero usage totals 100.0.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix

Possibly related PRs

Suggested labels: kind/bug, release-note/bug-fix

Suggested reviewers: turip

Merge Risk: Minimal · up to 22e9f

The fix correctly bills the lowest tier for zero usage regardless of tier order.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the zero-usage fallback for graduated tiers now bills the lowest tier. The raw summary says the code now selects the zero-quantity tier from the sorted order instead o…
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.
Full details: Title check

Explanation

The title names the main change: the zero-usage fallback for graduated tiers now bills the lowest tier. The raw summary says the code now selects the zero-quantity tier from the sorted order instead of the original order, which matches the title. The title is specific, concise, and free of file lists or vague terms. It uses the conventional commit prefix fix(billing), which fits a bug fix.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@arian-gogani

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate. This is the same change as #5347, which merged this morning and is already on main. I opened this against a stale base by mistake. Sorry for the noise.

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.

2 participants