Resuming Interrupted LLM Streaming Responses: A Stateful Gateway Design

    This article explains how a stateful gateway can resume an LLM streaming response after a mid-stream failure. It covers the causes of interruptions, a two-leg request design that keeps the gateway stateless across requests, and how to detect the true end of a stream by checking for `finish_reason` when `stream_options.include_usage` is enabled.

    Long LLM responses are rarely returned all at once. They are streamed to the user’s screen chunk by chunk, which improves time to first token and makes the interaction feel more responsive. In a complex distributed inference environment, however, a longer streaming path carries a higher risk of interruption. If the inference instance runs out of memory (OOM), gets preempted by the scheduler, or loses its network connection mid-generation, the text on screen stops abruptly.

    The usual fix is to retry the whole request. This forces the user to wait again, and the expensive GPU compute already spent on the partial response is wasted. The real question is whether the compute already produced can be reused, not only whether the service can restart.

    1. Goal and Challenges of Resumable Streaming

    The core goal of resumable streaming is to let the backend recover from a failure automatically, so the user sees one complete, continuous answer. Achieving this requires detecting the moment of interruption and using the content already generated to send a new request.

    This raises three challenges:

    1. Content continuity: The continued output must connect to the text already sent, both semantically and logically, with no repetition or gaps.
    2. State awareness: The gateway or proxy layer cannot act as a transparent byte forwarder. It must know how much content has already been sent so it can resume from the correct position after reconnecting.
    3. Scalability: To handle high concurrency, the continuation service must scale horizontally. Its state must not tie requests to specific nodes.

    2. Core Architecture: Introducing a Stateful Gateway

    To address these problems, a common and effective approach is to add a dedicated stateful gateway between the client and a general-purpose inference engine, such as a mainstream open-source inference framework.

    The request path typically looks like this:
    Client → Upstream entry layer (handles authentication and rate limiting) → Stateful continuation gateway → Downstream routing gateway → Inference instance

    The stateful continuation gateway communicates only with the downstream routing gateway. Health checks and load balancing are left entirely to the routing layer. When a continuation is needed, the gateway sends the request to the same downstream routing address, and the routing layer directs it to a currently healthy inference instance. If the business needs finer control, such as sending first requests to Cluster A and continuation requests to Cluster B, it can do so by configuring different routing addresses.

    3. Request Lifecycle: A Two-Leg Design

    Inside the gateway, each user request is modeled as up to two “legs.” This structure implements the resumable streaming logic.

    Leg 1: Forwarding and accumulating the original request
    When the client’s original request reaches the gateway, the gateway forwards it to the downstream inference engine and starts listening to the stream. In this phase, the gateway forwards each received chunk to the client as-is to keep latency low. At the same time, it parses the stream incrementally. The gateway parses the data stream according to the SSE (Server-Sent Events) protocol and keeps an in-memory record of the reasoning (chain-of-thought) and content (response text) already sent to the client.

    Leg 2: Continuing after an unexpected disconnect
    If Leg 1 ends normally, the request is complete. If the connection drops or the downstream service fails during Leg 1, the gateway does not return an error to the client. Instead, it starts Leg 2.
    Leg 2 uses the text accumulated in Leg 1 as a prefix in the context and asks the inference engine to generate the remaining content. When the Leg 2 stream returns, the gateway rewrites it. It removes the part that repeats the prefix, then attaches the new content directly after the text Leg 1 already forwarded, and keeps pushing it to the client.

    4. Architecture Property: Stateful Per Request, Stateless Across Requests

    The key design feature of this gateway is its state model: stateful within a single request, stateless across requests.

    Continuation requires the gateway to remember what it has already forwarded, parse SSE semantics, issue new requests, and rewrite the usage (token usage) statistics. For this reason, the gateway cannot be a pure transparent byte forwarder. All of this state lives inside the coroutine or lightweight thread that handles that specific request.

    From a global view, replicas share no in-memory state. The gateway can therefore be deployed like an ordinary stateless microservice on a container platform or cloud-native environment, such as Baidu AI Cloud’s container service. It can scale horizontally and elastically without sticky sessions. This design handles complex logic while meeting the high-availability requirements of cloud-native architecture.

    5. A Common Pitfall: SSE Semantics and the [DONE] Marker

    When implementing a streaming continuation gateway, developers often make a mistake with the end-of-stream signal. Many assume that receiving [DONE] is the only sign that generation is complete.

    In mainstream LLM streaming APIs, however, enabling stream_options.include_usage changes the tail of the stream. The typical ending sequence is:

    1. A data chunk containing the finish_reason field, which marks the end of model generation.
    2. A chunk containing only usage statistics.
    3. The protocol-level [DONE] marker.

    If the connection drops after any of these three steps, the model has already finished generating the text. If the gateway relies only on [DONE] to decide whether to trigger Leg 2, it treats a completed response as interrupted. This triggers an unnecessary continuation request, wastes compute, and can cause duplicated or garbled text on the frontend.

    Therefore, when parsing SSE incrementally, the gateway must detect finish_reason precisely. Once a finish_reason appears in a chunk, core generation is complete, and a disconnect afterward does not need to trigger continuation. Only a disconnect before finish_reason arrives should enter the Leg 2 error-handling path.

    6. Summary

    Interruptions in streamed long-text generation are an unavoidable reality in production. Placing a stateful gateway between the client and the inference engine reduces wasted compute and protects the user experience. The gateway splits each request into a forwarding-and-accumulating phase and a continuation phase. Keeping state isolated per request and handling SSE semantics precisely are the keys to reliable continuation. This design improves fault tolerance and provides a solid foundation for highly available, high-concurrency LLM inference services.