SabaKore

Home / Articles / AI guides

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, a junior infrastructure engineer

Ren

Can I build a feature where text appears a little at a time, like ChatGPT?
Procura, a cloud AI robot who explains

Procura

You can. With a method called streaming, you send the parts in order as they're ready, without waiting for the AI to finish producing its whole answer. Users perceive the waiting time as shorter.

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, a junior infrastructure engineer

Ren

Compared with returning the whole answer at once, what changes on the VPS side?
Procura, a cloud AI robot who explains

Procura

The big difference is that the connection time for each exchange gets longer. A communication that finishes in an instant when you return everything at once stays open until the answer finishes when you send it bit by bit.

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, a cloud AI robot who explains

Procura

The more you do to improve how it feels, the more it often creates a different load behind the scenes. Don't be satisfied with just the visible effect; also check how much the time connections stay open has grown.
Ren, a junior infrastructure engineer

Ren

If access keeps growing, might it be better to stop streaming?
Procura, a cloud AI robot who explains

Procura

In situations where access is extremely concentrated, it's a possible option. But rather than stopping suddenly, reviewing the setting for the number of connections you can hold and your VPS plan first often has less impact on users.

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, a junior infrastructure engineer

Ren

So it feels better, but you look at the load from keeping connections open separately.

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