Usage tracking
Metering, quotas, and usage-based billing.
Usage tracking meters what orgs consume, enforces per-metric quotas, and feeds usage-based billing. Events are recorded append-only, rolled up daily by a cron into a read model, and surfaced on a dashboard (charts via Recharts). Metered metrics are pushed to Stripe Billing Meters through billing's seam.
Optional feature
Usage is optional (core: false, requires: ["billing", "flags", "jobs"]).
It is a leaf — no feature depends on it — so it prunes cleanly on its own.
What it does
- Ingestion —
recordUsage(...)is the append-only entry point, callable from anywhere server-side; an optional idempotency key dedups replays. - Consumption & quotas —
usageConsumption/getConsumedAmountprovide theQuotaConsumptionthat flags'checkQuotareads;enforceQuotais the per-metric block/warn gate. Metrics are declared inconfig/usage.ts. - Rollup & retention — a daily
usage/rollupcron aggregates events intoUsageRollup; a retention cron prunes raw events pastRETENTION_DAYS(rollups are kept forever as history). - Warn emails — breaching a
warn-mode quota emitsusage/quota.warned;sendQuotaWarnedEmaildelivers the notice through the injected email surface. - Dashboard —
UsageDashboardrenders per-metric current-period usage. - Stripe Meters — the rollup reports metered totals via billing's
reportOrgMeterUsage.
Models it adds
| Model | Purpose |
|---|---|
UsageEvent | Append-only usage stream (metric, quantity, optional idempotency key). |
UsageRollup | One row per (org, metric, day) — the aggregated read model, kept forever. |
Adds the marker-wrapped Organization.usageEvents / usageRollups relations,
and vendors components/ui/chart.tsx (the Recharts wrapper) for the dashboard.
Env it needs
None. Adds the recharts dependency.
Composition
requires: ["billing", "flags", "jobs"]— quota limits resolve through flags'checkQuota; metered totals push through billing'sreportOrgMeterUsage; the rollup/retention crons and theusage/quota.warnedevent run on jobs.- email is a soft consumer seam: the quota-warned email is delivered
through an injected email surface (usage doesn't
requireemail). - admin reads a per-metric summary through the same source as the dashboard.
What pruning removes
Leaving usage out drops features/usage, config/usage.ts, the usage settings
page, the UsageEvent and UsageRollup models, the Organization usage
relations, the rollup/retention jobs, and components/ui/chart.tsx (plus the
recharts dependency). No feature requires usage, so removing it affects only
usage itself.