Repository navigation
Description
Description
When the model sends a native tool call whose arguments aren't valid JSON (for example, cut off by the output-token limit), the AI SDK quietly runs up to 10 more model requests inside a single client.chat() call. Nanocoder's loop never sees them. They don't count toward steps, usage only includes the last request, and the retry limits (nanocoder.retries.*) only apply after each hidden batch.
In my test, the run made 30 provider requests while --json reported steps: 3, and usage.totalTokens counted 45 of the 450 tokens the provider served. That's the token drain the retry limits are meant to stop ("so a stuck model cannot silently drain tokens", docs/configuration/index.md:293). #1464 already stopped the hidden loop for unknown tool names, but an invalid call to a real tool still takes it.
There's a second, smaller problem. The broken call is passed on with its raw argument string as arguments. It's executed as Invalid tool arguments, then replayed to the provider in later requests with the arguments double-encoded as a JSON string literal.
Environment
- OS: macOS (Darwin 25.6.0, x86_64)
- Node version: 22.23.3
- Nanocoder version: main at
ef4e79ec, and v1.31.0 - Provider: a local OpenAI-compatible test server with scripted responses
- Model: n/a (scripted)
Steps to Reproduce
- In a scratch folder with a
hello.txt, point nanocoder at a scripted OpenAI-compatible server that always answers with this tool call (the arguments are missing the closing}):
{"name": "read_file", "arguments": "{\"path\": \"hello.txt\""} - Run
nanocoder --trust-directory --plain --json run "read hello.txt". - Count the requests the server received, and compare with
stepsandusagein the JSON report. - Variant: the same with
finish_reason: "length"and awrite_filecall whose content is cut off mid-string, which is what an output-token limit produces.
Expected Behavior
Each model response that contains a tool call goes back to nanocoder's loop. That's what the comment at source/ai-sdk-client/chat/chat-handler.ts:235 says happens ("the SDK never auto-runs" these tools). The retry limits then apply after one request, steps counts every model call, and usage is summed across all of them (docs/features/commands.md:203-205).
Actual Behavior
| Case | Requests the provider got | steps |
usage.totalTokens |
|---|---|---|---|
| invalid JSON arguments | 30 | 3 | 45 |
arguments cut off by finish_reason: length |
30 | 3 | 45 |
| unknown tool name (control, #1464) | 3 | 3 | 45 |
| valid call (control) | 2 | 2 | 30 |
All four runs ended with exit 1 after "Model repeated the same tool call 3 times", except the valid control, which exited 0. Each of the three steps hid 10 requests.
The history replayed to the provider for the broken call:
assistant tool_calls=[["call_bad", "read_file", "\"{\\\"path\\\": \\\"hello.txt\\\"\""]]
tool id=call_bad | Error: Error: Invalid tool arguments: Expected ',' or '}' after property value in JSON at position 20
Same result on main ef4e79ec and on v1.31.0. In my test the scripted model repeats the broken call every time, so each batch reaches the cap of 10. A real model may fix the call sooner, but every broken attempt in between is still invisible to the loop.
Logs/Screenshots
What I think is going on:
chat()callsstreamTextwithstopWhen: [stepCountIs(MAX_TOOL_STEPS), stopOnUnknownTool](source/ai-sdk-client/chat/chat-handler.ts:274).- When the SDK can't parse a call's input, it marks the call invalid, adds a tool-error result for it, and starts another step.
stopOnUnknownToolonly stops onNoSuchToolError(chat-handler.ts:23-31). An invalid call to a real tool raisesInvalidToolInputError, so the SDK keeps looping up toMAX_TOOL_STEPS(10). The existing spec says so explicitly: "An unparsable call to a real tool is not this condition's concern." (chat-handler.spec.ts:1080).- Only the final step's tool calls and
result.usageare returned (chat-handler.ts:413-430), so the earlier requests disappear fromstepsandusage. convertAISDKToolCallspassestoolCall.inputthrough asarguments(source/ai-sdk-client/converters/tool-converter.ts:16). For an invalid call that's the raw string, which is what gets executed and replayed.
Additional Context
Proposed fix (happy to adjust): also stop the step loop on an invalid call to a known tool (InvalidToolInputError), the same way #1464 does for unknown tools. The call then comes back to the conversation loop after one request, and the existing malformed-call handling and retry limits apply. In the same change, I'd keep an invalid call's raw text from being passed on as arguments, so it isn't executed or replayed double-encoded.
For tests: extend the stopOnUnknownTool spec with an invalid-input call to a real tool (it should stop after one step), and add a chat() test with a scripted invalid call that checks it produces one provider request and one step.
- I have searched existing issues to ensure this is not a duplicate
- I can reproduce this issue consistently
- This issue occurs with the latest version of nanocoder
@addyCooks could you take a look when you have a moment? I'd like to work on this one, so please assign it to me if that works for you.
Activity
addyCooks commentedon Oct 20, 2026
Thanks @diyaavirmani! I've already assigned you a few other issues,
so please raise PRs for those first, then ping me here and I'll assign this one to you.
Vishesh-Verma-07 commentedon Oct 20, 2026
@addyCooks can you assign this to me
addyCooks commentedon Oct 27, 2026
Thanks for the interest @Vishesh-Verma-07! @diyaavirmani asked for this one first, in the issue itself.
Happy to point you to something else in the meantime : )
Vishesh-Verma-07 commentedon Oct 27, 2026
no worries would be happy to take other issues
fix(ai-sdk-client): stop hidden SDK retries of a tool call with inval…