feat(admin): meter platform-paid AI into the ledger (#44) - #47
Closed
serge-ivo wants to merge 1 commit into
Closed
Conversation
Makes the platform's own AI spend visible + attributable (closes the loop on "the platform silently pays for AI"). Platform env.AI calls now write ai_usage rows tagged provider="platform", so /v1/admin/spending's split is real. - lib/ai-pricing.ts: estimatePlatformCostMicros() + approxTokens() — a rough, clearly-labeled Workers-AI cost estimate (authoritative neuron spend = CF billing actuals, #45). - lib/usage.ts: recordPlatformUsage() (provider="platform"); UsageKind gains "embedding" | "summary". - agent-storage.ts: optional EngineMeter (6th ctor arg, default null → zero churn to the 30 existing call sites). embed() + generateSummary() ledger when a meter with a userId is present. - agent-do.ts: getStorageEngine(agentId, userId?) builds a meter from env.DB + the acting user; wired at the chat handler (recurring RAG-embed + summary spend). Other call sites unmetered (unchanged). - instances-translation.ts: platform translate path ledgers kind="translate". - /v1/admin/spending: platformPaid.metered=true, estimated=true. Coverage: chat-time embeds/summaries + translation. Background ingest/repo embeds not yet metered (documented). Tests: pricing + recordPlatformUsage (6) + updated spending assertions; full API suite green (655). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
17 tasks
serge-ivo
force-pushed
the
feat/admin-platform-metering
branch
from
July 31, 2026 23:42
f0018b7 to
59bd9c7
Compare
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.
Closes #44. Stacked on #32 / PR #43 (base
feat/admin-usage-spending) which is stacked on #28 / PR #42. Merge order: #42 → #43 → this.What
Makes the platform's own AI spend visible and attributable — the loop-closer on "the platform silently pays for AI." Platform
env.AIcalls now writeai_usagerows taggedprovider="platform", so the split in/v1/admin/spendingshows real platform-paid data instead of the previous honest-$0.estimatePlatformCostMicros()+approxTokens()— rough, clearly-labeled Workers-AI cost estimate (authoritative neuron spend = CF actuals, [Admin][API] Cloudflare billing-actuals for authoritative platform AI spend #45).recordPlatformUsage()(provider="platform");UsageKind+=embedding|summary.AgentStorageEnginegains an optionalEngineMeter(6th ctor arg, defaultnull→ zero churn to the 30 existing call sites).embed()+generateSummary()ledger when a meter+userId is present.getStorageEngine(agentId, userId?)builds the meter; wired at the chat handler (recurring RAG-embed + summary spend). Translation platform path ledgerskind="translate"./v1/admin/spending:platformPaid.metered=true, estimated=true.Coverage (honest)
Metered: chat-time embeddings + summaries + translation. Not yet: background ingest/repo embeds (documented — a follow-up can pass a meter on those paths). Cost is a token-based estimate; #45 wires CF billing actuals for the authoritative number.
Tests
Pricing +
recordPlatformUsage(6 new) + updated spending assertions. Full API suite green: 655 tests. Typecheck clean.