LLM ROUTING

Recognize OpenRouter keepalive comments without treating them as model text

In this article
THE QUICK TAKE

SSE framing is a transport protocol: comments are not JSON answers, and a live connection does not by itself prove successful model completion.

A chat interface can crash even when the model and network behave correctly if its stream parser treats every received line as JSON. OpenRouter documents occasional SSE comments used to keep connections alive. A robust client parses the event framing first, then interprets the selected API’s data events and completion state.

Recognize comment lines

The documented keepalive example begins with a colon: : OPENROUTER PROCESSING. Such a line is an SSE comment and can be ignored for answer assembly. It may support a loading indicator, but it is not a generated token or a final response. Sending it directly to a JSON parser creates a client-side error unrelated to the content the model is producing.

Buffer transport chunks into events

Network chunks need not align with complete SSE events or complete character sequences. Use a parser that handles framing and buffered decoding rather than assuming each read is one JSON object. For a hypothetical code-explanation UI, a text delta may arrive across several transport reads. The parser assembles an event before the application decides whether its data contains text, a tool update or another supported payload.

Interpret completion by endpoint

The current streaming guide distinguishes Chat Completions accounting chunks from Responses completion events. Chat Completions includes a final usage frame before its done marker, with a content-free choice that repeats the finish reason. Treat that as accounting rather than a second answer. Responses reports usage through its completed event. A shared client can normalize its UI while preserving those endpoint-specific lifecycle rules internally.

Handle errors after headers are committed

An error before streaming can return a normal JSON error response. Once response headers are committed, a later failure can arrive as a data event while the HTTP status remains successful. Inspect the documented failure events for the chosen API. A network response marked OK therefore cannot be the only success criterion. Preserve partial output as partial when a later event reports failure.

Test framing and lifecycle separately

Use fixtures with a keepalive comment, split event data, ordinary deltas and a mid-stream failure. Confirm that comments never enter the displayed answer and that completion is recorded only for the appropriate event. These tests validate the client’s protocol handling without making provider latency claims. The resulting UI can show activity honestly while retaining enough event information to explain a failed or interrupted generation.

Documentation and further study

Related engineering guides: Reading an OpenRouter streaming response incrementally; Errors inside an OpenRouter stream: why HTTP 200 is insufficient.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles