Skip to content

feat(billing): show the month's AI usage at the top right of the main pages (AI usage display, part 1) - #1430

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

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:feature/ai-usage-header-meter

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AI usage display, part 1: the month's AI usage at the top right of the main pages (Drive › AI Usage › AI-USAGE-spec.md §3.1 and §4).

Web only, no migration. It reads the server routes that are already on main: GET /api/billing/ai-usage (#1386), /api/audits/quota and /api/prompts/usage.

The meter

  • "AI 21%" with a small bar sits at the far right of each page's header row, on the title's line.
  • Colour: black under 80%, amber from 80%, red at 100%, with dark: variants. Its name says it too: "AI 84% used this month, close to the limit".
  • On a phone it sits under the header row, on the right, so the row's buttons keep the whole width and the tab order follows what you see.
  • Nothing shows on a plan without an allowance (self-hosted) or a workspace without an active plan.
  • While the first read runs, an empty box of the meter's size keeps the header from shifting (from 640 px up; on a phone, nothing).

The popover (on click)

  • The month's percent, the plan, and "Shared by every seat · resets Nov 1". When the allowance is used up, a red line says when it resets and that Agent Chat on the workspace's own key keeps working.
  • The split by feature, with the Billing card's labels (settings.aiUsageMeter.features).
  • "Agent Chat, on your own AI key · Not counted".
  • Other limits this month: Site Audit pages (used plus running, of the plan's) and prompt locations (the account's, of the plan's).
    • They are read when the popover opens. A later opening keeps what it shows until the new read lands, and only the latest read is kept.
    • One with no cap is left out.
  • Links (following one closes the popover):
    • Get more AI usage goes to Billing. It becomes the primary button from 80%, and Enterprise doesn't show it.
    • Use your own key goes to AI Providers.
    • Both are an admin's to act on, so other members see "To get more AI usage or use your own key, ask an admin of your workspace." instead.
    • See details goes to Billing's AI usage card (id="ai-usage"). The card scrolls itself into view once it has loaded, since Billing shows it only after reading the plan.

Reads

  • One read serves every page: AiUsageProvider sits in the dashboard layout.
  • It reads again on a page change, or on a return to the tab, once the last read is a minute old, and each time the popover opens.
  • It reads from the browser through the /api/aeo proxy, not a server action, because Next runs server actions one at a time and the meter would otherwise wait behind a page's own work, such as the Content Agent's. Each read gives up after 15 s.
  • Only Ansvisor Cloud reads anything. A failed read keeps the last value.
  • AiUsageProvider is in components/providers/, beside the plan and role providers.

Pages: each header row is wrapped in HeaderWithAiUsage, so the meter is in the same place on every page: 24 px from the top and from the right at 1280, 1024 and 375 px.

  • Covered:
    • Action Center (Actions, Signals, KPIs, History)
    • Insights, including its empty state
    • Prompts and its tabs
    • Mentions
    • Content (Opportunities, Plan, Library)
    • Site Audit, AI Traffic, Citations
    • Competitors, including its loading and empty states
    • Reports and a report
    • Shopping, Brands, Settings
  • Diff: most of it is re-indentation; review with "Hide whitespace".

Next PRs (same spec):

  • The Content Editor's meter (after "N running", with "This page so far") and the 80% / 100% banners. The editor's meter is stacked on feat(content): fit the editor header to its width, its icon buttons labelled on hover and focus (Next step basket, part 2) #1429's header.
  • The meter on the pages not covered here: a prompt, an answer, an audit, a cited URL, a topic page, Similar topics, an opportunity, a brand's settings / prompts / topics, and New brand. Most sit in a centred max-w-5xl column with a Back row, so they need their own placement.
  • Billing's AI usage card could read through the same provider instead of its server action (it reads the same route).
  • Per-run usage in the ledger (a run id, with briefs apart from the Content Agent), estimates before a run, "This run used x%", History, and Billing by page.

Chat has no header row yet. The meter goes there with the Chat work, and it is the only meter there (spec §3.1).

From the spec, changed here:

  • "Get more AI credits" reads Get more AI usage, because there are no credit packs yet (CHAT-18). It leads to the plans.
  • Storage is left out of the other limits, because no storage limit exists.

Pre-push review: no high findings. Fixed:

  • "See details" from another page opened Billing at the top;
  • the popover stayed open after a link was followed;
  • an earlier limits read could land over a later one, and the loading row blinked on every opening;
  • no timeout on the reads;
  • members who aren't admins were sent to plan and key settings they can't change;
  • the popover had no name, the meter's name didn't hold its visible text or state, and on a phone the visual order didn't match the tab order;
  • the date and number helpers from @/lib/format instead of new ones;
  • the provider moved to components/providers/.

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. Set NEXT_PUBLIC_IS_CLOUD=true for the web and IS_CLOUD=true for the server, and give the workspace a cloud plan (e.g. Growth) with an active subscription.
  2. Open Insights, Prompts, Mentions, Content, Site Audit and Action Center. "AI n%" sits at the top right of each, in the same place.
  3. Click it. The popover shows the percent, the reset date, what used it, "Not counted" for Agent Chat on your own key, Site Audit pages and prompt locations, and the three links.
  4. Put the workspace at 84%, then 100%, of its allowance (rows in prompt_improve_usage):
    • at 84% the meter turns amber and Get more becomes the primary button;
    • at 100% it turns red and the popover says when it resets.
  5. Self-hosted: no meter, and no read.
  6. Check light and dark, and 1024 px.

Checked locally on the demo workspace, temporarily on Growth with test usage rows (restored and removed afterwards):

  • At 1280 and 1024 px the meter is 24 px from the top and the right on all 23 header rows, with no horizontal overflow from the header.
  • At 375 px it sits under the header row, on the right.
  • "See details" from Insights opens Billing scrolled to the card; following a link closes the popover, also when it leads to the same page.
  • Contrast in the popover and on the meter is 4.7–17.2 in light and dark, at 21%, 84% and 100%.

Checklist

  • web/: yarn format:check, yarn lint (0 errors; 6 existing warnings elsewhere), yarn typecheck, yarn test (364 files, 3,300 tests) and yarn build pass
  • Tests:
    • ai-usage-client.test.ts: the reads go through the proxy. A missing plan reads as null and an error throws. Site Audit pages count the running ones, and prompt locations are the account's. A limit with no cap, one that can't be read, and prompts without a brand are left out.
    • ai-usage-meter.test.tsx:
      • the share, its bar and its name, in each state;
      • the box while reading (none on a phone);
      • nothing when unmetered or self-hosted (no read);
      • amber and red;
      • the popover's name, rows and links;
      • closing when a link is followed;
      • Get more as primary from 80%, and its absence on Enterprise;
      • "ask an admin" for other members;
      • other limits left out, kept while read again, and only the latest read landing;
      • the header wrapper's order;
      • one read for every page, read again when stale on a page change, a tab return (focus or visibility), keeping the last read on a failure.
    • ai-usage-card.test.tsx: the card scrolls itself into view for #ai-usage.
    • action-center/{actions,kpis}/page.test.tsx stub the wrapper, as they stub the tabs.
  • UI text in messages/en.json

🤖 Generated with Claude Code

… pages (AI usage display, part 1)

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. One small follow-up, fine to do later:

  1. web/src/components/providers/ai-usage-provider.tsx:53-56: a failed read doesn't move readAt. While the API is unreachable, every page change and every tab focus starts another read, and each one can wait the full 15 s. reading keeps it to one read at a time, so this isn't a storm, but a short back-off in the catch would keep an outage quiet. For example, set readAt.current = Date.now() - AI_USAGE_STALE_MS + 15_000 there, so the next try waits about 15 s instead of firing on the next navigation.

@gkhngyk
gkhngyk merged commit cb0eff9 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#1430 and ansvisor#1431 are on main (squash-merged), with ansvisor#1433 and ansvisor#1436; this
branch's own change (part 4) is applied on top of them.

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