Design A Token-Limited Multi-Profile Chat
Authenticate a user into one profile, stream chat generations, and reject work when that profile's remaining token budget is exhausted.
Problem statement
Design a ChatGPT-like chat service whose hard product pressure is a remaining token budget, multiple profiles for one person, and the authentication flow that binds a session to one profile.
For this exercise, assume an authenticated user continues a chat until the remaining token budget is exhausted and further generations are rejected. For this exercise, assume token remaining and chat history are isolated per user profile.
For this exercise, assume a login or profile-switch binds the session to exactly one profile before any generation starts. For this exercise, assume 20,000 concurrent streaming sessions and 5,000 new prompts per second with p99 first-token under 400 ms.
For this exercise, assume one in-flight generation per profile; a second prompt on the same profile is rejected or queued, not run in parallel against the same budget. For this exercise, assume model weights, training, billing invoices, and multi-region active-active are out of scope.
For this exercise, assume a focused follow-up will ask how the last remaining tokens stay correct if two generations race the same profile budget. For this exercise, assume this budget counts generated output tokens only.
Admission must atomically claim the profile's single active-generation slot; the admitted generation has an output cap no greater than its available budget and stops at that cap. Retried accounting for the same generation must not charge twice.
What to build
- A person can authenticate and bind the session to one of their profiles before chatting.
- A bound profile can submit a prompt and receive a streamed generation while remaining tokens last.
- When the profile has too few remaining tokens, the service rejects the generation without starting a stream.
- Chat history and remaining tokens for one profile are never mixed with another profile of the same person.
At scale
- For this exercise, assume 20,000 concurrent streaming sessions and 5,000 new prompts per second.
- For this exercise, target p99 time to first token under 400 ms for admitted generations.
- For this exercise, assume remaining tokens are checked before the first token and decremented as tokens are produced.
- For this exercise, remaining tokens are the live authority for admission at 5,000 prompts per second; history storage must not sit on that check.