Raise `IncompleteToolCall` right away when the token limit cuts off an output tool call, instead of retrying
Posted by Claude Code on Douwe's behalf.
Part of #12399
Why
When the token limit cut off a ToolOutput call's arguments (finish_reason == 'length', JSON that doesn't parse), the agent re-prompted the same model with the same limit until the output retries ran out (2 requests with retries=1, 4 with retries=3), and only then raised IncompleteToolCall. In run_stream() it raised Output validation failed during streaming instead, without naming the limit. #12406 stopped this for NativeOutput/PromptedOutput text; this does the same for output tools, following #3504/#3512 (after hitting the limit, don't keep sending the same request).
New public surface
None.
User-visible behavior
A cut-off output tool call raises IncompleteToolCall (same message as before) after the first request, in run(), iter() and run_stream(). Arguments that parse but that the schema or an output validator rejects keep their retry. When another output tool call in the same response succeeds, it still wins.
Verification
tests/test_agent.py::test_truncated_output_tool_call_raises_without_retry(run,iter; asserts one request withretries=3)tests/test_agent.py::test_output_tool_args_that_parse_but_are_rejected_with_finish_reason_length_are_retried(schema, output validator)tests/test_agent.py::test_truncated_output_tool_call_does_not_override_valid_output_in_same_response(end_strategy='exhaustive')tests/test_agent.py::test_tool_exceeds_token_limit_but_complete_non_object_argstests/models/test_openai_responses.py::test_openai_responses_tool_output_cut_off_by_max_tokens(recorded,streamFalse/True; each cassette has one request)
Notes from recording: OpenAI Chat Completions rejects a cut-off tool call with a 400, and Anthropic (without fine-grained tool streaming) returns {} as the input, so neither reaches this path; the Responses API does.
What changes for existing users
A run whose output tool call is cut off by max_tokens fails after one request instead of spending its output retries; the exception type and message are unchanged (run_stream() now raises IncompleteToolCall instead of the generic streaming validation error).
Only string args that don't parse as JSON count as cut off now: a tool call (function or output) with complete JSON args that aren't an object, at finish_reason == 'length', is treated as an ordinary invalid-arguments failure, so a function tool's exhausted retries raise the retry-limit UnexpectedModelBehavior rather than IncompleteToolCall.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code