Store conversations and messages in Postgres (indexed by conversation_id, created_at DESC for keyset history). For AI chat: POST /messages creates user message + empty/pending assistant run, streams tokens over SSE (backed by Redis Pub/Sub if decoupling worker from gateway), persists final message + token counts on completion, and supports reconnect/resume from last chunk.
Follow-up Interview Question · How do you handle a user refreshing the browser tab mid-stream without losing the generated answer?