Skip to content

feat(admin): meter platform-paid AI into the ledger (#44) - #47

Closed
serge-ivo wants to merge 1 commit into
feat/admin-usage-spendingfrom
feat/admin-platform-metering
Closed

feat(admin): meter platform-paid AI into the ledger (#44)#47
serge-ivo wants to merge 1 commit into
feat/admin-usage-spendingfrom
feat/admin-platform-metering

Conversation

@serge-ivo

Copy link
Copy Markdown
Contributor

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.AI calls now write ai_usage rows tagged provider="platform", so the split in /v1/admin/spending shows 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.
  • AgentStorageEngine gains an optional EngineMeter (6th ctor arg, default nullzero 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 ledgers kind="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.

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>
@serge-ivo serge-ivo mentioned this pull request Jul 31, 2026
17 tasks
@serge-ivo
serge-ivo force-pushed the feat/admin-platform-metering branch from f0018b7 to 59bd9c7 Compare July 31, 2026 23:42
@serge-ivo
serge-ivo deleted the branch feat/admin-usage-spending August 1, 2026 00:25
@serge-ivo serge-ivo closed this Aug 1, 2026
@serge-ivo
serge-ivo deleted the feat/admin-platform-metering branch August 1, 2026 00:25
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