fix(core): retry forced tool use rejected by Claude thinking
What
agent.act and exploring agent.assert retry with automatic tool choice when a gateway returns Anthropic's HTTP 400 error Thinking may not be enabled when tool_choice forces tool use. The retry keeps manual extended thinking enabled and ignores 5xx retry chains that quote the refusal.
The remembered auto mode has two scopes. A refusal that names the model (Claude Fable 5.1, Muse Spark) covers every later step on that model. Any other refusal or downgrade covers only later steps whose providerOptions match exactly, and a step with no providerOptions is a scope of its own.
Why
The forced-choice fallback missed Anthropic's manual-thinking wording, so the step failed with MODEL_PROVIDER_FAILED even though Claude could continue under auto.
Remembering that refusal on the bare model object would force sibling agents sharing the same model constant into auto even when their providerOptions leave thinking off. The same holds when thinking is a setting of the model itself (AI SDK defaultSettingsMiddleware): the agent that inherits it sends no options, and a sibling whose options turn thinking off must keep forced tool calls. A refusal that names the model is not about options, so scoping it per option set would cost one rejected request per agent where main costs one per model.
Adaptive thinking has a separate behavior. On models that accept a forced tool call, Claude can start its response with that call and skip thinking. A successful response takes no error-driven retry. Anthropic's tool-choice documentation
How it works
FORCED_CHOICE_PATTERNSadds Anthropic's manual-thinking HTTP 400 sentence (tool-choice.ts:21).MODEL_WIDE_PATTERNSlists the refusals that name the model (tool-choice.ts:33).forcedToolChoiceRefusal, which replacesisForcedToolChoiceRejected, walks the fullcauseandlastErrorchain up to depth 8. It returnsundefinedif any wrapped error carries a non-4xx HTTP status, so a statuslessAI_RetryErrorwrapping an HTTP 502 never triggers theautofallback. Otherwise it returns'model'for a model-wide match and'options'for any other match (tool-choice.ts:103).rememberAutoToolChoiceandprefersAutoToolChoicekeep aWeakMapper model instance. It holds a model-wide flag and a set ofproviderOptionskeys, with''for no options (tool-choice.ts:136). A key is the JSON form of the options with sorted keys, sotoJSONapplies. Options with no JSON form, such as abigintan executor passes directly, get no key: the step still retries withauto, and nothing is remembered (tool-choice.ts:174).LoopRunreads the memory at construction (tool-loop.ts:311).freeToolChoiceRetryrecords the refusal's scope, and'options'for a prose reply under a forced choice (tool-loop.ts:656).noticeToolChoiceDowngraderecords'options', because the AI SDK's Anthropic provider downgrades both for models that reject forced use and for manual thinking, and its warning does not say which (tool-loop.ts:673).
Verified
Ran it locally: yes
The built CLI, with a scripted anthropic.messages model, ran on main (7c1b2b258) and on this branch (d00f974b). The model throws the Anthropic 400 errors. The table lists the tool choice of each request.
shared-model.e2e.ts: thinking comes fromdefaultSettingsMiddleware. Thedefaultagent sends no options, andleansendsthinking: { type: 'disabled' }withmaxModelCalls: 1.fable.e2e.ts: the model refuses every forced choice with Fable 5.1's message. Agentslow(effort: 'low'),high(effort: 'high'), anddefault(no options) run in that order.
✓ |verify| tests-verify-2510/shared-model.e2e.ts (2 tests) 331ms ai 240 tokens
✓ |verify| tests-verify-2510b/fable.e2e.ts (3 tests) 519ms ai 360 tokens
| Main | This PR | |
|---|---|---|
| Inherited manual thinking + forced tool choice | required, MODEL_PROVIDER_FAILED | required, then auto with thinking intact, passes |
Sibling that turns thinking off (maxModelCalls: 1) | Skipped after step 1 failed | tool:complete_step, passes |
| Fable-style refusal across three option sets | required, then auto ×3: one rejected request | required, then auto ×3: one rejected request |
One more run covers options whose own field is a bigint and whose toJSON writes it as text, such as a 64-bit request id. Config resolution accepts them. The model answers the manual-thinking 400, and the test runs two agent.act steps. On 2e10b94b, the first step failed after one required call with ACTION_FAILED: Do not know how to serialize a BigInt. On d00f974b the calls are required, auto, auto, and the test passes.
Unit tests in packages/e2e/tests/unit/tool-loop-tool-choice.test.ts cover the following:
requiredandtool:complete_stepretries withproviderOptionskept- shared-model isolation with explicit options and with model-level thinking
- one rejection across option sets for a model-wide refusal
- the
forcedToolChoiceRefusalscope across gateway wrappers, 502AI_RetryErrorchains, and unrelated thinking 400s - the options scope of a downgrade warning and of a prose reply under a forced choice. Each test fails when its path remembers the whole model.
- scope keys from the JSON form of
providerOptions(toJSONapplied), and options with no JSON form, which are retried but not remembered
On 84f333309 the model-level thinking test and the model-wide test fail. Live gateway behavior is still unverified: no model key was available.
Summary by CodeRabbit
-
Bug Fixes
- Agent turns now retry with automatic tool choice when a model rejects forced tool use, including Claude’s manual extended-thinking restriction, while retaining thinking settings.
- Automatic tool choice applies to the affected model or matching provider options, allowing unrelated configurations to continue using forced tool calls.
-
Documentation
- Clarified which models reject forced tool calls and how fallback behavior applies to later steps.