Skip to content

A Bot asks its model again by itself when the connection drops, and carries on - #2944

Merged
davidmckayv merged 1 commit into
mainfrom
model-call-retry
Dec 4, 2026
Merged

davidmckayv merged 1 commit into
mainfrom
model-call-retry

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

A Bot no longer stops partway through its work when the connection to its model drops. The LangGraph (TypeScript) engine, the one new Bots run on, now asks the model again by itself, with backoff, and the turn carries on from where it was. The "connection dropped" notice with Carry on appears only once every try is used up.

What happened

An internal user's Bot on OpenBot Cloud (Dec 4, 10:38 UTC) was 17 steps into a turn, and had just said it would do more, when it stopped. Read from that run's events and the server and engine logs:

  • The step-18 model call went through the model gateway to a ChatGPT plan. It got a 502 back in 18 ms, and the model client retried it. chatgpt.com then answered 200, and reset the stream 0.645 s later. The gateway logged the stream as ended by the provider, the engine read undici's TypeError: terminated (read ECONNRESET), and the run ended with a RUN_ERROR.
  • Our own timers and the network in between did not cause it. The gateway gives up after 300 s of silence, counted on the raw bytes before anything parses the stream, so pings and keepalives reset it, and a cut by it logs model-gateway-silent, which never appeared. The server's idle limit is off for the gateway routes. The engine's undici body and headers timeouts are 300 s. The load balancer and Envoy are not on the engine-to-gateway hop. AWS NAT drops TCP only after 350 s idle. No pod restarted or ran out of memory; the 1.2.11 roll was about 19 minutes later. The longest gap anywhere was 3.4 s.
  • It was a short blip on the provider's side. 4 of the 7 network failures in that window came within 17 s, across 3 server pods and 2 organizations, all on OpenAI routes and none on Anthropic. The provider's status page shows no incident.
  • The gap was ours. The model client retries a failure that happens before the answer begins (it did, for the 502). Nothing retried an answer that broke after it had begun, so the run ended and the person had to press Carry on.

What the leaders do

  • Claude Code retries transient failures up to 10 times with exponential backoff before it shows an error: server errors, overloaded responses, request timeouts, dropped connections and passing 429 throttles, but never a spend-limit 429. A connection that drops before any part of the response is complete is re-issued, and the turn continues, even if some text had already started streaming. A stalled stream is aborted and streamed again at most once. The overloaded backoff starts at 500 ms and is capped at 32 s. https://code.claude.com/docs/en/errors
  • Anthropic's own recovery for an interrupted stream: keep what was received, and ask again with a user message that says the previous response was interrupted and to continue from where it left off. Thinking and tool-use blocks can't be partially recovered. https://platform.claude.com/docs/en/build-with-claude/streaming (Error recovery)
  • OpenAI's SDKs retry connection errors, 408, 409, 429 and 5xx twice with backoff, but only before the stream starts. https://github.com/openai/openai-node#retries. For a stream that drops, OpenAI's answer is background mode, which resumes from a sequence_number cursor. https://developers.openai.com/api/docs/guides/background. That works only on OpenAI's own Responses API, not on a ChatGPT plan, Anthropic or Gemini, and it keeps response data even under store: false and Zero Data Retention. So it isn't used here.

This follows Claude Code's numbers and Anthropic's documented recovery, and applies them to every provider the engine speaks to.

What changed

  • agent-langgraph-ts/src/retry.ts (new). It decides which failures are passing ones:

    • Retried: a dropped or reset connection; a stream error part way (Anthropic's overloaded_error, OpenAI's server_error); a 408, 409 or 5xx, 529 included; a passing 429.
    • Not retried: a stop the caller asked for; a refusal; a key, quota or billing error (a 429 that isn't a throttle included); a request the provider can't take; x-should-retry: false; a plan's harness error (the harness retries itself).
    • Backoff: 500 ms doubling to 32 s, with jitter, and at most 10 retries. A stall gets 1 retry, a passing 429 gets 3, and a connection that never opened gets 2.
    • 429s: a 429 waits as long as the provider asked (retry-after-ms, retry-after). One that asks for more than a minute is said to the person, not waited out.
  • run.ts. A failed step is asked again here, in the one place:

    • Words already on screen: they stay. The next try is sent them and Anthropic's "continue from where you left off" message, and its words carry on in the same message.
    • A call that was half written: it's closed before the next try. That run ends with a MESSAGES_SNAPSHOT holding only the calls the step finally made, since a snapshot is the only AG-UI way to take a call back out of the thread. Every other run still sends no snapshot.
    • No tool call is ever run twice. This engine runs none but the recall, and every other call goes back to the caller only once the step is whole.
  • What the person sees while a step waits. The run sends plain AG-UI CUSTOM events, so any Bot can send them:

    • openbot.reconnecting ({ attempt, attempts, retryInMs }) while it waits;
    • openbot.reconnected once the answer comes again.

    Nothing but CUSTOM events is sent during the wait. The web app draws them as "Reconnecting…" on the work line (Say "Reconnecting…" on the live line while a Bot retries its model #2942).

  • models.ts. The model client's own retries are off, so there's one owner of retries: ten tries, not ten times three. A run the server retries itself (openbotSendOnce) is still asked once.

  • Unchanged once every try is used up. The run ends with the same RUN_ERROR as before, "Connection dropped (…) before … finished answering.", and the person reads the existing notice with Carry on.

Tests

test/reconnect.test.ts, through the real engine over HTTP, against the fake provider's real SSE wire formats. A cut is a TCP reset part way through the stream, the way the provider's broke on Cloud.

  • Retried, and the run finishes on its own with no RUN_ERROR:
    • Atai's path: the gateway's /v1/responses breaks after a line and part of a call. The run finishes with the call the next try made. The line is said once, and the thread the caller holds (applied with @ag-ui/client) has no half-written call.
    • A reset part way through an answer, on OpenAI Chat Completions, Anthropic Messages and Gemini.
    • Anthropic overloaded part way through its stream, twice.
    • A 529 and then a 503 before the answer begins.
    • A passing 429 that waits the 300 ms it asked for.
  • Said at once, never retried:
    • a customer's quota 429;
    • a 429 asking for 10 minutes;
    • a 400;
    • a run sent with openbotSendOnce.
  • A connection that keeps dropping: 11 requests, then the same RUN_ERROR as before. model-route.test.ts's gateway test now covers this case.

Red/green: with run.ts and models.ts as they are on main, 7 of the 12 new tests fail: every retried case, and the "asked ten times" one. With the change, all 12 pass, and the whole engine suite passes (126, with 3 live tests skipped).

Changelog

Bots.

  • Fixed: a Bot no longer stops partway through its work when the connection to its model drops. A dropped connection, a server error, an overloaded model or a passing rate limit is now retried automatically, up to 10 times with backoff, and the turn carries on where it was. The Bot shows "Reconnecting…" while it waits. "The connection dropped" with Carry on appears only if every try fails. A spend limit, quota or billing refusal is still said at once.

…arries on

On OpenBot Cloud a Bot 17 steps into a turn stopped when chatgpt.com reset
its stream 0.6 s after answering 200. Nothing in OpenBot cut it, but nothing
retried an answer that broke once it had begun, so the run ended and the
person had to press Carry on.

The LangGraph (TypeScript) engine now asks a failed step again itself, as
Claude Code does: a dropped connection, a stream error part way, a 408, 409,
5xx or 529, or a passing 429, up to 10 times with backoff from 500 ms to 32 s,
the provider's retry-after kept to. A quota, billing or invalid request is
never retried. Words already said carry on in the same message, with
Anthropic's documented "continue from where you left off" request, and a
half-written call is taken out of the thread with a closing snapshot. While it
waits the run sends AG-UI CUSTOM openbot.reconnecting and openbot.reconnected.
The model clients' own retries are off, so retries have one owner. Once every
try is used up the run ends as before, with Carry on.
@davidmckayv
davidmckayv added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit d683fd9 Dec 4, 2026
36 checks passed
@davidmckayv
davidmckayv deleted the model-call-retry branch December 4, 2026 16:44
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.

1 participant