🔥 AI API Stream Drops Fix: Timeout in 5 Steps
Fix AI API stream drops after HTTP 200: learn AI streaming timeout causes, verify NGINX proxy_read_timeout, and debug with streaming API debugging—step by step.

Why Your AI API Stream Stops After HTTP 200 - and How to Find the Fault
Your app gets HTTP 200, shows a few lines of the AI response, and then the output just cuts off mid-sentence. Eventually the connection closes, and the access log still says the request succeeded. It's confusing - only if you assume the HTTP status code is the full story.
A 200 means the server accepted the request and started sending a response. It doesn't prove that your stream reached its normal end, or that your client processed the final event.
The real question is simpler and more concrete: where did the data stop moving, how long did it stay stuck, and who closed the connection? Those details help you tell an idle timeout from a request deadline, a buffering proxy, a parsing/rendering bug, or a UI that stopped updating while bytes were still arriving.
This guide walks through a trace-first approach: instrument every hop, compare the "quiet gaps" to the timeouts that apply there, change only the component the evidence points to, and test both the transport behavior and what the user sees. The goal is to diagnose the failure without logging prompts, credentials, or other sensitive content.
Why does HTTP 200 not mean the AI stream completed
HTTP status tells you what happened at the protocol level - not whether the streamed operation finished cleanly.
With streaming, the server can send headers with status 200 and then stream the body in chunks. If a proxy later times out, the network drops, or the client bails early, the status you already received can't be changed to something else. So the access log can show 200 while the user only got part of the answer.
A streamed flow usually has at least three things you should track:
- HTTP outcome: Did you get a successful status and response headers?
- Transport outcome: Did the connection stay open long enough to deliver the body?
- Application outcome: Did the client receive and interpret the stream's terminal/stop event?
These can disagree. Headers can look "successful" even if the connection ends mid-stream. Bytes can arrive while the parser misses the final event. The parser can be fine while the UI never renders the last chunk.
Treat completion as an application-level state
Most streaming protocols provide a way to tell when the response ended normally. That might be a terminal event name, a specific stop reason, or a documented end-of-stream signal. Your client should use that protocol contract - not "HTTP 200" and not "the socket closed."
For example, the Claude Messages API emits a message_stop event for normal completion and can also include errors in the stream. A client that only checks the status code will miss the difference. Track the documented terminal state and the stop reason separately from the HTTP status.
This is also why you need an explicit "incomplete" state. If the stream closes before the completion signal arrives, keep what you received - but mark the attempt as incomplete. Treating it as "complete" risks showing truncated text as a finished answer, or causing retries to append a second response onto the first.
Why the total request duration is a weak clue
A request that lasted 64 seconds might have ended because of a 60-second idle timeout, a 64-second overall deadline, or something else entirely. Total duration alone doesn't tell you which. What helps more is the idle gap after the last byte arrived at each hop, compared to the relevant timeout for that hop. Also look at the close reason.
How do you find where the stream stopped
Start with one broken request and follow it across the client, your gateway/reverse proxy, and the upstream API. Give each attempt a unique request ID and propagate it through everything you control. Without that, correlating timestamps across logs gets messy fast - especially when requests overlap.
Log events, not just a binary success/failure. At minimum, capture:
- when request/headers arrived
- when the first and most recent body bytes arrived at the client
- when the last upstream read happened at the proxy
- when the last downstream write happened
- the connection close reason
- in your stream parser: the last event type, any parser errors, and whether the expected completion/terminal signal arrived
Build a timeline from last movement to close
Use a monotonic clock inside each process to measure elapsed gaps. Wall-clock timestamps are helpful for cross-service correlation, but clock skew can make fine-grained comparisons misleading. Correlate via request ID and keep each process's elapsed timing local.
Here's the kind of timeline that makes the cause obvious:
| Time | Event |
|---|---|
| 0.00 s | Client starts request |
| 0.60 s | Client receives HTTP 200 |
| 4.20 s | Proxy reads upstream event 13 |
| 4.23 s | Client receives event 13 |
| 64.20 s | Proxy reports an upstream read timeout |
| 64.23 s | Client connection closes before the completion event |
Yes, the request lasted ~64 seconds - but the meaningful interval is the ~60 seconds between the proxy's final upstream read and its timeout. The matching close reason makes "upstream read timeout" a much stronger explanation than "some generic failure."
A server-side log that says "I wrote a chunk" tells you when the server handed bytes off. It still doesn't prove when the client received them. Compare proxy upstream reads, proxy downstream writes, and the client's read timing to see where the timeline stops matching.
Keep diagnostic logs useful and safe
You usually don't need to log prompts, generated text, or credentials just to find a stalled hop. Log timing, event types, model/route, and close reasons where you can.
If you're using an AI API gateway like YW Code API (ywcode), start with the route that actually failed and record the selected model and stream format. Avoid copying sensitive request/response content into logs "just for debugging transport."
How do you tell a timeout from buffering or a client bug
Once you have the last-movement timestamps across hops, the pattern usually points to the culprit.
Compare the last movement at each hop with the timeout or deadline that applies there. Don't just raise every timeout. That can hide the real cause, consume resources longer, and make the next incident harder to interpret.
A repeatable idle gap points to an idle timeout
If upstream data stops, the proxy waits a consistent interval, and then its logs report a read timeout, check the effective upstream read timeout for that route.
In NGINX, proxy_read_timeout is the interval between successive reads from the proxied server. People often encounter 60 seconds as a default - but what matters is the effective config for the specific location handling the stream, including any overrides.
A matching 60-second pause is good evidence, not proof. Confirm it with the proxy close reason and a controlled reproduction. Different gateways may use different defaults or interpret timeouts differently.
A fixed cutoff from request start points to a deadline
If the stream always ends around the same total elapsed time from request start - even when bytes are still arriving - check the client's overall request deadline and any route-level maximum connection duration.
Idle timeouts care about silence. Request deadlines care about total elapsed time. They fix different problems, so don't treat them as interchangeable.
Gateway writes without client reads point downstream
If the gateway keeps receiving upstream bytes but the client sees a long gap, inspect downstream writes and the path to the client.
Buffering can collect chunks and release them in bursts. That means "steady upstream activity" might not translate to "steady downstream activity." Compare timestamps at both ends, not just one server log.
Client bytes without visible updates point to parsing or rendering
If the client's networking layer shows bytes arriving continuously while the UI freezes, the connection may be fine. Look for issues in the stream parser and rendering pipeline:
- does the parser handle partial frames correctly?
- does it wait for a complete event delimiter?
- can a rendering or state update error prevent the last update from showing?
For server-sent events, a "network chunk" isn't necessarily a full event. Your parser still needs to respect the protocol framing.
A simple diagnostic rule helps: find the first pair of adjacent hops whose timelines stop matching. That's usually where you should focus.
How should you fix and test the failing route
Fix the layer that the evidence points to.
If the logs show an upstream read timeout, give that streaming route enough idle time for the expected pauses. Keep a separate overall request deadline so genuinely stuck operations still release resources.
The "right" idle value depends on the provider, model, route, and expected workload. Copying a large timeout from another service isn't a substitute for measuring your own stream behavior.
Before changing production settings, reproduce the failure with a controlled stream. Build a test fixture that emits two chunks, pauses, then emits the final chunk and the expected completion event. In the test environment, set a short idle limit so you can trigger the timeout quickly.
Disable buffering only when the trace supports it
If upstream events arrive steadily but downstream delivery is delayed, test whether buffering is responsible.
For an NGINX proxy, proxy_buffering off streams the proxied response as it is received. Apply it to the stream route that needs it, then re-measure arrival times at the client.
Buffering isn't automatically "bad." It can improve efficiency for normal responses. Broadly disabling it can change resource use and behavior across unrelated traffic. Scope it to streaming endpoints and verify it fixes the measured gap.
Use heartbeats with a clear understanding of the path
If you control the stream producer, heartbeats can prevent an otherwise quiet connection from going idle.
In server-sent events, a comment like : heartbeat followed by a blank line is ignored by the event parser, but it still sends bytes as long as the producer flushes it.
Just don't assume heartbeats solve everything. A heartbeat only resets the idle timer on the connection it reaches. Your gateway might keep the downstream client connection alive while its upstream read still times out. Confirm which hop owns the relevant idle limit, and verify the heartbeat reaches that hop.
Heartbeats also don't replace overall deadlines. Without another limit, they can keep a broken operation running indefinitely.
Test both failure behavior and the proposed fix
A solid regression test has two runs:
- Use the fixture pause long enough to exceed the test route's idle limit. Assert the client marks the attempt as incomplete and does not report a successful terminal event.
- Apply the proposed fix and run the same fixture. Assert chunks arrive incrementally, and the final chunk plus completion event arrive before the overall deadline.
This checks more than "the socket stayed open." It checks what the user actually gets and whether the timeout distinction is repeatable in CI or a route-level integration test.
How should the client handle an incomplete answer
A dropped stream is both a transport issue and a product-state issue. Even if you can't prevent every intermediary or upstream interruption, you can avoid claiming that an unfinished response is "done."
Model each request attempt explicitly, including states like:
- connecting
- streaming
- completed
- failed
- incomplete
Only move to completed when the protocol's documented terminal signal arrives. If the connection ends first, preserve the partial text and mark the attempt as incomplete. Show enough status for a user (or support engineer) to understand the response stopped early - without pretending you know the root cause when you don't.
Keep retries separate from the original attempt
Give every retry a new attempt ID. Don't automatically append a second generated answer onto the first partial attempt. Retries can repeat earlier content, take a different direction, or use a different model.
If you support "continuation," use explicit continuation semantics from the API (and make the UI behavior match that model). Otherwise, treat the retry as a distinct answer.
This also makes observability easier. Compare the first attempt's last event and close reason with the retry's completion state, instead of merging two timelines into one ambiguous record.
Keep the state machine aligned with the protocol
Your parser should separate transport chunks from protocol events. One chunk might include part of an event or several events. Buffer and parse according to the framing rules in the protocol.
Also handle in-band error events even when HTTP status was 200.
A practical state model can stay simple:
- Streaming: response is open and events are being processed.
- Completed: terminal event received and parsed.
- Incomplete: connection ended before terminal event.
- Failed: explicit error received or client couldn't continue processing.
Use the endpoint's documented semantics to decide whether an explicit error is "failed" vs "incomplete." The point is to avoid treating every close as success just because the response began.
Conclusion
When an AI response stops after HTTP 200, don't start by blaming the model or raising every timeout. Trace the last upstream read, downstream write, client read, parser event, and close reason for a single request.
The "quiet interval" and the first place where adjacent hop timelines stop matching usually narrow the diagnosis quickly.
Then fix the implicated route or component, keep an overall deadline, and test both the failure path and the expected completion path using a controlled stream. Treat the terminal event as the signal that an answer is complete - not the status code and not the socket close. This protects your infrastructure from indefinite requests and keeps your UI honest when the response is only partial.