Skip to content

fix(ai): a short connect failure to the gateway stops intake #4753

Closed

Description

@dan-petty

Problem Description

On 2026-11-11, between 17:14Z and 17:25Z, one devops roadmap intake --title ... --confirm run from the devcontainer stopped after 38 s with this error, and wrote nothing:

Could not plan intake: The model gateway <gateway URL> did not answer (AIClientError: Cannot connect to AI provider API. Check network access, api_base_url, and API key.), so intake wrote nothing.

The same command 1 min later worked. Other intake runs in the same hour worked. The llm-gateway pod had 0 restarts and ran for 11 h at that time, so the failure was on the path from the workstation to the gateway (the tunnel host behind Access, or name resolution), not in the gateway pod.

The HTTP transport is the single retry layer of the AI client (src/devops_cli/ai/client/unified.py). Its retries did not cover this failure.

Reproduction Steps

  1. Run devops roadmap intake --title <title> --body-file <file> --confirm from the devcontainer while many agent sessions call the gateway.
  2. Watch for a run that stops with "Cannot connect to AI provider API".

The failure is intermittent. It occurred 1 time in about 12 intake runs.

Expected vs Actual Behavior

Expected: a short connect failure on the path is retried with backoff inside the outer timeout, or the error names the cause (DNS, TCP connect, TLS or an Access response).

Actual: the run stops after 38 s with a generic message, and the person or agent runs the command again by hand.

Environment Diagnostics

devops-cli at origin/main 818ac6f (v0.112.1), devcontainer on rootless Podman, the AI client sends requests to the gateway host behind Cloudflare Access.

Logs, Traces, or Terminal Output

Read the cloudflared and llm-gateway logs in Loki for 17:14Z to 17:25Z on 2026-11-11 to find the cause. The exception class under AIClientError (for example httpx.ConnectError or httpx.ConnectTimeout) is not in the terminal output.

Alternatives check of 2026-11-14: in-use.
The repository already uses this tool.
Tool: tenacity (https://pypi.org/project/tenacity), Apache-2.0, last activity 2026-10-07, maintained.

Proposed design

Problem statement

A short connect failure on the path from the workstation to the model gateway stops a roadmap command, and the run writes nothing. The retry exists, but it covers only the request and its headers: the transport makes ai.max_retries + 1 attempts (five by default) with waits of 1, 2, 4 and 8 s and a 5 s connect timeout, so the 38 s run in the issue had already used every attempt while the outer read timeout is 3600 s. Every gateway call reads its reply as a stream, and a reset while that reply is read happens after handle_request returns, so it is not retried at all. The error that ends the run names no cause; it says only that the provider could not be reached. The GitHub listing path and the pydantic-ai agent path are separate and are deferred.

Acceptance criteria

Requirement: Connect failure retried until the deadline

The AI client's HTTP transport SHALL send a request again after a connect error, with jittered exponential backoff, until the retry deadline, and SHALL allow up to 10 attempts for a connect error whatever ai.max_retries is.

Scenario: Six connect failures then a reply

  • WHEN The first six attempts raise httpx2.ConnectError and the seventh returns a reply
  • THEN The call returns the seventh reply, the transport records seven requests, and no AIClientError is raised.

Requirement: Reset before the first chunk retried

The AI client SHALL send a request again when the reply resets before the first chunk, and MUST NOT replay a reply that has yielded a chunk.

Scenario: Reset while reading the reply

  • WHEN The gateway answers 200, the reply body raises httpx2.ReadError before the first chunk, and the second attempt answers a complete reply
  • THEN The call returns the second reply, the client sends two requests, and no AIClientError is raised.

Requirement: No replay after output

A stream that yielded at least one chunk MUST NOT be replayed when the connection resets.

Scenario: Reset mid-stream

  • WHEN The stream yields one chunk and the connection then resets
  • THEN The stream raises AIClientError, the spend row carries finish reason error, and the client sends no second request.

Requirement: Bounded by the outer read timeout

The retries SHALL stop before the outer read timeout elapses and SHALL raise AIClientError.

Scenario: Every attempt fails

  • WHEN Each attempt fails with httpx2.ConnectTimeout after the 5 s connect timeout and the read timeout is 60 s
  • THEN No attempt starts when its connect timeout would pass 60 s, no more than 10 attempts are made, and the call raises AIClientError within 60 s.

Requirement: Error names the cause

The AIClientError of a failed connect SHALL name the class of the error, the class of the first error of its cause chain and the backend host, and MUST NOT contain the API key.

Scenario: DNS failure

  • WHEN The transport raises httpx2.ConnectError from httpcore2.ConnectError from socket.gaierror, and the retries end
  • THEN The message contains ConnectError, gaierror and the host, and contains no API key.

Requirement: One retry layer

The AI client MUST NOT add a second retry loop: one request SHALL be sent at most the number of times of the transport's policy, and the dispatch SHALL re-raise its AIClientError unchanged.

Scenario: Every attempt fails with a connect error

  • WHEN The policy makes its last attempt and the transport raises httpx2.ConnectError
  • THEN The client sends exactly the policy's number of requests and raises one AIClientError.

Existing Tool Considered

tenacity (https://pypi.org/project/tenacity, Apache-2.0, maintained, last activity 2026-10-07) is the open retry library that the repository already uses: pydantic_ai.retries.HTTPX2TenacityTransport is built with tenacity stop, wait and retry strategies in src/devops_cli/ai/retries/__init__.py, and the project's own ReadRetryPolicy runs on tenacity.Retrying in src/devops_cli/http/retry.py. The change adds a jittered wait and a time-bound stop to the existing config and puts the pre-output replay in the AI client; it writes no retry library of its own. Tenacity lacks three things: it does not name the cause of a failure (DNS, TCP connect, TLS or an Access response), so the client's _connection_error adds the class, the first cause and the host; it is not an HTTP transport, so each caller wires it; and it cannot tell whether a reply has begun, so the "no replay after output" condition lives in the caller.

Key questions

  • Which failures does the transport retry today, and which does it miss?: The retry transport retries every httpx2.TransportError and httpx2.TimeoutException, so ConnectError and ConnectTimeout are already retried ai.max_retries + 1 times (five by default) with waits of 1, 2, 4 and 8 s. It misses a reset while the reply is read: handle_request returns after the headers, and every gateway call reads its reply as a stream, so the body read is outside the transport. [sources: src/devops_cli/ai/retries/init.py:39, src/devops_cli/ai/client/base.py:57, src/devops_cli/ai/client/base.py:113, src/devops_cli/config/defaults.py:128, src/devops_cli/config/defaults.py:367, src/devops_cli/ai/client/openai.py:283, src/devops_cli/ai/client/unified.py:354]
  • Where does the retry of a reset before the first chunk belong?: In LLMClient._dispatch_messages and _dispatch_stream, around the request and its read. _dispatch_messages returns only a complete reply, so it can replay; _dispatch_stream replays only until it yields its first chunk. Not in the transport, which returns before the body is read and cannot know whether output began; not in _retry_chat_dispatch, which re-raises AIClientError on purpose and would add a second loop over the same failures. (best practice) [sources: src/devops_cli/ai/client/unified.py:362, src/devops_cli/ai/client/unified.py:890, src/devops_cli/ai/client/unified.py:736, src/devops_cli/ai/client/openai.py:283]
    • Chosen: The AI client's dispatch (_dispatch_messages and _dispatch_stream). It owns the read of the reply and knows whether a chunk has been yielded, so it can replay before output and stop after it.
    • Rejected: The HTTP transport. httpx2.BaseTransport.handle_request returns after the headers; the body read is outside it, and it cannot tell whether a stream yielded a chunk.
    • Rejected: _retry_chat_dispatch. It re-raises AIClientError on purpose and cannot tell whether a stream yielded, so a loop there doubles the validation retry.
  • Which library retries the call?: tenacity, already a dependency: the transport is pydantic_ai.retries.HTTPX2TenacityTransport with tenacity stop, wait and retry strategies, and the project's ReadRetryPolicy runs on tenacity.Retrying. The change extends the existing config with a jittered wait and a time-bound stop and adds the pre-output replay in the client; it builds no retry loop of its own. (best practice) [sources: src/devops_cli/ai/retries/init.py:9, src/devops_cli/http/retry.py:27, docs/adr/0007-tests-double-only-process-edges.md:17]
    • Chosen: tenacity. It is open, maintained and already a dependency, and it gives a stop condition, a jittered exponential wait and a predicate over exception types.
    • Rejected: httpx2.HTTPTransport(retries=). It retries connect errors only, adds no backoff, and cannot cover a reset while the client reads the reply.
    • Rejected: A hand-written retry loop. It rebuilds the backoff, the jitter and the stop condition; the project already owns one such loop in http/retry.py and should not add a second.
  • What is the retry budget?: A connect error is retried until the client's read timeout minus one connect timeout, with waits of 1, 2, 4, 8, 16, 32 and then 60 s with jitter, at most 10 attempts, because a connect attempt sends nothing and bills nothing. A reply reset before the first chunk and a status or timeout keep ai.max_retries + 1 attempts, because a request that reached the gateway may be billed. The 5 s connect timeout stays. The 38 s run had already used all five attempts, so the attempt count, not the predicate, was the gap. (best practice) [sources: src/devops_cli/ai/client/base.py:57, src/devops_cli/config/defaults.py:128, src/devops_cli/config/defaults.py:367, README.md:137, src/devops_cli/ai/retries/init.py:29, src/devops_cli/http/retry.py:75, src/devops_cli/ai/spend/transport.py:118]
    • Chosen: Connect errors retried to a deadline, at most 10 attempts. The observed run used five attempts in 38 s while the outer read timeout is 3600 s, and the same command passed a minute later; a connect attempt writes and bills nothing.
    • Rejected: Keep five attempts. That is what ran in the 38 s failure, so it leaves the issue's case unfixed.
    • Rejected: Three attempts with a 0.5 s first wait. It is weaker than today's five attempts and cannot ride out the failures that were reported.
    • Rejected: Retry for the whole read timeout. An hour-long wait stops an interactive command, and the error text names the cause when the retries end.
  • Does the GitHub listing path join this change?: No. ADR 0006 records that the session does not retry a refused connect, a connect or read timeout, and github-session-retry and github-session-timeouts state the same. Retrying the listing changes those records, so it goes to its own issue, which amends the ADR and the two specs. (best practice) [sources: docs/adr/0006-github-session-requests-go-through-gidgethub.md:17, openspec/specs/github-session-retry/spec.md:40, openspec/specs/github-session-timeouts/spec.md:9]
    • Chosen: A separate item. It needs the ADR 0006 retry rule and two specs changed, which is a decision and a change of its own.
    • Rejected: The same change. It would silently overturn a recorded decision and two specs in a pull request about the AI client.
  • How does the raised error name the cause?: _connection_error names the class of the error, the class of the first error of its cause chain and backend_host, masked with mask_secrets, as model_call_error already does for an agent run. A failed name lookup then reads ConnectError from gaierror, a TLS failure reads SSLError, and the host is the one the client dialled, which carries no userinfo. (best practice) [sources: src/devops_cli/ai/client/unified.py:235, src/devops_cli/ai/client/unified.py:289, src/devops_cli/ai/client/base.py:130, src/devops_cli/roadmap/agent_run.py:71]
    • Chosen: Class, first cause and host. It answers the expected behavior with data the exception already holds, and the agent path already names the class and the first cause (fix(ai): the verify eval agent run fails with ModelConnectionError #4345).
    • Rejected: A fixed text for each stage. httpx2 does not always say whether DNS, TCP or TLS failed, so the text would be a guess.
    • Rejected: The current message. The issue reports that the exception class is missing from the terminal output.
  • Does the fix wait on the cause of the 17:14Z failure?: No. A failed lookup raises httpx2.ConnectError and a connect that does not answer raises ConnectTimeout, and the retry and the cause text cover DNS, TCP, TLS and an Access answer alike, so the change does not wait on the cloudflared and llm-gateway logs; the next failure names its cause. (best practice) [sources: src/devops_cli/http/egress.py:180, src/devops_cli/http/egress.py:190, src/devops_cli/http/egress.py:208]
    • Chosen: Retry regardless of the cause. Each reported failure is a transport error that a retry can ride out, and the message names the class when it cannot.
    • Rejected: Wait for the Loki logs. The logs are outside the repository, and the fix holds for each cause that the issue names.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor

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

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. The failure is intermittent (1 in about 12 runs) and requires a manual re-run, but does not lose data or block all users.
    • Value: Medium. Each occurrence wastes a full intake run and forces a manual retry, but the low frequency limits total impact.
    • Effort: Medium. The retry layer exists in unified.py; the fix adds connect-error handling with backoff or a clearer error message within the existing transport.
    • Related: fix(ai): the verify eval agent run fails with ModelConnectionError #4345, which asks for a different change. Both report an AI client connection failure that stops a roadmap command, but #4345 covers ModelConnectionError in eval verify while this candidate covers ConnectError or ConnectTimeout in intake.
  3. added
    priority/p2-medium
    on Nov 11, 2026
  4. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Another case on 2026-11-11, on the GitHub path, not the gateway path. One devops roadmap intake --title ... --confirm run stopped after 3 min 10 s and wrote nothing:

    Could not plan intake: GitHub gave no readable answer to GET /repos/dan-petty/devops-cli/issues?state=all&sort=updated&direction=desc&since=2026-11-11T20:41:32Z&per_page=100 (page 1): ConnectTimeout

    The same command worked a few minutes later. Thus a short connect failure on the GitHub listing read also stops intake. A fix must cover the two paths, or a separate item must name the GitHub path.

  5. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Another occurrence on 2026-11-12 at about 17:40Z: devops roadmap intake --title ... --body-file ... --confirm stopped after 17 s with AIClientError: Cannot connect to AI provider API, and intake wrote nothing. The same command passed on the first retry 1 minute later (it filed #5161). The other 8 intake runs of the same session passed.

  6. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    Another case of this failure on 2026-11-13, in devops roadmap eval verify --case 2169 --model deepseek-v4.1-flash-1017 from the devcontainer through the tunnel host.

    • Four runs of case 2169 stopped with AIClientError: The investigation's model call failed (ModelConnectionError, phase read, ConnectionResetError: [Errno 104] Connection reset by peer). They stopped at about 16:41Z, 16:46Z, 16:55Z and 16:56Z, after 15 s to 8 min of the run. The fifth run (16:57Z to 17:13Z) completed with no change.
    • Trace f9daefafc8dd3fe7afc8c583c03bb1ce (the 16:56Z run): one POST /v1/chat/completions got 200 in 3.3 s, and the run stopped 8 s later with no other gateway span. The gateway log has no error for that time, and llm-gateway had 0 restarts.
    • So a reset while the client reads the answer is also outside the retries of the HTTP transport. Each such failure costs a full case run again (5 to 20 min).
  7. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The issue needs a short connect failure (httpx.ConnectError, ConnectTimeout, or a reset while reading) retried with backoff inside the outer timeout in the AI client's HTTP transport, and the replies extend that need to the GitHub listing path. Tenacity is an open, maintained retry library that retries on chosen exception types with exponential backoff and a stop condition, and it is transport-agnostic, so it wraps both the httpx/httpx2 AI path and the gidgethub session; the searches surfaced it as the general retry library (the project already relies on tenacity's waiting parameters per ADR 0007), so it covers the whole need. No ADR in force rejects it.

    The repository already uses this tool.

    Tool: tenacity (https://pypi.org/project/tenacity)

    • License: Apache-2.0
    • Last activity: 2026-10-07
    • Maintained: yes

    Covers:

    • Retries a call on chosen exception types, e.g. httpx.ConnectError, httpx.ConnectTimeout and ConnectionResetError
    • Exponential backoff with wait and stop conditions, so retries stay inside the outer timeout
    • Works for sync and async calls, so it wraps the AI client transport and the gidgethub GitHub session alike
    • Transport-agnostic: it is not tied to one HTTP stack, unlike transport-level retries

    Lacks:

    • Does not by itself name the cause of the failure (DNS, TCP connect, TLS or an Access response) in the raised error
    • Not a drop-in HTTP transport: it must be wired around the AI client and GitHub calls
    • No built-in distinction of connect errors from read resets unless those exception types are listed in the retry predicate

    Tools considered:

    Searches run:

    • httpx transport retries connect error configuration, results: 5
    • python retry library backoff connect errors httpx, results: 5
    • tenacity python retry library httpx ConnectError, results: 5
    • httpx retries transport parameter connect timeout retry, results: 5
    • httpx HTTPTransport retries, results: 5
    • tenacity, results: 5
    • stamina python retry library, results: 5
    • tenacity retry on exception python library documentation, results: 5

    Sources:

    Checked on 2026-11-14. A person decides what this means for the item. The check changes no label and closes no issue.

  8. added
    status/ready
    and removed
    status/new
    on Nov 15, 2026
  9. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

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

    Moved to new items: #5805, #5806.

  10. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor

    Third short connect failure of intake to the gateway on 2026-11-16 (17:45Z, 17:52Z, 22:15Z): "The model gateway https:///v1 did not answer (AIClientError: Cannot connect to AI provider API...)", while the gateway liveness and model probes answered. A second try a minute later worked each time.

  11. added
    status/in-progress
    and removed
    status/ready
    on Nov 19, 2026
  12. added
    status/in-review
    and removed
    status/in-progress
    on Nov 19, 2026
  13. removed
    status/in-review
    on Nov 19, 2026
  14. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor
  15. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-19 05:06 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/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions