Usage metering
Recording every billable unit, such as model tokens, embedding calls, pages and compute seconds, as an event stamped with who caused it, so spend can be attributed, limited and billed.
In DocSlurpGlossary entry
On this page
Usage metering records each billable unit of work as an event: tokens sent to and received from a model, embedding calls, pages processed, compute seconds, storage. Each event says what happened, how much it cost and who caused it. Budgets, rate limits, invoices and cost dashboards are all queries over those events.
A typical event for one chat answer:
metric=chat-answer model=<model id> input_tokens=6200 output_tokens=410
org=meridian project=board-reporting api_key=key_7 cost_cents=<priced from the model's token rates>
Rules that keep it honest
- Use the provider’s reported counts. Model APIs return actual token usage with each response. Estimates from string length drift, especially across languages and tokenizers.
- Stamp the owner at write time. Record organization, project and API key on the event itself. If ownership is reconstructed later by joining through runs or projects, deleting a project makes its spend disappear or move.
- Make writes idempotent. A retried write must not bill twice. A deterministic event ID with “insert, do nothing on conflict” handles it.
- One reader. If billing, budgets and dashboards each write their own spend query, they will disagree. Route all of them through one attribution rule.
- Check before spending. Metering records what happened; a budget check before an expensive call prevents what shouldn’t.
In DocSlurp
AI calls on the API and worker go through a usage ledger that prices them from token counts, prints an [ai-usage] log line, and writes them to usage_events with organization, project and API key columns. Spend is read only through one attribution view, which also splits a cross-project answer evenly across the projects it searched. Model-calling paths check the caller’s budget first.
The gotcha
Not every cost is a model call. Worker compute, storage and base fees need their own events, or pages per $1 will look better than it is.
In DocSlurp
In DocSlurp
Every AI call is priced from actual token counts, logged as an [ai-usage] line and written to usage_events with org, project and API key columns (core/server/src/lib/ai-usage.ts). Budgets, rate limits and billing read spend only through one attribution view (core/server/src/services/usage-attribution.ts).