A model gateway's upstream stream fails after 200 output tokens are already on the user's screen: can the gateway fail over, and what should the user see?
answer
- the first forwarded token commits
- invisible only before first byte
- error, restart or continue
- never splice two models silently
- tool calls restart whole
basics
~20 sNot invisibly: once tokens reach the client, a fallback cannot seamlessly continue the same answer. Per route, the gateway either ends with a clear error, restarts on the fallback after an explicit reset signal, or asks the fallback to continue.
solid answer
~50 sThe first token the gateway forwards is a **commit point**. Before it, the gateway can fail over to the next entry and the user never knows. After it, there is no invisible option, and the route picks one of three honest ones: **end the stream with a clear error** and keep the partial text marked incomplete; **restart on the fallback** after sending the client an explicit reset event so it discards the partial answer; or **continue**, by sending the fallback the original request plus the partial answer and asking it to carry on. Continuation is the most fragile: tone shifts at the seam and not every target accepts a partial reply to extend. A stream that died inside a tool call or structured output must be restarted in full and the fragment discarded. The gateway must never splice a second model's text onto the first without telling the client, and you often pay for both attempts.
code
json · 8 lines[
{"event": "delta", "text": "The termination clause requires ninety days"},
{"event": "delta", "text": " written notice, and the"},
{"event": "upstream_failed", "attempt": 1, "target": "primary"},
{"event": "reset", "reason": "failover", "attempt": 2, "discard_previous": true},
{"event": "delta", "text": "The termination clause requires ninety days of written notice."},
{"event": "done", "attempts": 2, "answered_by": "fallback-entry-2"}
]go deeper
Recall that failover before the first token is invisible, and that once text is on screen any switch to another model is visible to the user.
Explain the commit point and the three options after it, end with an error, restart with a reset event, or continue, and what each costs.
Choose per route: restart structured and tool-call routes in full, never splice silently, signal the client explicitly, and account for both billed attempts.
Decide whether a short initial buffer is worth its latency on interactive routes, and set the house rule for which routes may continue across models.
## The commit point A streamed model reply arrives as a sequence of small events, typically over server-sent events, each carrying a few **output tokens** (pieces of words). The gateway relays them to the client as they arrive so the user sees text appear at once. That relay creates a **commit point**: the moment the gateway forwards the first token. Up to then, nothing has reached the user and the gateway is free to try another target. After it, the user is reading an answer from one specific model, and any switch is visible. ## Before the first token: invisible failover If the upstream connection fails, returns an error or produces nothing before the first token, the gateway simply sends the call to the next entry of the route's fallback chain. The main cost is extra time to first token. This is the common case, because many provider failures show up as an error response or a stall before any output. How long to wait for that first token before giving up belongs to timeouts in general; what matters here is that the gateway only fails over silently while it has not yet forwarded anything. ## After tokens are on screen: three honest options | Option | What the user sees | Cost | Fits | |---|---|---|---| | End with an error | Partial text, marked incomplete, with a retry action | Lowest; the partial attempt may be billed | Routes where a human can just ask again | | Restart on the fallback | Partial text replaced by a fresh full answer | Both attempts billed; full answer latency again | Structured output, tool calls, short answers | | Continue on the fallback | Answer carries on from where it stopped | Both attempts billed; original prompt resent | Long prose where restarting would waste a lot | For **restart**, the gateway sends an explicit reset event so the client knows to discard what it shows; how the client renders the replacement is a front-end concern. For **end with an error**, the gateway emits a distinct failure event rather than just closing the connection, so the client can tell "finished" from "cut off". Whatever the route chooses, the client must be told what happened in an event of its own; for the restart option a sensible sequence is the text deltas, an upstream-failure event, a reset event telling the client to discard the partial text, the fallback's deltas, and a done event naming the target that answered. A client that only ever sees plain text deltas has no way to know two models wrote its answer. ## Why continuation is fragile Continuation means sending the fallback the original request plus the partial reply, framed as the start of its own answer, and asking it to keep going. It can work, but: - **The seam shows.** A second model picks up mid-thought in its own style, vocabulary and structure. - **The cut may fall mid-word or mid-number.** Tokens split words, so the partial text may end in a fragment the fallback misreads. - **Support varies.** Not every target accepts a partial assistant reply to extend, and some treat it as finished text. - **Consistency is not guaranteed.** The fallback may disagree with what the first model already said, producing an answer that contradicts itself. - **Cost doubles up.** The fallback reads the whole prompt again plus the partial reply. ## Structured output and tool calls restart whole If the stream died while emitting a tool call's arguments or a JSON object, the fragment is not a partial answer; it is invalid data. Never let a second model finish it, and never pad it into valid JSON. Discard it and restart the whole call, and make sure no tool was executed on the fragment. ## Billing and observability Many providers bill the output tokens they generated even if the stream broke, so a mid-stream failover often costs the partial attempt plus the full second one. Record both attempts against the route, with the target that failed and the one that finished, so a rising mid-stream failure rate shows up as an incident rather than as a quiet increase in cost. ## Choosing per route 1. **Interactive chat**: restart with a reset, or continue for long answers, and show a subtle notice. 2. **Structured or tool-calling routes**: always restart in full. 3. **Background jobs**: no streaming to a person, so buffer and fail over freely.
- Why should a stream that died inside a tool call's arguments never be finished by another model?The fragment is invalid data, not a partial answer, and a second model guessing the rest can produce plausible but wrong arguments that a tool then executes. The gateway should discard the fragment, make sure nothing ran on it, and restart the whole call on the fallback so the tool call is produced in one piece.
- Why not buffer every stream until it finishes, so that failover is always invisible?Buffering removes the reason to stream: the user waits for the whole answer instead of seeing the first words in a second or two. It suits background jobs, and some routes hold a short initial buffer to catch early failures, but for interactive routes the latency cost is usually larger than the rare mid-stream failure it hides.
- The gateway can silently splice the fallback's text onto the partial answer.
- After the first token, failover is impossible in any form.
- Buffering every response costs nothing and solves mid-stream failover.
- A partial tool call can safely be finished by a different model.
- Providers never bill the tokens a broken stream already produced.