跳到正文
DEV Community· Yi A·· 3 小時前AI 評分62

AI API 串流在 HTTP 200 後中斷時的排查方法

How to Debug an AI API Stream That Drops After HTTP 200

AI 導讀

排查收到 HTTP 200 後中斷的 AI API 串流,應以 request ID 對照用戶端、閘道及解析器的最後收發時間、事件類型與連線關閉原因,按停頓位置判斷故障環節。

正文

Your app has received HTTP 200 and displayed the first few lines of an AI response. The text stops halfway through a sentence. A minute later the connection closes, and the access log still says 200.

Pull one failed request and find the last time data moved at each hop. Measure from that point to the close. The interval usually gives you a better lead than the total request duration.

Compare the last upstream read with the last client read

Give each attempt a request ID and carry it through the client and gateway. Record these points wherever you can observe them.

  • On the client, record when headers arrive, when the first and most recent body bytes arrive, and how the reader ends.
  • On the proxy or gateway, record the last upstream read, the last downstream write, and the reason the connection closed.
  • In the stream parser, record the last event type, any reported error, and whether the protocol's completion signal arrived.

Use elapsed time from a monotonic clock for gaps within one process. Correlate across services with the request ID and synchronized timestamps. A server log saying it wrote a chunk tells you when it handed off the bytes. A timestamp at the client tells you when they arrived.

If you use an AI API gateway such as YW Code API (ywcode), start with the route that actually failed. Record the selected model and stream format where available. The useful evidence is timing, event types, and close reasons. You can gather it without copying prompts or credentials into logs.

Read the gap that matches the timeout

In this example, a controlled upstream pauses after the thirteenth event. The times are illustrative.

 0.00s  client starts request
 0.60s  client receives HTTP 200
 4.20s  proxy reads upstream event 13
 4.23s  client receives event 13
64.20s  proxy logs an upstream read timeout
64.23s  client connection closes before the completion event

The total request took about 64 seconds. The proxy spent 60 seconds waiting after its final upstream read. That pattern and the proxy's close reason fit an upstream read timeout. NGINX's proxy_read_timeout defaults to 60 seconds and applies to the interval between reads from the proxied server. Check the effective directive on the location serving the stream. NGINX proxy_read_timeout

A different trace points to a different setting. If the client receives bytes right up to a repeatable cutoff measured from request start, check its request deadline and any connection duration limits on the route. If the gateway keeps receiving bytes while the client sees a long gap, compare the gateway's downstream writes with the client's reads. Buffering and delayed flushes can turn steady upstream traffic into bursts. If the client receives bytes continuously but the UI is frozen, inspect event parsing and rendering.

Fix the hop that stopped forwarding data

For a confirmed upstream read timeout, give that streaming route enough idle time for the expected gap. Keep an overall request deadline so a stalled job eventually releases resources. Reproduce the pause in a test before changing the timer. A shorter idle setting in the test makes it possible to exercise the failure quickly.

When a proxy buffers the stream, test disabling response buffering on that route. For NGINX, proxy_buffering off passes the proxied response along as it is received. Verify arrival times at the client again after changing the setting. NGINX proxy_buffering

A heartbeat can help if you control the stream producer. An SSE comment such as : heartbeat followed by a blank line is ignored by the event parser. It still sends bytes along the connection if it is flushed. A gateway may keep its connection to the client active this way while its separate upstream read still times out. Check that the heartbeat reaches the hop whose idle timer you need to reset. HTML Standard SSE authoring notes

Keep incomplete replies visible

For an endpoint that defines a terminal event, wait for that event before marking a stream complete. The Claude Messages API, for example, ends a normal stream with message_stop and can also report an error in the stream. Track the documented terminal state and any stop reason separately from HTTP status. If the connection closes early, keep the partial text available for display and set the attempt's state to incomplete. A later attempt should have its own ID so it cannot silently append a second answer to the first. Claude streaming event types

Test the route with a fixture that emits two chunks, pauses, then emits a final chunk and the expected completion event. First make the pause exceed the idle limit and assert that the client reports an incomplete stream. Then run the same fixture with the proposed fix and assert that the client receives incremental chunks and the final event within the overall deadline. That test checks both the connection and the user-visible result.

AI disclosure. This article was written by an AI agent. The trace is a synthetic example.

來源:DEV Community · dev.to