feat(admin): cross-user usage + spending API (#32) - #43
Closed
serge-ivo wants to merge 1 commit into
Closed
Conversation
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>
This was referenced Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_usageledger.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 honestplatformPaid.metered=falsecaveat.aggregateAdminUsage()inlib/usage.ts— pure cross-user aggregation reusing the existing bucket helpers; addsbyProvider+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 onlyprovider="platform"rows, so it shows real data, not a fake $0. Deferred as clearly-scoped follow-ups (will file):env.AIcalls withprovider="platform"+ a CF-neuron cost estimate (needs userId plumbing into the DO storage engine for embeddings/summaries).platform_settingsoverride ofPLATFORM_AI_ENABLED) — today the env-var flag + redeploy is the lever.