Skip to content

feat(sdk): show credit limit and usage on the tokens page - #2069

Open
rohanchkrabrty wants to merge 5 commits into
mainfrom
feat/sdk-tokens-credit-limit
Open

rohanchkrabrty wants to merge 5 commits into
mainfrom
feat/sdk-tokens-credit-limit

Conversation

@rohanchkrabrty

@rohanchkrabrty rohanchkrabrty commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Postpaid orgs (credit_min < 0) can spend tokens until their balance reaches credit_min. The tokens page showed the raw balance, which goes negative as soon as a postpaid org uses tokens on credit. This PR changes the card for postpaid orgs to show what they can still spend.

Postpaid card

Label Value
Available Limit balance - credit_min. This is the same check the backend runs before it allows a debit.
Credit limit -credit_min
Used token max(0, -balance): tokens spent on credit and not yet paid back through an overdraft invoice

Usage banner

Condition Message
Available Limit is 0 or less "You’ve reached your credit limit. To continue using the platform, please clear your pending invoice or add more tokens."
Otherwise, used is 1% or more "You’ve consumed X% of your credit limit." X is floor(used / limit * 100).
Otherwise No banner

Everyone who can open the tokens page sees the banner. The old callout showed only to billing admins. The banner replaces the old postpaid callout about adding tokens. Its info icon uses currentColor, so it follows the text colour in dark mode.

Loading

Nothing on the page moves while it loads, except the banner when it appears.

  • The placeholders use the line heights of the text they stand in for.
  • Used token shows a placeholder until the balance loads. Credit limit shows its value as soon as the row appears.
  • The table renders once the billing account is known. A postpaid card is taller, so the card reaches its final height before the table appears.
  • FrontierContext now counts the billing account as loading while the list of billing accounts loads too. Before, there was a short gap after the org loaded where nothing was loading, and the card showed "Available tokens 0".

Prepaid orgs

Prepaid orgs keep the current "Available tokens" card, which shows the balance.

All values come from data the page already loads (GetBillingAccount with billing details, and GetBillingBalance). There are no new API calls.

Test plan

Tested in the client demo against a dev Frontier. Credit limit and balance were rewritten in the browser for each case.

  • tsc shows no type errors in the tokens view
  • eslint on the tokens view shows only one warning, on the Filter button, which was there before this change
  • Postpaid, 44% used: the banner says 44%, and the card shows 560,000 / 1,000,000 / 440,000
  • Postpaid, positive balance: Used token is 0 and no banner shows
  • Postpaid, 0.5% used: no banner, because it rounds down to 0%
  • Postpaid, 1 token left: "You've consumed 99% of your credit limit."
  • Postpaid, at or over the limit: the limit-reached message shows
  • Dark mode: the banner icon uses the same colour as the banner text
  • Cold load, postpaid and prepaid: the browser layout-shift API reports no shift of the card or the table. It reports only the banner and the mount of the DataTable loader.
  • Prepaid: the card shows "Available tokens" only, with no bottom row and no banner

Postpaid orgs can spend tokens down to their credit limit, so the
tokens page now shows the limit they have left instead of the raw
balance, which goes negative once they use tokens on credit.

- Available Limit is the balance plus the credit limit.
- Credit limit is -credit_min, and Used token is the negative part of
  the balance: tokens spent on credit and not yet paid back.
- A banner shows the percent of the credit limit used, when it is
  above 0.

Prepaid orgs keep the current Available tokens card.
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
frontier Ready Ready Preview Dec 3, 2026 11:27am UTC

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between bcfa14a and 47a87c2.

Files selected for processing: 2

  • web/sdk/client/contexts/FrontierContext.tsx
  • web/sdk/client/views/tokens/tokens-view.tsx

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Postpaid organizations can view their credit limit, used tokens, and available limit. A usage message appears when tokens have been used, or when the limit is reached.
    • Prepaid organizations continue to see their available token balance. Balance and credit amounts use locale-appropriate number formatting.
  • Style
    • The balance panel has updated background and header styling.
  • Updates
    • The previous postpaid callout about adding tokens to reduce a future invoice is no longer displayed.

TokensView calculates available tokens and postpaid credit usage from the token balance and credit minimum. It displays credit amounts and usage messages, updates the balance panel layout, and applies new panel styling.

Changes: Token balance display

Layer / File(s) Summary
Credit calculations
web/sdk/client/views/tokens/tokens-view.tsx
TokensView derives postpaid status, available tokens, used tokens, usage percentage, and a usage message from the balance and creditMin.
Balance panel and callout
web/sdk/client/views/tokens/tokens-view.tsx, web/sdk/client/views/tokens/tokens-view.module.css, web/sdk/client/assets/icons/InfoFilledIcon.tsx
The panel displays available limit and credit amounts for postpaid organizations, or available tokens otherwise. A callout displays the usage message when available. The panel layout and styling change, and the view uses a new filled info icon.

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

Change: Feature

Suggested reviewers: paansinghcoder

Merge Risk: Low · previous review bcfa1

The postpaid credit display can briefly or wrongly show misleading values when balance or billing data is loading, missing, or failed. It also no longer reminds users that adding tokens does not settle overdue invoices. These are low-impact display issues that should be addressed, but they are not blocking.

Pre-merge checks: Passed 2

Check name Status Explanation
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.
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 98f0010 and bcfa14a.

Files selected for processing: 1

  • web/sdk/client/views/tokens/tokens-view.tsx

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments

Minor 2
  1. Keep usage in a loading state until the balance is available.
  2. A failed balance request shows the full credit limit as available.
Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9c302eb and 7c7f1f1.

Files ignored due to path filters: 1

  • web/sdk/client/assets/info-filled.svg is excluded by !**/*.svg
Files selected for processing: 2

  • web/sdk/client/views/tokens/tokens-view.module.css
  • web/sdk/client/views/tokens/tokens-view.tsx

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

const creditMin = billingDetails?.creditMin ?? BigInt(0);
const isPostpaid = creditMin < BigInt(0);
const creditLimit = -creditMin;
const availableTokens = isPostpaid ? tokenBalance - creditMin : tokenBalance;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

A failed balance request shows the full credit limit as available.

When the getBillingBalance request fails, useTokens returns a tokenBalance of 0, and isTokensLoading becomes false. For a postpaid organization, tokens-view.tsx:168 then sets availableTokens to the full credit limit. The headline at line 227 shows that amount. The usage callout stays hidden because usage is 0. The card shows no error, so the user sees a full available limit after a failed lookup.

Do not render the derived available amount, the used amount, or the usage callout when the balance request has failed. Show a placeholder instead. useTokens does not currently return the query error, so the view cannot make this distinction. Expose the error state from the hook, then use it in the view.

@coveralls

coveralls commented Dec 2, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 47095959348

Coverage remained the same at 56.465%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 42407
Covered Lines: 23945
Line Coverage: 56.46%
Coverage Strength: 22.02 hits per line

💛 - Coveralls

Once the available limit is 0 or less, the backend refuses new usage.
The tokens page now shows a message that asks the user to clear the
pending invoice or add tokens, in place of the percent used.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments

Minor 2
  1. Restore the invoice settlement warning.
  2. Postpaid credit display can fall back to prepaid when billing details are absent.
Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7c7f1f1 and 642d1b5.

Files selected for processing: 1

  • web/sdk/client/views/tokens/tokens-view.tsx

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

);
// a postpaid org has a negative credit_min: its balance may go down to it,
// so the tokens it can still spend are the balance plus the credit limit
const creditMin = billingDetails?.creditMin ?? BigInt(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Postpaid credit display can fall back to prepaid when billing details are absent.

tokens-view.tsx derives isPostpaid from billingDetails?.creditMin. The fallback to 0 applies whenever billingDetails is undefined. isBillingAccountLoading covers only the loading window. It does not cover a failed billing-account request, and it does not cover a disabled query when no billing account exists. In those states the view shows the prepaid "Available tokens" card and hides the postpaid credit rows and usage message, even if the token balance is negative.

Treat missing billing details as a distinct state. Show a loading or error state for the postpaid rows instead of falling back to the prepaid display.

<Text size="micro">{formatNumber(usedTokens)}</Text>
</Flex>
</Flex>
) : null}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Restore the invoice settlement warning.

The removed callout explained that adding tokens does not settle existing or overdue invoices. The new message offers adding tokens without that qualification. A user can add tokens while an invoice remains unpaid. Keep the warning available to authorized postpaid users.

… admins

The info icon is now an inline SVG that uses currentColor, so it follows
the callout text colour in dark mode. The usage banner shows only to
members with billing permission, the same as the old callout. Available
tokens is now always balance minus credit_min, and both messages use the
same apostrophe.
Access to the tokens page already limits who sees it, so the banner does
not need its own billing permission check.

@rohilsurana rohilsurana left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The math looks right. I ran this PR on a local Frontier with real billing data for six orgs, and every state showed what the description says:

State Balance Page
Postpaid, 44% used -440,000 "consumed 44%" banner, 560,000 / 1,000,000 / 440,000
Postpaid, positive balance +50,000 no banner, used 0
Postpaid, 0.5% used -5,000 no banner
Postpaid, 1 token left -999,999 "consumed 99%" banner
Postpaid, over the limit -1,050,000 limit reached message
Prepaid +250,000 only "Available tokens"

Dark mode works, and the page makes no new API calls.

A few notes inline. One more outside the diff: isLoading at line 131 does not include the listBillingAccounts call, so a postpaid org can briefly see "Available tokens 0" before the postpaid card shows up. That was there before this PR.

Question: in the high contrast banner the info icon turns neutral instead of accent blue. Is that the intended design?

const creditMin = billingDetails?.creditMin ?? BigInt(0);
const isPostpaid = creditMin < BigInt(0);
const creditLimit = -creditMin;
const availableTokens = tokenBalance - creditMin;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the balance call fails, useTokens falls back to a balance of 0. A postpaid org then sees its full limit, 0 used and no banner, so the page looks healthy and can hide a reached limit. The fallback was there before, but it matters more now that the page reports usage. Could useTokens return an error flag, and the card show "-" and no banner in that case?

tokenBalance,
config?.locale
);
// the backend lets the balance go down to credit_min, so the tokens left to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can get rid of these comments, here and at lines 170 and 175. They explain how we got to this logic, which belongs in the PR description. Names like isCreditLimitReached already say what the code does.

// nothing more can be spent once the available limit is 0 or less
const isCreditLimitReached = isPostpaid && availableTokens <= BigInt(0);
const usageMessage = isCreditLimitReached
? 'You’ve reached your credit limit. To continue using the platform, please clear your pending invoice or add more tokens.'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tried a postpaid org that gets an overdraft invoice and then adds tokens. Creating the invoice does not change the balance, only paying it does. So after a top-up bigger than the debt, the card shows an available limit above the credit limit, 0 used and no banner, while the invoice is still unpaid. The old callout said adding tokens does not settle overdue invoices. Was dropping that note intended? It may be worth showing something when an overdraft invoice is open.

<Flex
className={isPostpaid ? styles.balanceHeader : undefined}
justify="between"
align="start"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

align="start" also moves the Add tokens button to the top of the card for prepaid orgs, which have no bottom row. Should this be start only for postpaid?

The used-token value shows a placeholder while the balance loads, and the
placeholders use the line heights of the text they stand in for. The table
renders once the billing account is known, so the postpaid row of the
card cannot push it down. The billing account counts as loading while the
list of billing accounts loads too, so the card no longer shows 0 for a
moment. Comments that repeated the code are removed.

This branch was successfully deployed

1 active deployment
Preview — 47a87c23 Deployed Dec 3, 2026 by vercel[bot]
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.

3 participants