keepcalculator

06 Numbers and Powers

Meter AI Usage Across Models, Tools, and Customers

Card No. 06·24498 · Updated · Standard formula, shown below

Accurate AI metering shows how much each customer uses across models and tools, so you can allocate costs, investigate spikes, and produce reliable usage reports. First establish a common event format and capture usage at the point each request runs; then reconcile those records against provider reports and your own billing rules.

What You’ll Need

  • A request gateway, application middleware, or tool wrapper where usage events can be recorded.
  • Stable customer, workspace, and request identifiers, plus a way to map them to internal accounts.
  • Access to model and tool response metadata, including token counts or other provider-reported usage where available.
  • A durable event store, such as a database or analytics pipeline, and a defined retention and access policy.
  • Documentation of your internal pricing or allocation rules. Keep raw usage separate from prices, which can change.

Step-by-Step

  1. Define what counts as usage. Decide whether a billable unit is an API call, input and output tokens, cached tokens, image or audio units, tool execution time, or a combination. Specify how to treat retries, failed requests, streaming responses, background jobs, and requests that use multiple models. Write down the rules before implementing them so reports are consistent.
  2. Create a canonical event schema. Record one event per usage-producing operation, with fields such as event ID, timestamp, customer and workspace IDs, request or trace ID, provider, model, tool name, operation type, status, and usage quantities. Include input and output tokens as separate fields when relevant. Store provider-reported values and locally estimated values in distinct fields; do not disguise an estimate as a measured count.
  3. Attach customer identity at request entry. Resolve the authenticated account or workspace before calling a model or tool, then carry its stable ID through the whole request chain. Avoid relying on email addresses, mutable names, or a user-supplied customer ID. For shared accounts, record both the customer and, when appropriate, the initiating user or project.
  4. Instrument every model and tool path. Capture requests made through your main application, agents, background workers, retries, and direct tool integrations. For a multi-step workflow, record each model call and tool operation separately and link them with a shared trace or parent request ID. This lets you attribute total usage without losing the detail needed to diagnose it.
  5. Capture usage from the most authoritative source. Prefer the usage metadata returned with the completed provider response. For streaming, read the final usage event if the API supplies one; otherwise record that the count is unavailable and use a documented fallback. Tool usage should come from the tool wrapper or service that performs the work. Record units that differ by modality—such as audio duration or image count—rather than converting them into tokens without a defensible rule.
  6. Make event writes reliable and duplicate-safe. Assign each operation a unique event ID and use an idempotent write, so a retry of the logging operation cannot add usage twice. Persist events even when the customer-facing request fails after a provider has already consumed resources. Keep request handling resilient: logging failures should trigger an alert and a recoverable queue or local buffer, not silently erase usage.
  7. Separate usage, allocation, and cost. Preserve raw units as recorded. Apply pricing tables or internal charge rules in a separate calculation layer, tagged with the rule version and effective date. If several customers share a model call, define an allocation method—such as measured per-customer work or an explicitly labeled proportional split—and retain the original shared event for auditability.
  8. Build customer and operational reports. Aggregate by customer, period, model, tool, and unit. Show totals alongside coverage indicators, such as events with provider-reported counts, estimates, missing usage, or pending reconciliation. Include filters for failed, retried, and shared operations so a headline total can be explained rather than treated as an unexplained number.
  9. Reconcile and monitor. Regularly compare your event totals with provider usage exports or account-level dashboards for the same time window, model, and project. Differences can arise from time zones, delayed reporting, rounding, excluded calls, or differences in what each system counts. Track the gap, investigate meaningful changes, and alert on missing customer IDs, unknown models, duplicate IDs, and sudden usage spikes.
  10. Test changes before relying on reports. Send controlled requests for each supported model and tool, including streaming, retries, errors, and multi-step workflows. Confirm that identifiers and units are correct, events appear once, and customer totals reconcile with the raw event list. Re-run these checks when changing SDKs, provider APIs, model routes, or pricing rules.

Common Mistakes

  • Counting only successful responses: A provider may consume resources before a later application or tool failure. Record actual usage whenever it is available, independent of the final user-facing status.
  • Using estimates as exact counts: Text-length estimates and tokenizers may not match provider accounting. Label estimates and prefer response metadata or provider exports for reconciliation.
  • Mixing units: Tokens, seconds, images, tool calls, and monetary amounts are not interchangeable. Keep each unit explicit and aggregate only compatible quantities.
  • Double-counting retries or streams: A retry can be a separate provider call, while repeated stream chunks are not separate completed calls. Model each actual operation once and use idempotency keys for ingestion.
  • Attributing usage after the fact: If a customer ID is missing at request time, later guesses can misallocate usage. Validate identity before dispatch and quarantine unattributed events for review.
  • Changing historical totals with current prices: Repricing old usage can obscure what a prior report meant. Store raw quantities and versioned calculation rules so reports can be reproduced.

Tips

  • Keep an immutable raw event log and build corrected or summarized views from it. If an event needs correction, preserve the original and record the adjustment.
  • Use UTC timestamps and retain the provider request ID where available; these make cross-system comparisons easier.
  • Measure coverage as well as totals. A report that says “95% provider-reported, 4% estimated, 1% missing” is more useful than a falsely precise single number.
  • Restrict access to customer-level usage data, set a retention period, and avoid storing prompts or tool payloads unless they are genuinely needed for a documented purpose.
  • Keep model identifiers as returned by the provider and map them to normalized names separately. This protects historical reporting when provider naming changes.

Troubleshooting

  • Internal totals are lower than provider totals: Check background jobs, alternate API keys or projects, failed calls, and missing final usage events in streaming flows. Compare the same time zone and reporting window.
  • Internal totals are higher: Look for duplicate event ingestion, retries counted as one request in one system but several in another, or stream chunks incorrectly recorded as calls. Deduplicate using operation IDs, not approximate timestamps.
  • Usage has no customer attached: Trace the request ID from gateway to worker and tool wrapper. Add identity propagation at the earliest shared boundary; quarantine unmatched events rather than assigning them to a default customer.
  • Token counts are missing: Confirm the API response or final stream event includes usage under the current integration. If it does not, mark the count unavailable or estimated, then reconcile later with provider exports.
  • A report changes after it was generated: Check whether late events, revised allocations, or a new pricing rule were applied. Save report parameters, cutoff time, and rule version so you can reproduce the earlier view.

Frequently Asked Questions

Should I meter tokens or API calls?
Record both when available. Calls help explain workflow volume; tokens and modality-specific units are more informative for usage allocation.

How should I meter a request that uses several models?
Record each model operation as its own event, link them to one parent request, and aggregate the linked events for the workflow total.

Are provider usage counts always the billing total?
Not necessarily. Provider reports may use different timing, rounding, or unit rules. Treat them as a reconciliation source and consult the applicable provider documentation.

What should I do when usage metadata is missing?
Store the event with a missing or estimated status, retain its identifiers, and reconcile it against provider exports where possible. Do not silently substitute zero.

How often should I reconcile usage?
Choose a cadence that suits reporting needs and provider data availability. Reconcile frequently enough to catch broken instrumentation or unusual usage before the discrepancy grows.

Can I calculate customer charges directly from usage events?
Yes, but keep the pricing calculation separate and versioned. Preserve the raw quantities so a pricing change does not rewrite the underlying usage history.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →