跪拜 Guibai
← All articles
Frontend · Artificial Intelligence

AI Streaming Cuts Out at 90%: What 'Continue' Actually Does Under the Hood

By Canace ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

As AI output shifts from human reading to machine consumption — JSON for APIs, HTML for rendering, tool calls for real-world actions — a mid-stream break stops being a minor annoyance and becomes a data integrity and safety problem. Handling it correctly means separating raw deltas, parse state, and commit state, and never executing a half-formed tool call.

Summary

A dropped AI stream isn't just a missing sentence. For structured outputs like JSON, HTML, or tool-calling parameters, a mid-stream break produces unparseable objects, broken page layouts, or duplicate side effects like double charges. The fix depends entirely on where the break occurred — client connection, upstream model request, or the generation service itself. Only client-side disconnects allow precise replay; anything deeper forces a semantic re-continuation that can shift style, repeat content, or corrupt structure. Production systems decouple the generation task from the client subscription, persist every output chunk as a sequenced event log, and treat half-finished payloads as display-only drafts until validation passes.

Takeaways
Client-side disconnects are the easiest case: the model keeps running, and the server just replays missed events without a new inference.
If the upstream model request itself fails, the system can only feed existing text back into a new prompt, which risks repetition, style drift, and structural breaks.
Structured outputs like JSON and HTML must use incremental parsers and never be concatenated from two separate generation attempts.
Tool-calling agents need idempotency keys and status tracking to prevent duplicate emails, charges, or record creation on retry.
An event log with per-chunk sequence numbers, generation IDs, and revision markers guarantees no content is lost or duplicated during reconnection.
KV Cache can skip recomputation of preceding tokens but is rarely exposed by third-party APIs; most products rely on event replay first and fall back to semantic continuation.
Conclusions

Most products conflate three distinct failures — client drop, model request failure, and service crash — under a single 'continue' button, which hides very different recovery guarantees from the user.

The principle that 'half-finished output can be displayed but never parsed or executed' is a useful boundary that applies across plain text, code, JSON, and agent tool calls.

Decoupling the generation task from the client connection is an architectural choice that turns a brittle streaming experience into a resilient background job, but it requires deliberate state management that many early AI integrations skip.

Concepts & terms
KV Cache
A cache of Key and Value tensors produced during the attention computation for previously processed tokens. It allows a language model to skip recomputing the entire context when generating the next token, serving as the model's 'calculation draft' for faster continuation.
Event Log (for streaming)
A sequenced record of every output chunk from a generation task, tagged with a generation ID, revision number, and monotonically increasing sequence number. Clients use it to request only missing chunks after a disconnect, ensuring no duplication or loss.
Incremental Parser
A parser that processes a stream chunk-by-chunk while maintaining internal state about open strings, brackets, and structures. It allows partial JSON or other structured data to be validated progressively rather than waiting for the complete payload.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗