Skip to content

fix(ai): chunkTimeout counts model output, not keep-alives - #64492

Open
adamdotdevin wants to merge 2 commits into
devfrom
chunk-timeout-content
Open

adamdotdevin wants to merge 2 commits into
devfrom
chunk-timeout-content

Conversation

@adamdotdevin

@adamdotdevin adamdotdevin commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

No issue filed.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

A streamed anthropic-messages call through Zen went silent partway through a tool call for about 30 minutes and never errored. The model had stopped producing output, but the connection kept sending : keep-alive comments and Anthropic ping events, and those reset chunkTimeout:

  • Native HTTP route: RequestExecutor wraps the raw response body in the chunk timer (withBodyTimeouts in packages/ai/src/route/executor.ts), so any bytes reset it, comments included.
  • AI SDK route: wrapSSE already ignores comments, but every SSE event resets the deadline, ping included (packages/core/src/aisdk.ts:68).

Fix: chunkTimeout now resets only on events that carry model output.

  • http.ts: model routes send with chunk: false, so the executor skips its byte-level chunk timer, and run the chunk timer on the framed stream instead. SSE framing already drops comments, empty events and keepalives. The header timeout and whole-request deadline stay in the executor, and every other request keeps the byte-level chunk timer. When the chunk timer fires, it raises a retryable read TransportError with code Timeout, as before. The default doesn't change.
  • anthropic-messages.ts: ping is no longer one of the SSE event names the framing keeps, so it never becomes a frame.
  • aisdk.ts: ping events don't move the deadline.

Side effects:

  • document and the other non-SSE framings (lines, Bedrock event stream) now bound the time between frames, not between byte reads. For document, the chunk timeout covers downloading the whole body.
  • With includeRaw, a named event: ping no longer shows up as a raw event.

How did you verify your code works?

New tests:

  • packages/ai/test/http-timeout.test.ts:
    • A stream that sends only : keep-alive and ping past 15 minutes now fails with the read timeout.
    • A stream that sends a text delta every 10 minutes for 30 minutes completes.
  • packages/core/test/aisdk.test.ts: the heartbeat test now also sends ping events and still has to time out.

Each new test fails when the fix it covers is reverted.

Also ran:

  • packages/ai: bun run test (2267 pass, 0 fail) and bun run typecheck
  • packages/core: aisdk.test.ts, session-model-transport.test.ts and bun run typecheck

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #61346

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. On the base branch, the new keep-alive/ping transport test hangs and the AI SDK heartbeat test now fails. With this PR both pass, as do the full packages/ai suite and type checks for ai and core.

Moving the chunk timer after framing is the right layer: anything framing drops (comments, empty events, data: null, keepalives, unlisted event names) can no longer count as progress, and the whole-request timer still runs on the raw bytes. The document framing side effect (the chunk timeout now covers the whole body download) is fine with the 15-minute default.

Public API is unchanged, apart from one behaviour change: with includeRaw, a named Anthropic event: ping no longer appears as a raw event.

This fixes the keep-alive half of #61346. The other half, silent compaction retries, is not covered, so that issue should stay open.

Related: #61346

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keep-alive/ping fix works: the new transport test hangs on the base branch and passes here. The full packages/ai suite, core aisdk.test.ts and type checks for both packages also pass. The rebased version adds a new hang, though (see the inline comment).

Turning off the executor's chunk limit for model routes also removes the only limit on reading an HTTP ≥400 body. A gateway that returns 503 headers and then stalls on the body now hangs forever when no whole-request timeout is set, which is the default. I checked this with a TestClock test that advances 20 minutes: on the base branch it fails with "Timed out waiting for response data"; with this PR it never finishes.

Public API unchanged.

const response = yield* RequestExecutor.withTimeout(
RequestExecutor.withTls(runtime.http.execute, request.http?.tls),
request.timeout,
{ ...limits, chunk: false },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With chunk: false, nothing limits statusError's response.text read on an HTTP ≥400 response any more. The frame timer below only runs on success bodies. A provider or gateway that sends 503 headers and then stalls on the body hangs forever unless a total timeout is set. On the base branch this failed after 15 minutes with "Timed out waiting for response data". I reproduced it with a TestClock test: a 503 whose body sends a few bytes and then stalls, clock advanced 20 minutes. One fix is to keep the byte-level chunk limit for error statuses, or to have the executor take the frame-level chunk limit only for 2xx bodies.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b445e0: the executor keeps its byte-level chunk limit for status ≥ 400 bodies even when the caller passes chunk: false to time frames itself, since statusError reads those whole. Added a TestClock test: a 503 whose body stalls now fails after 15 minutes with the 503, and it hangs without the fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't verify this: 8b445e0 isn't on the PR. pr/head is still a9a4f2e17 from 16:49 CST, my checkout doesn't have that commit, and I couldn't fetch from origin. It looks like it hasn't been pushed yet. In the code I have, executeOnce in packages/ai/src/route/executor.ts (around lines 280–285) still passes the same chunk limit to withBodyTimeouts whatever the status, so the 503 body read still has no limit.

The approach you describe is right. Choosing the limit from raw.status after headers arrive is the correct place, because statusError reads ≥ 400 bodies in full and the frame timer never sees them. Once it's pushed, check one case. http.ts sends { ...limits, chunk: false } both when it times frames itself and when the user turned the chunk timeout off. If the executor turns false back into the 15-minute default for ≥ 400, a user who disabled chunkTimeout would still get that limit on error bodies. To avoid this, have http.ts pass the user's original chunk value separately (or as a flag) and use that for ≥ 400 bodies. Or decide that this case doesn't matter.

I'll check the diff and run the new TestClock test once the commit shows up on pr/head. I made no changes to the checkout. The bun.lock modification there isn't mine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed now: 8b445e0d0a is on the branch. The first push was blocked by the pre-push typecheck, which passed once dependencies were refreshed after the rebase.

On a user who turned chunkTimeout off: I'm treating that case as not worth the extra plumbing. statusError reads an error body whole just to build the error message, so an error body that never finishes would hang the call forever, which nobody turning off the stream timeout means. Error bodies always get the 15-minute byte limit, and success bodies honor false as before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, 8b445e0d0 is on pr/head. The change in executor.ts (lines 282–286) fixes the stalled 503: when the status is ≥ 400 and the chunk limit is false, it now falls back to DEFAULT_CHUNK_TIMEOUT_MS. Success bodies behave as before. packages/ai/test/http-timeout.test.ts passes locally, 11 of 11 including the new 503 test. I didn't revert the fix to see the test hang, and I didn't rerun the full suite or the typecheck.

I'm fine with your call on users who turn chunkTimeout off. One related case you may not have intended: a user who sets a shorter chunkTimeout, say 30s, also gets 15 minutes on error bodies on model routes. That's because http.ts always passes chunk: false, so the executor never sees the user's number. It's no worse than before this PR's first commit for anything a user would notice, and fixing it needs the same plumbing you've decided against, so I'd leave it.

I made no changes to the checkout. The modified bun.lock there isn't mine.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error-body hang is fixed. A stalled 503 body now fails after the default chunk timeout again, as on the base branch. My earlier repro passes, as do the new test, the full packages/ai suite and its type check.

Public API unchanged.

This branch has not been deployed

No deployments
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