Design a Resumable Streaming AI Chat
Design a real-time AI chat that streams audio, survives disconnects, and drains WebSocket servers safely during deployment.
Problem statement
For this exercise, assume the following customer-service AI chat contract. Design an AI chat box whose clients connect to the service over WebSockets. The service receives streamed model output from an LLM API over SSE and delivers the agent response as ordered audio frames.
A listener may disconnect before hearing the full response, so the client acknowledges the last frame actually heard and must be able to resume after reconnecting.
The service runs on Kubernetes, scales through stateless application instances, and must handle SIGTERM, graceful socket shutdown, and an appropriate termination grace period without losing resumable session state.
Also consider a partially failing LLM provider: some requests may fail or a stream may end after partial output while other requests still succeed.
Explain how the service and client distinguish generation failure from a broken delivery connection, use bounded retries with jitter, and report recoverable or terminal outcomes without silently replacing already heard output.
What to build
- A user can submit a chat message and hear the agent response as ordered audio frames while it is produced.
- The client can acknowledge the last audio frame the user actually heard for each agent response.
- After a socket disconnect, the client can reconnect and resume an unfinished response after its last acknowledged frame.
- WebSocket-serving application instances can scale horizontally without keeping the only copy of resumable session state in one process.
- A terminating Kubernetes instance can stop accepting work, drain or redirect active sockets, and preserve the metadata needed for reconnect.
At scale
- The interview report did not provide traffic, active-session, or retention numbers; state and justify assumptions before sizing components.
- Each active client chat uses a WebSocket, while the service consumes streamed model output from the LLM API over SSE.
- Agent responses are delivered as ordered audio frames, and a listener may disconnect before hearing the complete response.
- Kubernetes may terminate a serving instance, so the design must define SIGTERM handling and a negotiated termination grace period.