Repository navigation
feat(billing): show this period's AI usage on the Billing page - #1386
Conversation
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]>
|
@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 |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. Two small things, fine as a follow-up:
-
"Resets on" can be early for renewals on the 29th–31st.
server/src/lib/plan-guard.js:380rebuilds the period's end asshiftMonthClamped(periodStart, 1), but the startanchoredPeriodStartreturns (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 }withanchoredPeriodStartbuilt on it, and the calendar-month fallback ending on the 1st of the next month. Then use thatendin both places. -
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 beforepercentLabelfloors 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
percentLabeldo 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
left a comment
There was a problem hiding this comment.
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).
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.prompt_id/content_draft_id/action_id).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
feature:routes/prompts.js(Improve);lib/content-brief.jsrecordUsage(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 }] }:enforceImproveQuota(quotaPeriodStart).GET /api/billing/ai-usage:plan_limit, and the card stays hidden.Web
AiUsageCardsits on Billing under the current plan (components/settings/ai-usage-card.tsx), with: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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
NEXT_PUBLIC_IS_CLOUD=true, serverIS_CLOUD=true) with an org on Starter, active,subscription_ends_atset.prompt_improve_usagerows with afeature.I checked it in the browser that way on the dev DB:
The temporary plan and usage rows were restored and deleted.
Tests:
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 (featureon every insert);ai-usage-card.test.tsx(states, used up, Enterprise, rounding),plan-switcher.test.tsx.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) (0 errors; the 6 warnings are in untouched files, as on main)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
yarn test(2,821 tests) andyarn build;npm run format,npm run lintandnpm test(2,058 tests);bash supabase/build-schema.sh.🤖 Generated with Claude Code