fix(core): retry forced tool use rejected by Claude thinking

Merged
MI
1d ago
+400-60

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

  1. FORCED_CHOICE_PATTERNS adds Anthropic's manual-thinking HTTP 400 sentence (tool-choice.ts:21). MODEL_WIDE_PATTERNS lists the refusals that name the model (tool-choice.ts:33).
  2. forcedToolChoiceRefusal, which replaces isForcedToolChoiceRejected, walks the full cause and lastError chain up to depth 8. It returns undefined if any wrapped error carries a non-4xx HTTP status, so a statusless AI_RetryError wrapping an HTTP 502 never triggers the auto fallback. Otherwise it returns 'model' for a model-wide match and 'options' for any other match (tool-choice.ts:103).
  3. rememberAutoToolChoice and prefersAutoToolChoice keep a WeakMap per model instance. It holds a model-wide flag and a set of providerOptions keys, with '' for no options (tool-choice.ts:136). A key is the JSON form of the options with sorted keys, so toJSON applies. Options with no JSON form, such as a bigint an executor passes directly, get no key: the step still retries with auto, and nothing is remembered (tool-choice.ts:174).
  4. LoopRun reads the memory at construction (tool-loop.ts:311). freeToolChoiceRetry records the refusal's scope, and 'options' for a prose reply under a forced choice (tool-loop.ts:656). noticeToolChoiceDowngrade records '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 from defaultSettingsMiddleware. The default agent sends no options, and lean sends thinking: { type: 'disabled' } with maxModelCalls: 1.
  • fable.e2e.ts: the model refuses every forced choice with Fable 5.1's message. Agents low (effort: 'low'), high (effort: 'high'), and default (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
MainThis PR
Inherited manual thinking + forced tool choicerequired, MODEL_PROVIDER_FAILEDrequired, then auto with thinking intact, passes
Sibling that turns thinking off (maxModelCalls: 1)Skipped after step 1 failedtool:complete_step, passes
Fable-style refusal across three option setsrequired, then auto ×3: one rejected requestrequired, 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:

  • required and tool:complete_step retries with providerOptions kept
  • shared-model isolation with explicit options and with model-level thinking
  • one rejection across option sets for a model-wide refusal
  • the forcedToolChoiceRefusal scope across gateway wrappers, 502 AI_RetryError chains, 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 (toJSON applied), 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.