Skip to content

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

Description

@dan-petty

Problem Description

A devops command that calls the LLM gateway through the Cloudflare Tunnel (https://ai.example.com/v1) fails when the model works for more than 125 s before the reply is complete. The Cloudflare Proxy Read Timeout is 125 s. Only an Enterprise zone can change it.

The client sends each gateway chat call without streaming (_openai_compat_messages in src/devops_cli/ai/client/openai.py). Thus no byte comes back until the full reply is ready. At 125 s, the edge returns 524 to the client and closes the request to the origin. LiteLLM then records a 499, and cancel_on_disconnect stops the Ollama task. That setting is correct: it releases the GPU slot.

The retry transport retries every 5xx status, 524 included (src/devops_cli/ai/retries/__init__.py). Each retry sends the same long prompt to a different tier, and the edge cuts it again at 125 s. One call can use 5 × 125 s of GPU time and return no reply.

Reproduction Steps

  1. Use the default configuration: ai.provider: gateway, ai.gateway_url: https://ai.example.com/v1, ai.model: qwen3.8:27b.
  2. Send a chat call that makes the model work for more than 125 s, for example an analysis call with a prompt of about 90k tokens.
  3. Each attempt fails with 524 after about 125 s. The gateway counts a 499 for each attempt.

Expected vs Actual Behavior

  • Expected: the call returns the full reply while the client's own read timeout (1,200 s for a review) is not reached.
  • Actual: each attempt stops at about 125 s, and the retries stop at the same point.

Environment Diagnostics

devops-cli 0.7.1 (main at 69928b0). Gateway: LiteLLM v1.103.0. Tunnel: cloudflared 2026.10.4. Ingress: Traefik on k3s. Client: openai 3.26.1.

Logs, Traces, or Terminal Output

Measured on 2026-10-27:

Feasibility

  • A stream stays open past 125 s through the tunnel. A probe on 2026-10-27 streamed a qwen3.8:27b reply through ai.example.com for 200 s. The first byte came at 1.7 s, and no gap was longer than 1.7 s. The probe closed the connection itself at 200 s. Thus bytes on the wire stop the edge timeout, and no hop (Cloudflare, cloudflared, Traefik) buffers the stream.
  • LiteLLM can send bytes before the first token. The deployed LiteLLM v1.103.0 has litellm_settings.sse_keepalive_ping_interval_seconds (litellm/__init__.py:263). When it is set and the request has stream: true, open_sse_before_first_byte (litellm/proxy/common_request_processing.py) opens the response after one interval. It then writes : ping SSE comments until the first token comes, and in each later gap. It does nothing for a request without stream: true (ttft_keepalive_interval). A deployment can turn it off with keepalive_seconds: 0.
  • The client ignores the pings. openai 3.26.1 skips SSE comment lines (openai/_streaming.py:404). It raises openai.APIError on a data: frame that holds error (openai/_streaming.py:94).

Proposed fix

Use open tools: the LiteLLM keepalive and the OpenAI SDK stream. Build nothing new.

  1. In k8s/llm/gateway/config.yaml, set litellm_settings.sse_keepalive_ping_interval_seconds to a value well below 125 s. Start with 30 s: a call whose first token comes in less than 30 s keeps its x-litellm-* headers (see "Costs to decide").
  2. In src/devops_cli/ai/client/openai.py, make _openai_compat_messages send stream: true with stream_options.include_usage for the gateway provider. Build the same LLMResponse from the chunks: content, reasoning, usage, finish reason and model. Use the stream reader that _openai_compat_stream uses now. Keep the DEFAULT_AI_MAX_RESPONSE_BYTES limit on the streamed body.
  3. Map an openai.APIError from an error frame to the same error that an HTTP error status gives. The except clause catches only APIStatusError and APIConnectionError now. After the first ping, the status line is 200, so a later failure comes only as an error frame.

Costs to decide

  • After a ping, LiteLLM cannot send its x-litellm-* headers. Then extract_served_by and extract_served_model (src/devops_cli/ai/client/network.py) return None, and fix(ai): a hosted gateway call is priced at the deployment model that the gateway names, not at the route name #2080 prices a hosted call from that header. Decide how to keep the served model. One option is to turn off the keepalive on hosted deployments (keepalive_seconds: 0), because they reply fast. Record the decision in the change's design.
  • Open WebUI streams through the same gateway, so it gets the pings too. Make sure that it ignores SSE comments.
  • Out of scope: the ollama provider through ollama.example.com has the same 125 s limit, and Ollama sends no keepalive. No task in the default configuration uses it.

Options considered and not taken

  • WebSocket (Realtime API, or the WebSocket mode of the Responses API). The backends are Ollama (ollama_chat), which has no realtime API, and the client uses Chat Completions. It is a large client change for the result that the SSE keepalive gives.
  • Turn off proxy buffering in the ingress. The ingress is Traefik, not nginx, and the gateway route has no buffering middleware. The probe shows no buffering on the path.
  • Increase the Proxy Read Timeout. Only an Enterprise zone can do this.
  • A DNS-only host or a LAN path to the gateway. This shows the origin and goes around the tunnel and Access. The cluster accepts external traffic only through the tunnel (k8s/ingress/traefik-values.yaml).
  • Status polling with an asynchronous job API. Neither LiteLLM nor Ollama has one for chat. The build cost is high.
  • Cluster Service DNS. In-cluster callers use it already. The 499 traffic comes through cloudflared, from workstation CLIs that cannot use cluster DNS.
  • Stop the retry of a 524. This treats the symptom. After the fix, a slow model cannot cause a 524 on a gateway chat call, so the retry policy can stay as it is.

Acceptance criteria

  • A fail-to-pass test shows that a gateway chat call reads a reply whose stream starts with : ping comments. The call returns the same LLMResponse fields as a call without streaming.
  • A test shows that an error frame after a ping gives the same error as the HTTP status.
  • The response size limit applies to a streamed reply.
  • A live call through the tunnel whose first token comes after more than 125 s returns its reply. The pull request comment records the timing.
  • For 24 h after the rollout, the gateway counts no 499 on /v1/chat/completions that a client did not cancel itself. The pull request comment records the Prometheus result.

Related

Activity

  1. added
    source/agent
    type/bug
    on Oct 27, 2026
  2. dan-petty commented on Oct 27, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/bug. The title prefix is fix and the body describes a defect where long replies are cut at 125 s.
    • Priority: P1-High. 73 failed calls in 24 h waste GPU time and block long analysis prompts for all users.
    • Value: High. It unblocks the core long-reply use case and stops 5 x 125 s of wasted GPU per call.
    • Effort: Medium. The fix enables an existing LiteLLM setting and switches the client to streaming, but the streaming path needs testing.
    • Related: feat(monitoring): the ai-spend dashboard counts gateway traffic from the gateway's own series, shows failed attempts and names each caller #1668, which asks for a different change. Both deal with gateway call failures, but #1668 adds dashboard visibility while this item fixes the timeout by enabling streaming.
  3. dan-petty commented on Oct 27, 2026

    @dan-petty
    OwnerAuthor

    More evidence, from the spend ledger of one workstation (.data/ai/spend.db) on 2026-10-27:

    • From 18:20 to 18:40 UTC, 7 analysis calls to qwen3.8:27b through the tunnel failed after 125.1 to 125.8 s with finish_reason: error. devops roadmap intake uses the analysis task. Two intake runs in that time had failed calls of this type, so intake is one of the callers.
    • The analysis calls that passed in the same runs had prompts of about 4,000 tokens and took 50 to 79 s. They made 1,155 to 3,923 completion tokens, and 75 to 88 % of these were reasoning tokens (reasoning_effort: high).

    Thus a large prompt is not necessary for the cut. A small prompt also goes past 125 s when the model reasons for a long time. Any devops roadmap intake run can show the failure, so it is a simpler reproduction than step 2 in the body.

  4. added 2 commits that reference this issue on Oct 27, 2026
    0abf552
    520e8b8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions