Skip to content

fix(agents): stop a fallback chain when onResponse aborts a failed Anthropic request - #3306

Merged
ben-reitz merged 2 commits into
mainfrom
fix/anthropic-onresponse-abort
Nov 26, 2026
Merged

ben-reitz merged 2 commits into
mainfrom
fix/anthropic-onresponse-abort

Conversation

@ben-reitz

@ben-reitz ben-reitz commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

If your onResponse throws an AbortError on a failed Anthropic response (a 429, say), the stream ended as an ordinary error instead of aborted, so a fallback chain went on to call its next model. The Anthropic wire kept the provider's error and reported that instead of what the hook threw. Now the hook's error is reported, as the chat-completions and Responses wires already do. This came up in the Devin review of #3223.

Summary

 streamAnthropic
   fetch (per request)
     gateway failure → onResponse(raw)
     vendor error    → pi-ai calls onResponse
+      a throw is kept as hookError
   settle stream
-    failStream(transportError)
+    failStream(hookError ?? transportError)

The docs also said onResponse fires for "every response", but a stream that drops and resumes reconnects without calling it again. models-pi-ai.md now says it reports each request once.

Evidence

New tests in gateway.test.ts: a hook that throws AbortError on a gateway 402 and on a vendor 429, on each of the three wires, plus ai(claude, { fallback: [workersAI] }) with a hook that aborts on the 429.

  • Before: the three Anthropic tests fail (stopReason is "error", and the fallback calls Workers AI).
  • After: all 40 gateway tests pass, along with the 135 pi-ai model tests and pnpm run check.

Merge Danger

Door: two-way

Blast Radius: small

Only Anthropic wire requests whose onResponse throws are affected. A hook that throws any other error still ends the stream as an error, now with the hook's message instead of the provider's.


Devin Review

… AbortError

On a failed response the wire kept the transport's error and reported it over whatever the caller's onResponse threw, on both the gateway-failure path and pi-ai's vendor-error path. An AbortError from the hook became an ordinary error, so a fallback chain tried its next leg. The hook's error now wins. Six tests (gateway failure and vendor 429 on all three wires, and a fallback that must not run); the three Anthropic ones fail without the fix. From the #3223 review.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c5666f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Patch
@cloudflare/agent-think Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration 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.

Devin Review found 2 potential issues.

Devin Review

// Only the newest request's failure is the stream's: a fallback that
// resends after one is answered on its own.
transportError = undefined;
hookError = undefined;

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.

🔍 Anthropic retries after a hook abort

With maxRetries enabled, the Anthropic SDK treats an AbortError thrown inside fetch as a retryable timeout. The next attempt clears hookError, so it can return a successful stream despite the abort. The regression tests disable retries.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

failStream(
outer,
message,
hookError !== undefined ? hookError.error : transportError,

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.

🔍 Missing diagnostics for rejected responses

When onResponse throws an ordinary Error on a failed dispatch, failStream gets only the hook error. The cloudflare-error diagnostic loses the dispatch status, code, and log ID.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@3306

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@3306

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@3306

hono-agents

npm i https://pkg.pr.new/hono-agents@3306

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@3306

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@3306

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@3306

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@3306

commit: 5c5666f

@agent-think

agent-think Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (d25feba9 → 5c5666f6, workflow run)

@ben-reitz
ben-reitz merged commit 9709c43 into main Nov 26, 2026
29 checks passed
@ben-reitz
ben-reitz deleted the fix/anthropic-onresponse-abort branch November 26, 2026 21:25
@github-actions github-actions Bot mentioned this pull request Nov 26, 2026
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