Design Token Usage Metering and Monthly Invoicing for the API
Last updated: November 29, 2026
Quick Overview
Design the pipeline that records input, cached and output tokens for every API request across many models, aggregates them per organization and project, and produces accurate invoices and usage dashboards.
OpenAI
November 29, 20260
0
0 solved
Design the pipeline that records input, cached and output tokens for every API request across many models, aggregates them per organization and project, and produces accurate invoices and usage dashboards.
Every API call at OpenAI is billable, priced differently per model and token type, so metering has to be both high volume and financially exact. Interviewers look for exactly-once semantics, a clear line between real-time estimates and the billing source of truth, and handling of price changes.
What the Interviewer Expects
- Emit usage events from the serving path without adding latency
- Achieve effectively exactly-once counting with idempotent event IDs
- Separate raw events, aggregated usage, and rated (priced) usage
- Version price lists and apply the price effective at request time
- Reconcile aggregates against raw events before invoicing
Key Topics to Cover
How to Approach This
- Start by clarifying functional and non-functional requirements with the interviewer.
- Estimate the scale: QPS, storage, bandwidth. This drives your design decisions.
- Draw a high-level architecture first, then deep dive into 1-2 critical components.
- Discuss trade-offs explicitly (e.g., consistency vs availability, SQL vs NoSQL).
- Address failure scenarios, monitoring, and how the system handles 10x traffic spikes.
Possible Follow-up Questions
- How do you show near-real-time spend while invoices use the batch numbers?
- A pricing bug overcharged customers for two days; how do you correct it?
- How do you attribute usage to projects and API keys for chargeback?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Every request emits usage: org, project, key, model, input tokens, cached input tokens, output tokens, batch or not, timestamp.
- Usage dashboard updated within minutes; monthly invoice exact to the...
High-Level Design
- Serving layer writes a usage event to a local buffer, flushed async to Kafka with key
org_id. Each event has a uniquerequest_id. - Stream aggregator (Flink) dedupes on
request_idwi...