Sarghaby Araghatta
Home / Knowledge Base / Streaming responses (token streaming / SSE)
Serving & infrastructure · AI Engineering

Streaming responses (token streaming / SSE)

Delivering the model's output incrementally — token by token — over a long-lived connection instead of returning the whole completion in one response.

Request with stream=trueSends requestGenerates tokenFlushed as eventSends token chunksConcatenate and renderClientRequestServer-SentEvents StreamModelGenerates TokensTokenChunksClientConcatenatesand Renders
Token Streaming Process Overview

How it works

Server-Sent Events (SSE) is the primary transport mechanism for streaming responses, using HTTP text/event-stream. This method allows a one-way communication channel where the server continuously sends data to the client. When you set stream=true in APIs like those from OpenAI or Anthropic, the model generates tokens incrementally. Each token is sent as a small event chunk, often containing a delta that represents the change from the previous state. The client receives these chunks and concatenates them, rendering the output as it arrives, which creates a more dynamic interaction. In contrast, WebSockets can be used when you require bidirectional communication, such as for voice applications, while gRPC streaming is suitable for service-to-service communications, offering a different set of capabilities.

Token streaming allows the model to produce output in real time, which means that as soon as a token is generated, it can be sent to the client. This process involves decoding the token and flushing it to the transport layer immediately. The client then processes these tokens in the order they arrive, which can lead to a more engaging user experience. For instance, if a user asks a model to generate a story, they can start seeing the first few sentences almost instantly, rather than waiting for the entire output to be ready. This incremental delivery is crucial for applications that require immediate feedback, such as chatbots or interactive agents.

The ability to stream tokens also introduces challenges in managing the connection and handling errors. If a connection drops, the client must be able to reconnect smooth and continue receiving data without losing context. Since the model can make tool calls mid-generation, the client needs to be equipped to handle these calls and any resulting data appropriately. This means implementing logic for cancellation and managing partial outputs, which can complicate the client-side code but ultimately enhances the responsiveness of the application.

  • Client sends request with stream=true
  • Server generates tokens incrementally
  • Tokens are sent as event chunks
  • Client concatenates and renders tokens
  • Use streaming for responsive UX

The software-engineering analogue

Streaming responses can be likened to a tail -f command that allows you to view log files in real time. Just as you see new log entries appear as they are written, with token streaming, you see new tokens generated by the model as they are produced. This analogy helps to understand the flow of data, where the client continuously updates its display based on incoming information. However, the key difference lies in the nature of the data being streamed: with token streaming, the chunks are generated by an autoregressive model that relies on previous tokens to produce the next one. This means that the pace of the stream is directly tied to the compute time, which can vary based on the complexity of the output.

The analogy breaks down when you consider the need for real-time processing of the tokens. Unlike simple log entries, the tokens generated by the model may require immediate action or context, such as when a user interacts with a chatbot. This adds complexity because the client must be capable of handling mid-stream tool calls, parsing partial JSON responses, and managing cancellations, which are not typically concerns with standard log streaming.

Why not the obvious alternative

A newcomer might first think to implement a blocking request for generating responses. This approach would mean the user has to wait for the entire output to be generated before seeing any results. The problem here is that users are left staring at a blank screen for several seconds or even tens of seconds, which creates a frustrating experience. The perceived latency is high, and users may feel disengaged during this wait, leading to a poor user experience.

The failure of this approach lies in its inability to provide timely feedback. While the total generation time remains unchanged, streaming responses significantly reduce perceived latency by delivering the first token almost immediately. This allows users to start interacting with the output right away. Streaming enables early cancellation of requests, which can save on token usage and costs, making it a more efficient option compared to a blocking request.

When to use which

Streaming responses are ideal for applications like chatbots, interactive agents, or long-form content generation where user engagement is crucial. In these cases, the ability to see the output as it is generated enhances the experience and allows for real-time interaction. However, for tasks that involve short structured data extraction or when you need the entire validated JSON response before proceeding, non-streaming methods are simpler and can prevent issues with partial parsing. This is particularly important in contexts where accuracy and completeness of the data are paramount.

Choosing between streaming and non-streaming should be guided by the nature of the application and user expectations. Streaming incurs some additional complexity in implementation, but it pays off in responsiveness and user satisfaction. Non-streaming is straightforward and can be more efficient for tasks that don't require immediate feedback, so understanding the trade-offs is essential for making the right decision.

Perceived Latency Advantage Streaming reduces perceived latency by delivering the first token quickly, enhancing user experience despite total generation time remaining unchanged.

In production: when it breaks

The scenario Token streaming latency increased to 800ms instead of 200ms.
Connection StableConnection UnstableHigh Latency DetectedCheck Connection StabilityAnalyze Model ThroughputInspect Server LoadOptimize Token BufferingReduce Response Size
Debugging token streaming latency

Why it happens. Streaming responses can experience delays due to network congestion or server-side bottlenecks. If the model's output generation is slower than the expected rate, it can lead to a backlog, causing tokens to be sent to the client at a much slower pace than intended.

  • Check server logs for response times and error rates.
  • Measure network latency between the server and client.
  • Analyze the model's output generation time for each token.
The call To fix this, optimize the model's inference speed or increase the server's capacity. Be aware that increasing capacity may incur additional costs. Monitor the average latency per token and server load to ensure performance remains within acceptable limits.

What it delivers

Streaming responses provide a more responsive user experience by reducing perceived latency and allowing for early cancellation of requests. This leads to a more engaging interaction, where users can see results as they happen. The additional complexity in implementation is minimal compared to the benefits, making it a valuable feature for applications that prioritize user engagement and real-time feedback.