Skip to content

feat(billing): show this period's AI usage on the Billing page - #1386

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/billing-ai-usage
Oct 23, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/billing-ai-usage

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The Billing page now shows this period's AI usage: how much of the plan's monthly AI allowance the workspace has used, what used it, and when it resets.

Until now nobody could see how much of the AI budget was left; the first sign was an "AI usage is used up" error in the editor or the Improve tab. The budget is the one plan.limits.improveMonthlyBudgetUsd (Starter $1 / Growth $5 / Enterprise $10 or 1% of the contract) that Improve, the Content Editor's AI and the Action Center's task writer all draw on.

Migration 00242

  • prompt_improve_usage.feature (improve | content | action_center, nullable): what each AI call was for.
    • Rows from before are filled from the item each still names (prompt_id / content_draft_id / action_id).
    • Rows whose item was deleted stay null and show as "Other".
  • ai_usage_by_feature(org, since) sums the period's spend by feature in the database. It is service role only. Unlike the JS sum, it has no 5,000-row cap.

Server

  • The three writers set feature:
    • routes/prompts.js (Improve);
    • lib/content-brief.js recordUsage (briefs, Content Agent, Start Agent);
    • lib/action-center/task-writer.
  • getAiUsage(orgId) (plan-guard.js) returns { plan, planName, periodStart, periodEnd, unlimited, percentUsed, features: [{ feature, percent }] }:
    • Shares of the budget only; dollars stay on the server, as the Improve tab's quota does.
    • The same period as enforceImproveQuota (quotaPeriodStart).
    • 100% only once spent (a budget of 0 too, since enforcement stops there); just under reads 99.9.
    • Spend past the budget (calls already running) reads as 100%, with the features scaled to fit.
  • GET /api/billing/ai-usage:
    • Any member of the workspace can read it, as the Billing tab and the Improve tab's quota already show to all.
    • With no active plan it returns 402 plan_limit, and the card stays hidden.
    • Self-hosted / no org: unlimited, no meter.

Web

  • An AiUsageCard sits on Billing under the current plan (components/settings/ai-usage-card.tsx), with:
    • "42% used", the bar (green / amber from 80% / red at 100%), and "Resets on {date}" in the same format and time zone as "Renews on";
    • each feature's share, with "<1%" for a sliver, rounded down so nothing reads full early;
    • a line saying Agent Chat runs on the workspace's own AI key, so it doesn't use this allowance.
  • When it's used up, the card says so with the date. On non-Enterprise plans it also says "A bigger plan below comes with more."
  • The plan cards' "Nx AI usage" hint now names the Action Center too.

Deploy order

Apply 00242 before (or with) the server deploy. The writers insert feature; on a database without the column, the usage insert fails, and it's only logged, so calls would go uncharged meanwhile.

Numbering: 00240 is the open content stack's (#1371…) and 00241 is #1385's (Perception, urgent). This is 00242.

Related issue

Sprint item "Billing AI usage meter" (the pre-reservation fix, so concurrent calls can't overrun the budget, comes next as its own PR on top of this). No issue.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Run cloud mode (NEXT_PUBLIC_IS_CLOUD=true, server IS_CLOUD=true) with an org on Starter, active, subscription_ends_at set.
  2. Insert a few prompt_improve_usage rows with a feature.
  3. Open Settings › Billing:
    • "This period's AI usage" shows the share used, the reset date (the same day as "Renews on") and each feature.
    • Push the spend past $1: the bar turns red and says it's used up, with the date.
  4. An org with an inactive subscription doesn't show the card.

I checked it in the browser that way on the dev DB:

  • 42% used, then used up;
  • light and dark theme, at 1024 px and phone width.

The temporary plan and usage rows were restored and deleted.

Tests:

  • server: plan-guard-ai-usage.test.js (shares, scaling past the budget, just under, budget 0, no active plan, unlimited), billing.test.js (route, self-hosted, 402, failure), and the writers' tests (feature on every insert);
  • web: ai-usage-card.test.tsx (states, used up, Enterprise, rounding), plan-switcher.test.tsx.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • web yarn test (2,821 tests) and yarn build;
  • server npm run format, npm run lint and npm test (2,058 tests);
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

The workspace's one AI budget (Improve, the Content Editor's AI and the
Action Center's task writer) had no meter: the first sign of it was an
"AI usage is used up" error. Migration 00242 records what each AI call
was for (prompt_improve_usage.feature, backfilled) and sums the period's
spend by feature (ai_usage_by_feature). GET /api/billing/ai-usage
returns the shares of the plan's budget, and the Billing page shows the
share used, when it resets and what used it, with Agent Chat noted as
running on the workspace's own key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 22, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Sprint item: the Billing page's AI usage meter (migration 00242). Please apply 00242 before or with the server deploy — the three AI-usage writers now insert feature. The pre-reservation fix (so parallel AI calls can't overrun the budget) follows as its own PR on top.

gkhngyk
gkhngyk previously approved these changes Oct 23, 2026

@gkhngyk gkhngyk 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.

Looks good. Two small things, fine as a follow-up:

  1. "Resets on" can be early for renewals on the 29th–31st. server/src/lib/plan-guard.js:380 rebuilds the period's end as shiftMonthClamped(periodStart, 1), but the start anchoredPeriodStart returns (server/src/lib/quota-period.js:24) may already be clamped. With a renewal on March 30, the window starts Feb 28 and really ends March 30 (the quota doesn't reset before then), yet the card says "Resets on March 28". A renewal on the 31st reads a day early after every 30-day month. getContentUsage (plan-guard.js:555) has the same line, so the Content page's counter is off the same way.
    Fix: have the window math return its end as well, e.g. anchoredPeriod(endsAt, now) → { start, end } with anchoredPeriodStart built on it, and the calendar-month fallback ending on the 1st of the next month. Then use that end in both places.

  2. The server's rounding undoes the card's "<1%" and its rounding down. tenths() (plan-guard.js:360, used at 393 and 401) rounds to the nearest tenth before percentLabel floors it:

    • A first call on a large Enterprise budget ($0.02 of $50 = 0.04%) comes back as 0, so it reads "0% used" and "0%".
    • A feature at 99.96% comes back as 100 and reads "100%", while the total reads "99%".

    Fix: floor to a tenth on the server and keep a non-zero share at 0.1 or more. Or send the unrounded share and let percentLabel do all the formatting.

Deploy note: the card reads the new server route. Deploy the server before the web deploy, or with it; until then the card says "Couldn't read this period's AI usage". Rows written between applying 00242 and the server deploy get no feature and show as "Other". Re-running the migration's backfill update after the deploy labels them.

@gkhngyk gkhngyk 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.

Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).

@gkhngyk
gkhngyk merged commit c456881 into ansvisor:main Oct 23, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants