Streaming through a gateway: three SSE dialects and the one boundary you cannot retry
Streaming fixes perceived latency, but after the first token a retry means double billing. Here are the three upstream event formats, what a gateway must translate, and how to resume a broken stream.
The short version
stream: true solves *waiting*. The first token lands early and the product feels alive; total time is unchanged.
It also creates a hard boundary: once the first byte is out, the call can no longer be retried. Gateway retries, channel failover and downgrades only work before the first packet. So the real design problem is two timeouts — a first-packet timeout (retryable) and an idle timeout (resume only).
Three upstreams, three formats
All called SSE, none identical in code:
- Chat Completions:
data: {JSON}lines, text deltas underchoices[0].delta.content, terminated bydata: [DONE]1. - Anthropic Messages: named semantic events —
message_start(emptycontent), a run ofcontent_block_deltacarryingtext_delta, thenmessage_deltaandmessage_stop2. - OpenAI Responses API: semantic events again, with text in
response.output_text.deltaandresponse.completedat the end1.
In other words: one OpenAI-compatible /v1/chat/completions endpoint may sit on top of three completely different event streams.
What the gateway must get right
- Protocol translation — compress Claude
content_block_deltainto OpenAI chunks, translatemessage_stopinto[DONE]. Order andindexmust survive, or the UI renders out of order.
- Usage aggregation — Anthropic's
usageinsidemessage_deltais cumulative, not incremental2. Take the last value, never sum. Get this line wrong and tokens are over-counted.
- First-packet gating — until the first SSE event arrives the request is still retryable; after it, pin the channel. Most "one request billed across three channels" anomalies come from missing this lock.
Three client-side traps
- A total timeout on a streaming call.
timeout=30chops long answers in half. Use connect plus first-packet timeouts, then a read-idle timeout. - Proxy buffering. Nginx defaults to
proxy_buffering on, which batches SSE and turns the stream into "nothing… then everything". Setproxy_buffering offand dropX-Accel-Buffering. - Re-sending the whole prompt after a broken stream. The most expensive mistake: tokens already produced are billed, and the resend bills again.
How to resume
The official recipe is to keep the partial response and use it as a new starting point2, with a caveat per model generation:
- Claude 4.5 and earlier: prepend the partial response as an assistant message and re-send.
- Claude 4.6 and later: send a user message containing the partial response plus a continue instruction ("the previous output was interrupted here; continue from that point").
Tool-use and thinking blocks cannot be partially recovered — resume from the last text block. Long agent runs should checkpoint text summaries separately instead of hoping a half-finished tool_use can be stitched back together.
A parser that actually runs
import json, requests
r = requests.post(
"https://www.salpx.com/v1/chat/completions",
headers={"Authorization": "Bearer sk-..."},
json={"model": "claude-sonnet-4-5", "stream": True,
"messages": [{"role": "user", "content": "Tell me a dry joke"}]},
stream=True, timeout=(5, 60),
)
for line in r.iter_lines(decode_unicode=True):
if not line or not line.startswith("data: "):
continue
payload = line[6:]
if payload == "[DONE]":
break
delta = json.loads(payload)["choices"][0]["delta"]
print(delta.get("content") or "", end="", flush=True)
In timeout=(5, 60), five seconds covers connect and first packet, sixty covers the idle gap between chunks. Replace 60 with a total timeout and long answers get cut off by your own client.
What it does to billing
Streaming does not change token prices, but it changes two things:
- Failed retries cost double. A failed non-stream request usually produced nothing upstream; a stream that emitted 800 tokens before dying bills for those 800 — then bills again on the resend.
- Deduplicate by request_id. One user action can map to several request records. Without deduplication, retries inflate the denominator and make your success rate look worse than it is.
Bottom line
Streaming is a UX optimisation, not a reliability one. It compresses the retry window to everything before the first packet; the rest has to be covered by resumption and idempotency.
Sources & attribution
This article references the public materials listed below. Analysis and wording are SalpX's own; quoted material remains the property of its original authors and publishers.
- Streaming responses — API guide — OpenAI · accessed 2026-11-13Note:The distinction between Chat Completions chunks and Responses API semantic events described here follows this official guide.
- Streaming Messages — API Reference — Anthropic · accessed 2026-11-13Note:Event order (message_start/content_block_delta/message_stop), cumulative usage in message_delta, and the resume-after-interruption procedure are taken from this reference.
One key for Claude, GPT and 50+ models
The SalpX gateway is OpenAI-SDK compatible. Point base_url at https://www.salpx.com/v1 and you are done.