Skip to content

feat(ai): End a gateway stream that gives no first token #6000

Done

Description

@dan-petty

Problem

LiteLLM has no first-token deadline: OpenRouter keep-alive comments reset each read timeout, so a call that started while a provider stalls waits about 300 s and then fails (#5990 design, comment 6922318562). After #5995 the gateway routes new calls around a stalled deployment, but a call in flight still waits the full stall, and the client resend of #5965 starts only after it fails.

Expected behavior

  • GatewayCallModel (src/devops_cli/ai/models/) ends a stream that gives no first content event within a setting (for example ai.first_token_timeout_seconds, default 60), and sends the call again once, as it does for a stream that fails before its first event.
  • The timeouts still nest: first-token deadline < gateway timeout < ai.request_timeout_seconds.
  • A test with a stalled fake stream proves the deadline and the single resend.
  • This step goes away when LiteLLM gets an upstream first-chunk deadline (see the upstream item).

Related

Alternatives check of 2026-11-24: none.
Tool: none found.
Reason: Searches found no tool with a first-token deadline: LiteLLM and pydantic-ai offer request timeouts and retries, but gateway keep-alive comments reset per-read timeouts and a stalled stream is never resent.

Lacks:

  • No deadline on the wait for the first content event
  • Keep-alive comments reset each per-read timeout instead
  • No resend of a stream that stalls before its first token
  • No check that the deadline sits below the whole-call timeout

Proposed design

Problem statement

LiteLLM has no deadline for the first content event of a stream: the gateway writes an SSE : ping comment every 30 s, also before the first token, and each comment resets the read timeout, so a call that starts while a provider stalls waits about 300 s and then fails. #5995 routes new calls around a stalled deployment, but a call in flight still waits the full stall, and the client resend of #5965 starts only after that failure. This item adds ai.first_token_timeout_seconds (default 60 s) as a deadline on the wait for the first content event, inside the one retry layer of GatewayCallModel, so the call is sent again at most ai.max_retries times; it goes away when LiteLLM gets an upstream first-chunk deadline.

Acceptance criteria

Requirement: First content event deadline

The gateway SHALL end a streamed call that gives no content event within the first-token deadline.

Scenario: Stalled stream ends at the deadline

  • WHEN a fake gateway sends an SSE keep-alive comment every 0.4 s and gives no content event, and a GatewayCallModel holds a first-token deadline of 0.2 s with max_retries 0
  • THEN the run raises FirstTokenTimeout before the fake gateway sends its second keep-alive, and model_call_error of that error reports timed_out

Requirement: One resend with the cluster retry count

The gateway MUST send the call again at most ai.max_retries times when the first-token deadline ends its stream.

Scenario: A stalled first send is resent once

  • WHEN max_retries is 1, the first fake reply sends keep-alive comments and no content event, and the second reply sends a content event
  • THEN the fake gateway records exactly two calls, and the run returns the content of the second reply

Requirement: No deadline after the first content event

The gateway MUST NOT end a stream at the first-token deadline after its first content event.

Scenario: A slow stream keeps its connection after the first token

  • WHEN a fake reply sends one content event and then waits longer than the first-token deadline before its next event
  • THEN the run returns the full content, and the fake gateway records one call

Requirement: First-token deadline below the call deadline

The settings MUST refuse a first-token deadline that is not smaller than ai.request_timeout_seconds.

Scenario: An invalid nesting is refused

  • WHEN AIConfig is built with first_token_timeout_seconds 120 and request_timeout_seconds 90
  • THEN validation raises an error whose message names ai.first_token_timeout_seconds and ai.request_timeout_seconds

Requirement: Default first-token deadline

The gateway SHALL use a first-token deadline of 60 s when ai.first_token_timeout_seconds is absent.

Scenario: Default value

  • WHEN the settings hold no ai.first_token_timeout_seconds
  • THEN settings.ai.first_token_timeout_seconds is 60

Requirement: The gateway router timeout stays above the deadline

The router timeout of the gateway MUST stay above the default first-token deadline.

Scenario: Live gateway timeout

  • WHEN a person runs kubectl -n llm get configmap -l app.kubernetes.io/name=llm-gateway -o yaml on the cluster
  • THEN the router timeout is 900 s, above the 60 s default of ai.first_token_timeout_seconds

Requirement: The generated settings documents list the key

The generated settings documents SHALL list ai.first_token_timeout_seconds with its default and description.

Scenario: Settings documents are fresh

  • WHEN uv run devops docs generate runs after the field is added, and then uv run devops ci docs checks the generated documents
  • THEN uv run devops ci docs passes, and the settings pages hold the row of ai.first_token_timeout_seconds with 60 as its default

Existing Tool Considered

The alternatives check of 2026-11-17 in the issue body (devops-roadmap-alternatives block) found no open, maintained tool or library with a first-content-event deadline. The closest are LiteLLM, the gateway in use, which offers timeout and request_timeout plus retries but no upstream first-chunk deadline, and pydantic-ai, whose timeout setting limits each wait for data and whose request_timeout is one deadline over the whole call (src/devops_cli/config/settings.py:1022-1037); the gateway's : ping every 30 s resets the first, and the second ends the call instead of resending it. The change therefore adds the deadline in project code, as a WrapperModel around each send inside the stream FallbackModel, with one error class and its branch in failed_before_first_event. Written cost of that code: about 45k tokens to build the deadline, the resend and the tests, about 10k tokens each year to keep and fix them, and about 5 hours of elapsed work.

Key questions

  • Where does the first-token deadline live?: In a new FirstTokenDeadlineModel(WrapperModel) around each chat-model send, inside the stream FallbackModel of GatewayCallModel (src/devops_cli/ai/models/gateway.py:84-86), and not around the FallbackModel itself. The FallbackModel waits for the first event of a stream before it gives the stream to its caller (src/devops_cli/ai/models/gateway.py:15-19), so a wrapper outside it sees the stall only after the whole-call deadline. The wrapper inside it yields the first event under anyio.move_on_after(first_token_timeout_seconds) and lets the existing FallbackModel send the call again. (best practice) [sources: src/devops_cli/ai/models/gateway.py:15-19, src/devops_cli/ai/models/gateway.py:84-86, src/devops_cli/ai/models/gateway.py:103-118]
    • Chosen: A wrapper model around each send, inside the stream FallbackModel. It reads the first event of its own send under the deadline, so failed_before_first_event sees the failure and the existing FallbackModel sends the call again.
    • Rejected: A wrapper around self._streams in GatewayCallModel.request_stream. The FallbackModel returns only after the first event (src/devops_cli/ai/models/gateway.py:15-19), so this wrapper cannot time the stall, and a timeout there would need its own resend loop.
    • Rejected: The pydantic-ai timeout model setting. It limits each wait for data, and the gateway's : ping comment every 30 s resets it (k8s/llm/gateway/config.yaml:301-305, src/devops_cli/config/settings.py:1027-1037).
    • Rejected: Wait for an upstream LiteLLM first-chunk deadline. The alternatives block records no such release, and the body names that upstream work as the later removal step, not as this step.
  • How does the first-token deadline reach the single resend layer?: The deadline raises a new FirstTokenTimeout(ModelTimeoutError), and failed_before_first_event (src/devops_cli/ai/models/gateway.py:53-64) gets an explicit branch for it before its ModelTimeoutError exclusion. The existing predicate deliberately does not resend a ModelTimeoutError, and a transport read timeout must stay unresent; the subclass keeps model_call_error's timed_out report (src/devops_cli/roadmap/agent_run.py:67). (best practice) [sources: src/devops_cli/ai/models/gateway.py:53-64, src/devops_cli/roadmap/agent_run.py:67]
    • Chosen: A new FirstTokenTimeout(ModelTimeoutError) and an explicit branch in failed_before_first_event. The branch resends the deadline failure before the predicate's ModelTimeoutError exclusion, and the last send still reports a timeout.
    • Rejected: Raise a plain ModelTimeoutError. failed_before_first_event returns False for it (src/devops_cli/ai/models/gateway.py:60-61), so the stalled call would not be sent again.
    • Rejected: Raise a ModelConnectionError with the phase read. It reports a transport read failure that did not happen.
    • Rejected: Raise UnexpectedModelBehavior. It is resent today, but the caller loses the timeout class and model_call_error sets no timed_out.
  • Does the deadline cover the whole stream or only the wait for the first content event?: Only the wait for the first content event, and only in request_stream. The whole-call request_timeout already covers every send and the read of the stream (src/devops_cli/config/settings.py:1022-1026), and a failure after the first event fails the call once (src/devops_cli/ai/models/gateway.py:8-13, tests/ai/models/test_gateway_call.py:139-157). A non-streamed request keeps the whole-call deadline, and the gateway writes the : ping comment into streamed replies only (k8s/llm/gateway/config.yaml:301-305). (best practice) [sources: src/devops_cli/config/settings.py:1022-1026, src/devops_cli/ai/models/gateway.py:8-13, tests/ai/models/test_gateway_call.py:139-157, k8s/llm/gateway/config.yaml:301-305]
    • Chosen: Deadline up to the first content event. The stall that the issue names happens before the first token, and the whole-call request_timeout already covers the rest of the stream.
    • Rejected: Deadline for the whole stream. It duplicates request_timeout and would cut a long answer with slow tokens, which a failure after the first event must not do (src/devops_cli/ai/models/gateway.py:8-13).
  • What does the gateway do with the stalled stream before the resend?: It closes the stalled stream, then sends the call again. The deadline model holds its send in an AsyncExitStack, so the timeout cancels the read and the context exits the inner stream before FallbackModel makes the next send (src/devops_cli/ai/models/gateway.py:85-86, src/devops_cli/ai/models/gateway.py:103-118); one upstream call is open at a time. (best practice) [sources: src/devops_cli/ai/models/gateway.py:85-86, src/devops_cli/ai/models/gateway.py:103-118]
    • Chosen: Close the stalled stream, then resend. The deadline model's AsyncExitStack exits the timed-out inner stream before FallbackModel starts the next send, so one upstream call is open at a time.
    • Rejected: Resend while the stalled stream stays open. Two upstream calls would hold two slots, and the stalled one would still cost tokens.
  • Which setting key, type, default and nesting check does the first-token deadline use?: ai.first_token_timeout_seconds, an int field with a Variable() marker and ge=1, default 60 s from a DEFAULT_AI_FIRST_TOKEN_TIMEOUT_SECONDS constant, plus one model_validator(mode="after") on AIConfig that refuses a value not below request_timeout_seconds. The field follows ai.request_timeout_seconds (src/devops_cli/config/settings.py:1022-1026, src/devops_cli/config/defaults.py:141), and pydantic_ai_bridge passes it to GatewayCallModel (src/devops_cli/ai/pydantic_ai_bridge.py:216-226). The gateway router timeout is a manifest value (k8s/llm/gateway/config.yaml:358), not a setting, so the settings cannot compare with it; the default 60 s stays below the 900 s router timeout and below the 1200 s default of ai.request_timeout_seconds, and the existing invariant test keeps the gateway order (k8s/llm/gateway/config.yaml:357). (best practice) [sources: src/devops_cli/config/settings.py:1022-1026, src/devops_cli/config/defaults.py:141, src/devops_cli/ai/pydantic_ai_bridge.py:216-226, k8s/llm/gateway/config.yaml:358, k8s/llm/gateway/config.yaml:357]
    • Chosen: ai.first_token_timeout_seconds, default 60 s, with a validator below ai.request_timeout_seconds. It follows the ai.request_timeout_seconds field and DEFAULT_AI_* constant pattern, the body's example, and keeps the one relation that the settings can see.
    • Rejected: A required setting with no default. Every existing deployment would fail at load until a person sets it.
    • Rejected: A value computed from ai.request_timeout_seconds. It hides the deadline from the settings, and a person cannot tune it for a slow provider.
    • Rejected: A validator that also compares with the gateway router timeout. The router timeout is a manifest value (k8s/llm/gateway/config.yaml:358), not a setting, so the settings cannot read it; the existing invariant test keeps that order.
  • How many times does the gateway send the call again after the deadline?: At most ai.max_retries times: the stream FallbackModel holds each send ai.max_retries + 1 times (src/devops_cli/ai/models/gateway.py:84-86), and the cluster sets ai.max_retries 1 (tests/ai/models/test_gateway_call.py:171-180), so a stalled first send is sent exactly twice. This keeps the owner decision that the client has one retry layer and sends a call again only when it failed before the first stream event (docs/SERVICE_PATTERNS.md:97); the deadline joins that predicate instead of adding a second loop. [sources: src/devops_cli/ai/models/gateway.py:84-86, src/devops_cli/config/settings.py:1017-1021, tests/ai/models/test_gateway_call.py:171-180, docs/SERVICE_PATTERNS.md:97]
  • Which tests prove the deadline and the resend?: tests/ai/models/test_gateway_call.py, which answers each call at the httpx2 transport interface, so the real pydantic-ai OpenAI model and the OpenAI SDK read the fake SSE body (tests/ai/models/test_gateway_call.py:40-50). It already holds the KEEPALIVE = b": ping\n\n" frame (tests/ai/models/test_gateway_call.py:33), the fake gateway call counter (tests/ai/models/test_gateway_call.py:73-85), the resend tests (tests/ai/models/test_gateway_call.py:103-122) and a TestModel fake that stalls its stream (tests/ai/models/test_gateway_call.py:209-233). The new tests add a stalled SSE reply with keep-alives and a second reply with content. [sources: tests/ai/models/test_gateway_call.py:33, tests/ai/models/test_gateway_call.py:40-50, tests/ai/models/test_gateway_call.py:73-85, tests/ai/models/test_gateway_call.py:103-122, tests/ai/models/test_gateway_call.py:209-233]
  • What happens to the generated settings documents?: docs/CONFIGURATION.md and docs/ENV_VARS.md are generated (docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:18), uv run devops docs generate writes them after a setting change (docs/ROADMAP_WORKFLOW.md:336), and the field's description becomes the new rows beside ai.request_timeout_seconds (docs/CONFIGURATION.md:253, docs/ENV_VARS.md:26). uv run devops ci docs fails a stale document. [sources: docs/CONFIGURATION.md:253, docs/ENV_VARS.md:26, docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:18, docs/ROADMAP_WORKFLOW.md:336]
  • Does this item wait on feat(ai): Route DeepSeek calls around a stalled provider #5995 or fix(ai): one retry layer for a gateway call #5965?: No. feat(ai): Route DeepSeek calls around a stalled provider #5995 is merged (CHANGELOG.md:284), and the fix(ai): one retry layer for a gateway call #5965 retry layer is in the repository (src/devops_cli/ai/models/gateway.py:78-118), so the deadline extends code that is present and needs no other issue first. [sources: CHANGELOG.md:284, src/devops_cli/ai/models/gateway.py:78-118]
  • What counts as the first content event?: The first pydantic-ai stream event that holds content. An SSE keep-alive comment is not an event (k8s/llm/gateway/config.yaml:301-305), and a role-only chunk does not end the resend window today (tests/ai/models/test_gateway_call.py:125-136), so neither stops the deadline; only the first content event does. A stream that ends with no content is the UnexpectedModelBehavior case that failed_before_first_event already resends (src/devops_cli/ai/models/gateway.py:53-64). [sources: tests/ai/models/test_gateway_call.py:125-136, k8s/llm/gateway/config.yaml:301-305, src/devops_cli/ai/models/gateway.py:53-64]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/feature
    scope/ai
    source/agent
    status/new
    value/medium
    on Nov 16, 2026
  2. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor
  3. added
    effort/medium
    priority/p2-medium
    on Nov 16, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • LLM gateway first chunk timeout stream idle timeout setting, results: 5
    • LiteLLM first token timeout stream deadline, results: 5
    • litellm docs timeout stream_timeout request_timeout, results: 5
    • "first token" timeout AI gateway "time to first token" Envoy AI Gateway, results: 5
    • litellm proxy request_timeout stream_timeout config yaml, results: 5
    • litellm github issue first chunk timeout streaming provider stall, results: 5
    • litellm, results: 5
    • OpenRouter keep-alive comments reset read timeout stream stall, results: 5
    • litellm documentation timeouts streaming, results: 5
    • BerriAI litellm GitHub, results: 5

    Sources:

  5. added
    status/ready
    and removed
    status/new
    on Nov 18, 2026
  6. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at ff42fbe (see proposed design in issue body).

  7. added
    owner/agent
    on Nov 25, 2026
  8. added
    status/in-review
    and removed
    status/ready
    on Nov 25, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 25, 2026
  10. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-25 17:24 UTC

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

    effort/mediumpriority/p2-mediumscope/aisource/agenttype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions