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
System Design
Software Engineer
OpenAI
November 29, 2026
Software Engineer
System Design Round
System Design
Medium

0

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
event streaming
exactly-once
idempotency
aggregation
financial correctness
How to Approach This
  1. Start by clarifying functional and non-functional requirements with the interviewer.
  2. Estimate the scale: QPS, storage, bandwidth. This drives your design decisions.
  3. Draw a high-level architecture first, then deep dive into 1-2 critical components.
  4. Discuss trade-offs explicitly (e.g., consistency vs availability, SQL vs NoSQL).
  5. 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 Problems
Sample 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
  1. Serving layer writes a usage event to a local buffer, flushed async to Kafka with key org_id. Each event has a unique request_id.
  2. Stream aggregator (Flink) dedupes on request_id wi...

Submit Your Answer
Markdown supported