Skip to content

feat(billing): read a Content Editor page's share of the month's AI usage (AI usage display, part 3a) - #1432

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:feature/ai-usage-page-server
Oct 24, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:feature/ai-usage-page-server

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AI usage display, part 3a (server): a Content Editor page's share of the month's AI usage, for the editor meter's "This page this month" line (Drive › AI Usage › AI-USAGE-spec.md §3.1). Server only, additive, no migration.

  • getDraftAiUsage(orgId, draftId) in lib/plan-guard.js, next to getAiUsage:
    • It sums the page's rows in prompt_improve_usage (its brief, Content Agent and writer charge content_draft_id; holds included, as the month's figure counts them).
    • It reads them over the same period as getAiUsage, so the page is a part of the month's figure.
    • The result is a share of the plan's budget, in percent to a tenth.
    • Spend past the budget is scaled to fit, as getAiUsage scales its features, so a page never reads more than the month.
    • A page that used any AI reads at least 0.1 (shown as "<1%"), never 0.
    • Only the organization's own rows count, so another workspace's page id reads 0.
    • Unlimited plans read { unlimited: true, percent: null }.
  • GET /api/billing/ai-usage/draft/:draftId (routes/billing.js):
    • z.guid() check, 400 otherwise, as routes/content-drafts.js;
    • no organization row (self-hosted) reads unlimited, as /ai-usage;
    • without an active plan, the PlanLimitError status with error: 'plan_limit';
    • otherwise 500.
    • Dollars stay on the server.

Read cost:

  • The page's rows are filtered from the organization's rows this period, which the (organization_id, used_at) index from 00120 serves. They are a few hundred at most, since the budget caps them.
  • The month's total comes from the existing ai_usage_by_feature RPC.
  • Both reads run in parallel.

Deploy order: this one first; the web part (the editor meter, stacked on #1429 and #1431) reads it.

Pre-push review: no high findings. Fixed:

  • a page could read over 100% when the month went past its budget;
  • a small spend on a big budget read 0%;
  • one test name now says what it checks.

Noted outside this PR:

  • content_draft_id (00212) has no index, so deleting a draft scans the ledger for ON DELETE SET NULL;
  • improveSpendThisPeriod asks for 5000 rows, but PostgREST's max_rows caps that at 1000.

Related issue

Drive spec AI Usage (approved 2026-10-23). No GitHub issue.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation

How to test

  1. With IS_CLOUD=true and a workspace on a cloud plan, call GET /api/billing/ai-usage/draft/<draftId> as a member. It reads { unlimited: false, percent }: the page's share of the month's budget.
  2. Run Fix open rules on the page, then call it again: the percent grows.
  3. Another workspace's draft id reads percent: 0; a bad id answers 400; self-hosted reads { unlimited: true, percent: null }.

Checklist

  • server/: npm run format, npm run lint, npm test (190 files, 2,314 tests) pass
  • Tests:
    • plan-guard-ai-usage.test.js:
      • the page's rows filtered by organization, page and period;
      • 0 with no calls, 0.1 for a sliver;
      • scaled to fit past the budget;
      • plan_limit without an active plan;
      • unlimited without a budget.
    • billing.test.js: the route reads for the member's organization, turns away a bad id, has no share without an organization, and maps plan_limit and failures.
  • Root npm run version:check fails on main already (compose images 0.2.0 vs 0.3.0); not touched here

🤖 Generated with Claude Code

…sage (AI usage display, part 3a)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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. The route reads only the member's own organization's rows, so another workspace's draft id reads 0 and shows nothing about it. Only percents leave the server.

One low item, fine as a follow-up:

  1. server/src/lib/plan-guard.js:570: the page's share isn't capped at the month's figure, so rounding can put the page above the month. Example: $4.998 of a $5 budget spent, all on one page. The month reads 99.9, because getAiUsage holds an unspent budget at 99.9, but the page reads tenths(99.96) = 100. The popover then shows "This page this month 100%" under a "99%" meter. Fix: cap the page the same way, e.g. Math.min(percent, spentAll >= budget ? 100 : 99.9).

@gkhngyk
gkhngyk merged commit c910606 into ansvisor:main Oct 24, 2026
6 checks passed
cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 24, 2026
ansvisor#1432 is on main (squash-merged), with ansvisor#1429–ansvisor#1436 around it; this
branch's own changes (part 5 and its follow-up) are applied on top.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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