Expand description
usage_events Postgres store (enterprise#17, baseline fields enterprise#18).
One row per measured LLM turn: who (person/team/project, enterprise#11),
what (provider/model/tokens), what it cost (priced with the shared
ModelPricing table) and the counterfactual-baseline inputs that make the
success fee provable (uncompressed_input_tokens, reference_model,
reference_cost_usd, is_local — Doc 08 §2).
Schema management follows the repo rule: init_schema is idempotent
batch_execute DDL (CREATE TABLE IF NOT EXISTS …), no migration files.
The writer consumes the proxy::usage_sink stream: bounded channel, spawned
task, INSERT per event. Fail-open (enterprise#12): insert errors are logged
and counted, never propagated to the request path.
Structs§
- Usage
Event - One
usage_eventsrow, fully derived from a finalizedRealUsage.
Constants§
- POOL_
MAX_ SIZE_ ENV - Env var overriding the store pool’s
max_size(chart:database.poolMaxSize). - WRITER_
QUEUE - Buffered events between the proxy choke-point and the Postgres writer.
Sized for bursts (a full channel drops events, counted in
usage_sink).
Functions§
- budget_
window_ sums - Current-window spend sums for the budget gate (enterprise#25):
per-person spend for the running UTC day and per-project spend for the
running UTC month, straight from
usage_events. - delete_
person_ events - Deletes all events of
person_keys(GDPR Art. 17). Returns rows removed. - evidence_
rows - Daily evidence aggregates for the export window (enterprise#36): bounded output regardless of event volume, yet fine-grained enough for an EU-AI-Act usage-evidence audit (per day × person × project × model).
- init_
schema - Applies the usage-store DDL. Safe to run on every start (idempotent).
- insert_
event - Inserts one event. Errors bubble to the writer loop, which logs and moves on.
- person_
events - All events attributed to one of
person_keys(raw + pseudonym, GDPR Art. 15 export), as self-describing JSON rows. - pool_
from_ database_ url - Builds the store pool from a
DATABASE_URL, honoringsslmode(#54/#58). - purge_
events_ older_ than - Deletes
usage_eventsrows older thandays(enterprise#36). Returns the number of purged rows.days == 0is rejected by the caller (retention disabled), never here — this function always deletes what it is told. - spawn_
writer - Wires the usage stream into Postgres: installs the process-wide sink
(
proxy::usage_sink) and spawns the writer task. Call once at gateway startup, afterinit_schema.