Repository navigation
Description
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 exampleai.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
- feat(ai): Route around a stalled or offline model provider #5990 (step 3), fix(ai): one retry layer for a gateway call #5965, feat(ai): Route DeepSeek calls around a stalled provider #5995.
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
GatewayCallModelholds a first-token deadline of 0.2 s withmax_retries0 - THEN the run raises
FirstTokenTimeoutbefore the fake gateway sends its second keep-alive, andmodel_call_errorof that error reportstimed_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_retriesis 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
AIConfigis built withfirst_token_timeout_seconds120 andrequest_timeout_seconds90 - THEN validation raises an error whose message names
ai.first_token_timeout_secondsandai.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_secondsis 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 yamlon the cluster - THEN the router
timeoutis 900 s, above the 60 s default ofai.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 generateruns after the field is added, and thenuv run devops ci docschecks the generated documents - THEN
uv run devops ci docspasses, and the settings pages hold the row ofai.first_token_timeout_secondswith 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 streamFallbackModelofGatewayCallModel(src/devops_cli/ai/models/gateway.py:84-86), and not around theFallbackModelitself. TheFallbackModelwaits 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 underanyio.move_on_after(first_token_timeout_seconds)and lets the existingFallbackModelsend 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_eventsees the failure and the existingFallbackModelsends the call again. - Rejected: A wrapper around
self._streamsinGatewayCallModel.request_stream. TheFallbackModelreturns 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
timeoutmodel setting. It limits each wait for data, and the gateway's: pingcomment 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.
- Chosen: A wrapper model around each send, inside the stream FallbackModel. It reads the first event of its own send under the deadline, so
- How does the first-token deadline reach the single resend layer?: The deadline raises a new
FirstTokenTimeout(ModelTimeoutError), andfailed_before_first_event(src/devops_cli/ai/models/gateway.py:53-64) gets an explicit branch for it before itsModelTimeoutErrorexclusion. The existing predicate deliberately does not resend aModelTimeoutError, and a transport read timeout must stay unresent; the subclass keepsmodel_call_error'stimed_outreport (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 infailed_before_first_event. The branch resends the deadline failure before the predicate'sModelTimeoutErrorexclusion, and the last send still reports a timeout. - Rejected: Raise a plain
ModelTimeoutError.failed_before_first_eventreturns False for it (src/devops_cli/ai/models/gateway.py:60-61), so the stalled call would not be sent again. - Rejected: Raise a
ModelConnectionErrorwith the phaseread. It reports a transport read failure that did not happen. - Rejected: Raise
UnexpectedModelBehavior. It is resent today, but the caller loses the timeout class andmodel_call_errorsets notimed_out.
- Chosen: A new
- 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-callrequest_timeoutalready 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-streamedrequestkeeps the whole-call deadline, and the gateway writes the: pingcomment 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_timeoutalready covers the rest of the stream. - Rejected: Deadline for the whole stream. It duplicates
request_timeoutand 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).
- Chosen: Deadline up to the first content event. The stall that the issue names happens before the first token, and the whole-call
- 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 beforeFallbackModelmakes 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
AsyncExitStackexits the timed-out inner stream beforeFallbackModelstarts 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.
- Chosen: Close the stalled stream, then resend. The deadline model's
- Which setting key, type, default and nesting check does the first-token deadline use?:
ai.first_token_timeout_seconds, anintfield with aVariable()marker andge=1, default 60 s from aDEFAULT_AI_FIRST_TOKEN_TIMEOUT_SECONDSconstant, plus onemodel_validator(mode="after")onAIConfigthat refuses a value not belowrequest_timeout_seconds. The field followsai.request_timeout_seconds(src/devops_cli/config/settings.py:1022-1026,src/devops_cli/config/defaults.py:141), andpydantic_ai_bridgepasses it toGatewayCallModel(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 ofai.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 belowai.request_timeout_seconds. It follows theai.request_timeout_secondsfield andDEFAULT_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.
- Chosen:
- How many times does the gateway send the call again after the deadline?: At most
ai.max_retriestimes: the streamFallbackModelholds each sendai.max_retries+ 1 times (src/devops_cli/ai/models/gateway.py:84-86), and the cluster setsai.max_retries1 (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 thehttpx2transport 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 theKEEPALIVE = 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 aTestModelfake 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.mdanddocs/ENV_VARS.mdare generated (docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:18),uv run devops docs generatewrites them after a setting change (docs/ROADMAP_WORKFLOW.md:336), and the field's description becomes the new rows besideai.request_timeout_seconds(docs/CONFIGURATION.md:253,docs/ENV_VARS.md:26).uv run devops ci docsfails 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 theUnexpectedModelBehaviorcase thatfailed_before_first_eventalready 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
dan-petty commentedon Nov 16, 2026
Both address a stalled provider, but #5990 routes new calls around the stall while this candidate ends an in-flight stream that gives no first token.devops-cli-service commentedon Nov 17, 2026
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:
Sources:
devops-cli-service commentedon Nov 18, 2026
Status set to Ready at ff42fbe (see proposed design in issue body).
feat(ai): enforce gateway first-token timeout (#6000)
chore(main): release 0.220.0 (#7199)
devops-cli-service commentedon Nov 25, 2026
Released in v0.220.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.220.0
devops-cli-service commentedon Nov 25, 2026
Live since 2026-11-25 17:24 UTC