Design review: usage billing for an LLM API
I was asked this
No reports yet. Sign in to say where you were asked it.
A system design interview question asked in Anthropic's system design round. It tests design review, billing and metering.
You are reviewing a teammate's design doc before it goes to the wider team. Read it, then lead a critique: what is wrong, how bad each problem is, what you would change, and what the doc is missing. You have 45 minutes.
Usage billing for the API (draft)
Goal. Bill API customers per token, monthly, and cut off prepaid accounts when their balance runs out. Traffic today: 20,000 requests per second at peak across 50 gateway instances, about 1,500 tokens per request.
Counting. Each gateway keeps an in-memory map from API key to tokens used. Every 60 seconds it flushes the map to Postgres with UPDATE usage SET tokens = tokens + $n WHERE api_key = $k AND month = $m, then clears the map.
Streaming. For streamed responses the gateway counts tokens when the stream completes. If the client disconnects first, nothing is counted.
Retries. Clients retry on timeouts; each retry is a new request and is billed as one.
Months. The month for a request is computed from the gateway's local clock and time zone (gateways run in three regions).
Pricing. At invoice time, a job multiplies each customer's monthly tokens by the current price per token (a float in dollars) and rounds to the cent.
Invoices. The invoice job runs at 00:00 on the 1st, reads usage, and emails invoices.
Prepaid accounts. A daily job at 02:00 subtracts the day's usage from each prepaid balance and disables keys whose balance is below zero.
Monitoring. A dashboard shows total revenue per day.
What a strong answer covers
- Reading the doc the way money flows: from a token served to a line on an invoice to a key being cut off, and naming each place where a token is lost, counted twice or priced wrong.
- Each problem ranked by impact and likelihood, with numbers where the doc gives them, not a flat list of nits.
- A concrete change for each serious problem, and what it costs in latency, complexity or money.
- What the doc never mentions and must before it ships: how a customer disputes a bill, how usage is audited or reconciled, and who is paged when counting breaks.
- A clear verdict at the end: approve, approve with changes, or send back, and the two or three changes that would flip it.
Follow-ups
- A customer says the invoice is 8% higher than their own logs. How does your design let you answer them?
- Prices change in the middle of a month. What does your data model store so old usage keeps its old price?
- Prepaid customers want to be cut off within a minute of running out, not a day. What changes, and what does it cost the gateway?
Where it's asked
- Anthropic · System design55 min · Design discussion, often typed into a shared Google Doc rather than drawn; some rounds let you share any drawing tool · No AI
What it tests
design review · billing · metering · llm api
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
