Streaming AI Responses: Implementation and the Load on a VPS
How streaming, which shows the AI's answer bit by bit, works, and how the longer connection time it adds affects the VPS.

Ren

Procura
What's good about it
- The feeling of being 'kept waiting' until the answer is complete decreases
- Even for a long answer, the first few lines appear quickly, so people are less likely to leave
- It's also easier to cut it off if the user wants to stop partway

Ren

Procura
The impact of longer connections
When many processes that keep connections open occur at the same time, even if each one is light, you get closer to the upper limit on how many connections can be handled at once. Once access starts to increase, check whether you're hitting that limit. It's reassuring to make your estimate of how many users you can serve at the same time a bit more conservative than with the return-everything approach.
- Check in advance the upper limit on connections that can be held at the same time
- Add handling that closes the connection without leaving it behind if the user leaves partway
- Once access increases, check whether this mechanism is getting clogged up

Procura
Ren

Procura
How to handle a connection that is cut off partway
Compared with returning everything at once, the effect of a connection being cut off partway is more noticeable with the bit-by-bit method. If transmission stops partway because of network trouble or an action by the user, an incomplete answer can remain displayed. If you make it possible for the screen side to tell that it was cut off, and provide a way to fetch the rest again, you can avoid confusing users.
- Make it possible for the screen side to tell that the connection was cut off partway
- Don't leave a half-finished display as it is; provide a way to fetch it again
- On the server side too, clean up the resources of cut connections without leaving them behind

Ren
This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.