Billing and Usage Metering
Canonical sources
Do not duplicate
- Rate version values and units
- Full API endpoint list
- Prisma table definitions
- Cost estimation algorithm
Group-scoped metering of billable activities (OCR, classification, enrichment, training, blob storage), per-month cost rollups, and optional monthly spending caps enforced before work starts. Rates come from a versioned rate file, not per-workload logic. Full behavior, configuration, and API live in the canonical doc docs-md/architecture/USAGE_METERING_AND_BILLING.md.
Source Map
- Canonical doc:
docs-md/architecture/USAGE_METERING_AND_BILLING.md. - Shared write path and arg builder:
packages/billing/. - Backend billing (seeder, cost estimation, cap check, usage queries, config):
apps/backend-services/src/billing/; authoritative rates in that folder'srate_versions.json. - Temporal billing (activity interceptor, nightly storage charge, month-end archival):
apps/temporal/src/billing/. - Blob inventory feeding storage charges:
storage-ledger-db.service.ts(backend) andblob-storage/storage-ledger.ts(temporal). - Schema (six
Decimalmoney tables):apps/shared/prisma/schema.prisma. - UI:
apps/frontend/src/pages/BillingPage.tsx.
Design Notes
- One shared write path: insert a
usage_eventsrow and atomically{ increment }theusage_period_summariesrollup in a single UTC-keyed transaction. - Rate versions are immutable; a price change is a new version, seeded on backend boot.
- The spending cap is a soft pre-flight check, not a budget reservation — see the canonical doc's Known Limitations.
Related Topics
- Blob storage: key scheme and provider abstraction the ledger uses.
- Auth and groups: group scoping and role checks for cap endpoints.
- Graph workflows: the activities the interceptor meters.
- System overview: backend vs Temporal vs frontend boundaries.
Common Drift Risks
rate_versions.jsonvalues drift from documented examples; treat the JSON as authoritative and never restate specific units in the wiki.- Two activity registries exist and can drift: the backend's
REGISTERED_ACTIVITY_TYPES(apps/backend-services/src/workflow/activity-registry.ts) and the Temporal worker's (apps/temporal/src/activity-registry.ts). Rate coverage is enforced against the backend list only, so an activity present solely in the worker's list is unpriced — and an unpriced activity is silently free.