Loading...
Loading...
Design the backend for a ChatGPT-like conversational AI product where users send text prompts inside multi-turn conversations and receive model-generated responses streamed token by token.
The system must preserve conversation history so users can leave a thread and resume it days later. For every new request, the backend loads recent conversation turns, constructs a prompt that fits within the model's context window, applies input safety checks, schedules inference on a GPU cluster, and streams generated tokens back through Server-Sent Events (SSE).
The main challenge is that the product should feel immediately responsive even though GPU inference is expensive and may run for several seconds. The time-to-first-token (TTFT) target leaves only a small budget for history loading, context construction, safety checks, admission control, queueing, and the first model decode.
Peak demand introduces another constraint: roughly 50,000 inference requests may be active simultaneously. When GPU capacity is exhausted, paid users must remain within their service SLO, while free-tier requests may be queued or rejected. The system therefore needs an explicit admission-control and priority-scheduling policy rather than treating every request identically.
The design should also separate durable conversation state from the live inference stream. A dropped SSE connection must not erase already generated content. Users who reconnect should be able to recover the partial assistant response that had already been produced.
Users should be able to create conversations, send text messages, list previous conversations, and reopen an existing thread with its durable message history.
Sending a user message should start an AI inference request whose response is streamed progressively through SSE rather than waiting for the full response to complete.
Before inference, the system should assemble the conversation context from persisted turns. If the resulting prompt exceeds the model's approximately 8K-token context limit, the oldest conversation turns should be dropped until the request fits. Summarization is out of scope.
The platform should apply safety guardrails to both sides of generation: user input should be screened before inference, and generated output should be filtered before it is delivered to the client.
The system should persist completed conversation turns and enough in-progress response state that a user reconnecting after an SSE drop can recover text already generated before the disconnect.
Per-user usage limits, token budgets, rate limits, and overload behavior should depend on subscription tier. Paid requests receive priority when GPU capacity is scarce, while free-tier requests may wait or receive an explicit overload response.
Time-to-first-token should be at or below approximately 500 ms at p50 and 2 seconds at p95 under normal operating conditions.
Once generation begins, the system should sustain approximately 30–60 output tokens/sec per active stream.
The chat completion API should target approximately 99.9% availability.
The platform should support approximately 100M registered users, 10M daily active users, and around 50K concurrent inference requests at peak.
Durable conversation turns must survive application and service restarts. Live transport failure should not cause previously persisted conversation history to disappear.
GPU utilization should remain high enough to control inference cost while admission control prevents excessive queueing from destroying paid-user latency SLOs.