Repository navigation
Usage ledger: daily per-workspace usage + cost, monthly CSV - #92
Merged
Merged
Conversation
leorfer23
force-pushed
the
usage-ledger
branch
from
October 14, 2026 18:27
61c7fb0 to
d277e3a
Compare
workspace_usage_daily records what each workspace cost per UTC day per metric, so an operator can see daily and monthly usage and invoice from it. - Nightly rollup (00:00 UTC, for the day that ended): serving requests, job runs + duration, AI tokens (BYO-key calls excluded) and non-LLM AI from the existing event tables; storage measured by listing R2, so drafts, thumbnails, inbox mail and connection caches count, not only assets/datasets/blobs. - Live counters: artifact email recipients (sendArtifactEmail) and browser seconds (screenshots, slide export, pilot verify). - Cost at Cloudflare list price (R2 $0.015/GB-mo, Browser $0.09/h, Email $0.35/1k) or real provider cost for AI; storage priced per day so a month sums to GB-months. - Admin: "Usage ledger" view (this month + last month), GET /v1/admin/usage.csv?month=YYYY-MM[&by=day], POST /v1/admin/usage/rollup to backfill a day. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leorfer23
force-pushed
the
usage-ledger
branch
from
October 14, 2026 19:00
d277e3a to
4b121d9
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.
What
workspace_usage_dailyrecords what each workspace cost per UTC day, one row per metric, so an operator can see daily and monthly usage and invoice from it. The existing "Cost by workspace" view only estimates the current window, and its storage is point-in-time and misses most of R2.How
analytics_events)job_runs)agent_usage_events, BYO-key calls excluded)ai_usage_events)sendArtifactEmail, which every customer send path uses) and browser seconds (screenshots, slide export, pilot verify).cf-pricing.ts. AI uses the real provider cost. Storage is priced per day, so a month sums to GB-months. Account-wide free allowances are not split across workspaces.GET /v1/admin/usage.csv?month=YYYY-MM[&by=day]POST /v1/admin/usage/rollup {day}to backfill. It only measures storage for yesterday or today.Schema
New migration
0010_workspace_usage_daily.sql, documented in SCHEMA.md §12.ON DELETE CASCADEon workspace: deleting a workspace drops its ledger, so export before deleting.Checks
npm run typecheck✓check:migrations✓tooling/scripts/ci-check-fast.sh✓ (incl. gitleaks + customer names)npm test: 5061 tests, all green after updating 3scheduling-emailassertions for the newrecipientsfieldtests/unit/usage-ledger.test.tsruns the real migrations in Miniflare D1/R2 (attribution, day bounds, BYO exclusion, idempotency, live counters).Deploy notes
Needs
wrangler d1 migrations apply --remotebefore the version goes live. After deploy, backfill the current month's event metrics withPOST /v1/admin/usage/rollupper day. Storage history starts the first night after deploy.🤖 Generated with Claude Code