Skip to content

fix(ai): chat calls stream their reply, and the gateway keeps the stream open past the 125 s edge timeout - #2379

Merged
dan-petty merged 3 commits into
mainfrom
fix/2365-gateway-stream-keepalive
Oct 27, 2026
Merged

dan-petty merged 3 commits into
mainfrom
fix/2365-gateway-stream-keepalive

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

A workstation reaches the gateway through the Cloudflare tunnel, and the edge cuts a request that has no data for 125 s. The client sent each OpenAI-compatible chat call without streaming, so a reply that took longer than 125 s failed with 524, the gateway counted a 499, and each retry was cut again.

  • The gateway config sets litellm_settings.sse_keepalive_ping_interval_seconds: 30. LiteLLM writes ": ping" SSE comments into a streamed reply that has no data, also before the first token.
  • Every OpenAI-compatible chat call that returns one reply sends stream: true with stream_options.include_usage, and joins the chunks with ChatCompletionStreamState of the OpenAI SDK. The blocking read is deleted.
  • The response size limit applies to the decoded data of the stream. A cut stream fails as the streamed path fails.
  • An error frame after a ping gives the error of the HTTP status that it names, and the spend ledger records it as a failed attempt (record_failed_attempt).
  • The read timeout limits each wait for the next data of a reply. The settings description, docs/CONFIGURATION.md, k8s/README.md and the gateway config comments say so.
  • The shared test fakes stream a canned chat completion to a request with stream: true, as a server does, and the canned reply of a review dry run is a stream.

Closes #2365

…eam open past the 125 s edge timeout

A workstation reaches the gateway through the Cloudflare tunnel, and the edge cuts a request that has no data for 125 s. The client sent each OpenAI-compatible chat call without streaming, so a reply that took longer than 125 s failed with 524, the gateway counted a 499, and each retry was cut again.

- The gateway config sets litellm_settings.sse_keepalive_ping_interval_seconds: 30. LiteLLM writes ": ping" SSE comments into a streamed reply that has no data, also before the first token.
- Every OpenAI-compatible chat call that returns one reply sends stream: true with stream_options.include_usage, and joins the chunks with ChatCompletionStreamState of the OpenAI SDK. The blocking read is deleted.
- The response size limit applies to the decoded data of the stream. A cut stream fails as the streamed path fails.
- An error frame after a ping gives the error of the HTTP status that it names, and the spend ledger records it as a failed attempt (record_failed_attempt).
- The read timeout limits each wait for the next data of a reply. The settings description, docs/CONFIGURATION.md, k8s/README.md and the gateway config comments say so.
- The shared test fakes stream a canned chat completion to a request with stream: true, as a server does, and the canned reply of a review dry run is a stream.

Closes #2365
@dan-petty

Copy link
Copy Markdown
Owner Author

Checks run on the branch before the push (2026-10-27):

  • Fail-to-pass, uv run devops ci fail-to-pass --base origin/main: 7 failed on base and pass on the branch: tests/invariants/test_k8s_manifests.py::test_the_gateway_keeps_a_streamed_chat_reply_open_under_the_edge_timeout and the 6 tests of tests/test_ai_chat_reply_stream.py.
  • Full suite, uv run devops ci test: 11,300 passed.
  • devops ci typecheck, lint, format, docs and devops openspec check: passed.

Evidence for the design:

  • A streamed qwen3.8:27b reply through the tunnel ran 200 s with no gap longer than 1.7 s, so bytes keep the edge from cutting the request.
  • ChatCompletionStreamState adds about 0.2 ms for each chunk (20,000 chunks in 4 s), well under the 20 to 30 ms between tokens.

After the merge, when Argo CD has rolled the gateway onto the new config:

  • A live call through the tunnel whose first token comes after more than 125 s returns its reply.
  • For 24 h, sum(increase(litellm_deployment_failure_responses_total{exception_status="499"}[24h])) counts no 499 that a client did not cancel itself.

@dan-petty
dan-petty force-pushed the fix/2365-gateway-stream-keepalive branch from 0a1b042 to 4e8cdba Compare October 27, 2026 19:56
@dan-petty
dan-petty marked this pull request as ready for review October 27, 2026 20:03
@dan-petty
dan-petty merged commit 0abf552 into main Oct 27, 2026
16 checks passed
@dan-petty
dan-petty deleted the fix/2365-gateway-stream-keepalive branch October 27, 2026 20:03
@dan-petty

Copy link
Copy Markdown
Owner Author

The first run failed the changed-line coverage step (89 %). The new commit covers the lines: an empty stream, and the failed-attempt branches that record_failed_attempt took over from the transport (an error body with usage and a model, an HTML body, a body cut by the network). _frame_status and _watch_frames are simpler. diff-cover gives 100 % on the changed lines, and every check passes.

@dan-petty

Copy link
Copy Markdown
Owner Author

After the merge, Argo CD rolled the gateway onto the new config at 20:05 UTC (2026-10-27). Checks through the Cloudflare tunnel:

  • Two streamed calls to devops-background, whose tier serves one request at a time. Call A generated for 132 s. Call B waited in the queue behind it: the gateway sent its status line with the first : ping at 30.7 s, sent 4 pings in all, and the first token came at 130.5 s. B finished with [DONE]. Before this change, the edge cut such a call at 125 s.
  • In the 15 min around the rollout, the gateway counted 3 499s, all on the old gateway pod and none on the new one. A client that still runs a build before this change sends calls without streaming, so it still gets the 125 s cut until it updates.

dan-petty pushed a commit that referenced this pull request Oct 27, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.7.4](v0.7.3...v0.7.4)
(2026-10-27)


### Bug Fixes

* **ai:** chat calls stream their reply, and the gateway keeps the
stream open past the 125 s edge timeout
([#2379](#2379))
([0abf552](0abf552)),
closes [#2365](#2365)
* **ai:** the devops ai failover help says that the gateway failover
records a route, not that it reroutes requests
([#2376](#2376))
([6560b93](6560b93)),
closes [#2079](#2079)
* **docs:** add home page, remove raw markdown publishing, and check
links ([#2070](#2070))
([#2372](#2372))
([7f9321c](7f9321c))
* **llm:** pass num_ctx with direct ollama embedding requests
([#2378](#2378))
([ff47504](ff47504)),
closes [#2089](#2089)
* **roadmap:** intake's reply schema limits each fixed field to its
values, so the model cannot leave type empty
([#2380](#2380))
([b9220b1](b9220b1)),
closes [#2370](#2370)
* **telemetry:** a gateway inference span names the deployment model
that served the call as gen_ai.response.model
([#2381](#2381))
([78dbd1d](78dbd1d))


### Performance Improvements

* **ci:** lower the coverage floor to 85% and fail a pull request below
90% on its changed lines
([#2375](#2375))
([9f97c14](9f97c14))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ai): gateway chat calls stream with keepalive pings, so the Cloudflare 125 s read timeout does not cut a long reply

1 participant