Skip to main content

Module usage_meter

Module usage_meter 

Source
Expand description

Measured per-model spend meter.

record aggregates the real provider usage extracted by super::usage into per-model token sums, prices them with the shared ModelPricing table, and persists the totals to proxy_usage.json so the dashboard, CLI and the savings ledger (which run in other processes) can read the user’s real provider bill.

Unlike lean_ctx::core::proxy_metrics (which resets per proxy lifetime), this meter is a lifetime-cumulative spend counter: resume_from_disk seeds the in-memory totals on proxy startup so a restart never zeroes the user’s measured spend.

Structs§

CohortUsage
Cumulative holdout cohort totals. Keyed by arm name: "control" | "treatment" for output savings (#895 Track B), "compression:control" | "compression:treatment" for input compression (#1905). The average tokens per turn are *_tokens / requests. Only populated while a holdout is active.
ModelSpend
One model’s measured, priced spend for /status and the dashboard.
VerifiedSavings
Cross-model verified-savings totals (#701) for /status and the dashboard.

Functions§

cohort_snapshot
Live output-savings cohort totals (#895). Empty until a holdout runs.
compression_cohort_key
Cohort-store key for an input-compression holdout arm (#1905). Prefixed so it never collides with the output-savings arms stored as control / treatment, which keeps existing files and readers unchanged.
load_persisted
Cross-process read of the persisted measured spend (dashboard / CLI / ledger).
persisted_cohorts
Cross-process read of the persisted output-savings cohort totals.
persisted_projects
Priced spend per project key (the device view’s split).
persisted_session
Priced spend of one app session, read from disk; None when the proxy saw no turn of it (or keeps it no longer).
persisted_snapshot
Cross-process priced spend rows, read from disk.
persisted_snapshot_where
Priced spend rows of the projects whose key keep accepts, summed per model.
price_models
Prices a model usage map into sorted ModelSpend rows. Pure: shared by the in-memory snapshot and the cross-process persisted_snapshot.
record
Records one turn’s measured usage against its model bucket (and its output-savings cohort, when tagged) and persists.
resume_from_disk
Seeds the in-memory totals from proxy_usage.json. Call once on proxy startup so measured spend is cumulative across restarts. Idempotent-ish: it merges the persisted totals into whatever is in memory (normally empty).
snapshot
Live per-model measured spend, priced and sorted by USD descending.
total_cost_usd
Total measured spend across all models (live in-memory totals).
verified_savings
Aggregated provider-verified savings across all models (#701), or None until at least one probe-covered request has been recorded. Unlike the tokens_saved estimate (bytes/4), both sides of this pair were counted by the provider on the same request — receipts, not estimates.