Skip to content
Preptima
hardDesignMidSeniorStaff

Design a chat service that streams model output to users and lets them press stop. What has to happen when they do?

Stream tokens over server-sent events or a WebSocket, but treat stop as a server-side command routed to whichever worker owns the generation. That worker aborts the upstream model call, saves the partial reply with a stopped status, and sends a final event so the client and the stored history agree.

6 min readUpdated 2026-10-10

Answer

Short answer

Streaming is the easy half. Send tokens to the browser over server-sent events or a WebSocket, but treat the stop button as a command to the server, not a client-side switch. The worker that owns the generation must abort the upstream model call, save the partial reply with a stopped status, and emit a final event so the screen and the stored history agree.

Was this answer helpful?

Likely follow-ups

  • The user refreshes the page halfway through a reply. How does the new tab pick up the same stream?
  • How do you run moderation on output the user is already reading?
  • A stopped reply is half a sentence. What goes into the context of the next turn: the fragment, nothing, or a marker?
  • Your provider keeps generating for a moment after you close the connection. How would you find out what you are billed for?

Related questions

Masterclass guide

Further reading