Skip to content

feat(admin): cross-user usage + spending API (#32) - #43

Closed
serge-ivo wants to merge 1 commit into
feat/admin-foundationfrom
feat/admin-usage-spending
Closed

feat(admin): cross-user usage + spending API (#32)#43
serge-ivo wants to merge 1 commit into
feat/admin-foundationfrom
feat/admin-usage-spending

Conversation

@serge-ivo

Copy link
Copy Markdown
Contributor

Addresses #32 (visibility half). Stacked on #28 / PR #42 — review/merge that first (base is feat/admin-foundation).

What

The operator "what are we spending on AI" view, over the real ai_usage ledger.

  • GET /v1/admin/usage?range=7d|30d|90d|all — cross-user rollup by provider, model, kind, agent, user, and day, with a platform-paid vs BYOK split.
  • GET /v1/admin/spending — totals, BYOK spend (real, token-estimated), top spenders, top models, trend, platformAiEnabled, and an honest platformPaid.metered=false caveat.
  • aggregateAdminUsage() in lib/usage.ts — pure cross-user aggregation reusing the existing bucket helpers; adds byProvider + byUser + the split.

Tests

14 tests (pure aggregation + route gate/shape). Typecheck clean; per-user usage aggregator unchanged (no regression).

Honest scope / follow-ups

BYOK spend (the real Anthropic $) is fully accurate. Platform-paid AI (embeddings/summaries/translation on env.AI) is not yet metered into the ledger — the split counts only provider="platform" rows, so it shows real data, not a fake $0. Deferred as clearly-scoped follow-ups (will file):

  1. Write-path metering — tag platform env.AI calls with provider="platform" + a CF-neuron cost estimate (needs userId plumbing into the DO storage engine for embeddings/summaries).
  2. Cloudflare billing-actuals integration — the authoritative platform Workers-AI spend (needs CF API token + Analytics GraphQL scope bound to the API worker).
  3. Runtime AI kill switch (platform_settings override of PLATFORM_AI_ENABLED) — today the env-var flag + redeploy is the lever.

Adds the operator money view over the ai_usage ledger, stacked on the admin
foundation (#28):

- GET /v1/admin/usage?range=7d|30d|90d|all — cross-user rollup by provider,
  model, kind, agent, user, and day, with a platform-paid vs BYOK split.
- GET /v1/admin/spending — totals, BYOK spend (real, token-estimated), top
  spenders, top models, trend, plus platformAiEnabled and an honest
  platformPaid.metered=false caveat.
- aggregateAdminUsage() in lib/usage.ts — pure, cross-user aggregation reusing
  the existing bucket helpers; adds byProvider + byUser + the split.
- 14 tests (pure aggregation + route gate/shape); typecheck clean; no
  regression in the per-user usage aggregator.

Scope note: platform-paid AI (embeddings/summaries/translation on env.AI) is
not yet metered into the ledger — only provider="platform" rows are counted, so
the split is honest rather than a fake $0. Write-path metering + Cloudflare
billing-actuals + the runtime kill switch are tracked as #32 follow-ups.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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