In my newest posts, talked quite a bit about immediate caching in addition to caching usually, and the way it can enhance your AI app by way of value and latency. Nevertheless, even for a totally optimized AI app, typically the responses are simply going to take a while to be generated, and thereโs merely nothing we are able to do about it. After we request massive outputs from the mannequin or require reasoning or deep pondering, the mannequin goes to naturally take longer to reply. As affordable as that is, ready longer to obtain a solution may be irritating for the consumer and decrease their general consumer expertise utilizing an AI app. Fortunately, a easy and easy manner to enhance this challenge is response streaming.
Streaming means getting the mannequinโs response incrementally, little by little, as generated, relatively than ready for the complete response to be generated after which displaying it to the consumer. Usually (with out streaming), we ship a request to the mannequinโs API, we look forward to the mannequin to generate the response, and as soon as the response is accomplished, we get it again from the API in a single step. With streaming, nonetheless, the API sends again partial outputs whereas the response is generated. It is a relatively acquainted idea as a result of most user-facing AI apps like ChatGPT, from the second they first appeared, used streaming to point out their responses to their customers. However past ChatGPT and LLMs, streaming is actually used in all places on the internet and in trendy purposes, comparable to for example in dwell notifications, multiplayer video games, or dwell information feeds. On this put up, we’re going to additional discover how we are able to combine streaming in our personal requests to mannequin APIs and obtain an analogous impact on customized AI apps.
There are a number of totally different mechanisms to implement the idea of streaming in an utility. Nonetheless, for AI purposes, there are two extensively used kinds of streaming. Extra particularly, these are:
- HTTP Streaming Over Server-Despatched Occasions (SSE): That may be a comparatively easy, one-way kind of streaming, permitting solely dwell communication from server to consumer.
- Streaming with WebSockets: That may be a extra superior and complicated kind of streaming, permitting two-way dwell communication between server and consumer.
Within the context of AI purposes, HTTP streaming over SSE can help easy AI purposes the place we simply have to stream the mannequinโs response for latency and UX causes. Nonetheless, as we transfer past easy requestโresponse patterns into extra superior setups, WebSockets grow to be notably helpful as they permit dwell, bidirectional communication between our utility and the mannequinโs API. For instance, in code assistants, multi-agent programs, or tool-calling workflows, the consumer might have to ship intermediate updates, consumer interactions, or suggestions again to the server whereas the mannequin continues to be producing a response. Nevertheless, for most straightforward AI apps the place we simply want the mannequin to offer a response, WebSockets are often overkill, and SSE is ample.
In the remainder of this put up, weโll be taking a greater have a look at streaming for easy AI apps utilizing HTTP streaming over SSE.
. . .
What about HTTP Streaming Over SSE?
HTTP Streaming Over Server-Despatched Occasions (SSE) relies on HTTP streaming.
. . .
HTTP streaming signifies that the server can ship no matter it’s that it has to ship in components, relatively than all of sudden. That is achieved by the server not terminating the connection to the consumer after sending a response, however relatively leaving it open and sending the consumer no matter extra occasion happens instantly.
For instance, as an alternative of getting the response in a single chunk:
Hiya world!we might get it in components utilizing uncooked HTTP streaming:
Hiya
World
!If we had been to implement HTTP streaming from scratch, we would want to deal with every little thing ourselves, together with parsing the streamed textual content, managing any errors, and reconnections to the server. In our instance, utilizing uncooked HTTP streaming, we must someway clarify to the consumer that โHello world!โ is one occasion conceptually, and every little thing after it could be a separate occasion. Happily, there are a number of frameworks and wrappers that simplify HTTP streaming, one among which is HTTP Streaming Over Server-Despatched Occasions (SSE).
. . .
So, Server-Despatched Occasions (SSE) present a standardized method to implement HTTP streaming by structuring server outputs into clearly outlined occasions. This construction makes it a lot simpler to parse and course of streamed responses on the consumer aspect.
Every occasion sometimes contains:
- an
id - an
occasionkind - a
knowledgepayload
or extra correctly..
id:
occasion:
knowledge: Our instance utilizing SSE might look one thing like this:
id: 1
occasion: message
knowledge: Hiya world!However what’s an occasion? Something can qualify as an occasion โ a single phrase, a sentence, or hundreds of phrases. What truly qualifies as an occasion in our explicit implementation is outlined by the setup of the API or the server we’re related to.
On prime of this, SSE comes with varied different conveniences, like robotically reconnecting to the server if the connection is terminated. One other factor is that incoming stream messages are clearly tagged as textual content/event-stream, permitting the consumer to appropriately deal with them and keep away from errors.
. . .
Roll up your sleeves
Frontier LLM APIs like OpenAIโs API or Claude API natively help HTTP streaming over SSE. On this manner, integrating streaming in your requests turns into comparatively easy, as it may be achieved by altering a parameter within the request (e.g., enabling a stream=true parameter).
As soon as streaming is enabled, the API not waits for the total response earlier than replying. As a substitute, it sends again small components of the mannequinโs output as they’re generated. On the consumer aspect, we are able to iterate over these chunks and show them progressively to the consumer, creating the acquainted ChatGPT typing impact.
However, letโs do a minimal instance of this utilizing, as traditional the OpenAIโs API:
import time
from openai import OpenAI
consumer = OpenAI(api_key="your_api_key")
stream = consumer.responses.create(
mannequin="gpt-4.1-mini",
enter="Explain response streaming in 3 short paragraphs.",
stream=True,
)
full_text = ""
for occasion in stream:
# solely print textual content delta as textual content components arrive
if occasion.kind == "response.output_text.delta":
print(occasion.delta, finish="", flush=True)
full_text += occasion.delta
print("nnFinal collected response:")
print(full_text)On this instance, as an alternative of receiving a single accomplished response, we iterate over a stream of occasions and print every textual content fragment because it arrives. On the similar time, we additionally retailer the chunks right into a full response full_text to make use of later if we need to.
. . .
So, ought to I simply slap streaming = True on each request?
The brief reply isn’t any. As helpful as it’s, with nice potential for considerably bettering consumer expertise, streaming isn’t a one-size-fits-all resolution for AI apps, and we must always use our discretion for evaluating the place it ought to be carried out and the place not.
Extra particularly, including streaming in an AI app may be very efficient in setups once we anticipate lengthy responses, and we worth above all of the consumer expertise and responsiveness of the app. Such a case could be consumer-facing chatbots.
On the flip aspect, for easy apps the place we anticipate the supplied responses to be brief, including streaming isnโt seemingly to offer important beneficial properties to the consumer expertise and doesnโt make a lot sense. On prime of this, streaming solely is smart in circumstances the place the mannequinโs output is free-text and never structured output (e.g. json information).
Most significantly, the key disadvantage of streaming is that we aren’t in a position to assessment the total response earlier than displaying it to the consumer. Bear in mind, LLMs generate the tokens one-by-one, and the which means of the response is shaped because the response is generated, not prematurely. If we make 100 requests to an LLM with the very same enter, we’re going to get 100 totally different responses. That’s to say, nobody is aware of earlier than the responses are accomplished what it will say. Because of this, with streaming activated is rather more troublesome to assessment the mannequinโs output earlier than displaying it to the consumer, and apply any ensures on the produced content material. We will all the time attempt to consider partial completions, however once more, partial completions are tougher to judge, as we now have to guess the place the mannequin goes with this. Including that this analysis must be carried out in actual time and never simply as soon as, however recursively on totally different partial responses of the mannequin, renders this course of much more difficult. In follow, in such circumstances, validation is run on the complete output after the response is full. However, the difficulty with that is that at this level, it could already be too late, as we might have already proven the consumer inappropriate content material that doesnโt move our validations.
. . .
On my thoughts
Streaming is a function that doesnโt have an precise affect on the AI appโs capabilities, or its related value and latency. Nonetheless, it will possibly have an amazing affect on the best way the consumerโs understand and expertise an AI app. Streaming makes AI programs really feel sooner, extra responsive, and extra interactive, even when the time for producing the whole response stays precisely the identical. That stated, streaming isn’t a silver bullet. Totally different purposes and contexts might profit roughly from introducing streaming. Like many selections in AI engineering, itโs much less about whatโs doable and extra about what is smart in your particular use case.
. . .
For those who made it this far, you would possibly discover pialgorithms helpful โ a platform weโve been constructing that helps groups securely handle organizational information in a single place.
. . .
Liked this put up? Be a part of me on ๐Substack and ๐ผLinkedIn
. . .
All photos by the creator, besides talked about in any other case.



