Skip to content

[Bug] A tool call with invalid JSON arguments makes up to 10 hidden provider requests, missing from steps and usage #1855

Closed

Description

@diyaavirmani

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

  1. 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\""}
  2. Run nanocoder --trust-directory --plain --json run "read hello.txt".
  3. Count the requests the server received, and compare with steps and usage in the JSON report.
  4. Variant: the same with finish_reason: "length" and a write_file call 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() calls streamText with stopWhen: [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.
  • stopOnUnknownTool only stops on NoSuchToolError (chat-handler.ts:23-31). An invalid call to a real tool raises InvalidToolInputError, so the SDK keeps looping up to MAX_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.usage are returned (chat-handler.ts:413-430), so the earlier requests disappear from steps and usage.
  • convertAISDKToolCalls passes toolCall.input through as arguments (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

  1. added
    needs-triage
    on Oct 19, 2026
  2. addyCooks commented on Oct 20, 2026

    @addyCooks
    Collaborator

    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.

  3. Vishesh-Verma-07 commented on Oct 20, 2026

    @Vishesh-Verma-07
    Contributor

    @addyCooks can you assign this to me

  4. added
    area:agent
    area:providers
    area:tools
    bug
    performance
    and removed
    needs-triage
    on Oct 20, 2026
  5. addyCooks commented on Oct 27, 2026

    @addyCooks
    Collaborator

    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 : )

  6. Vishesh-Verma-07 commented on Oct 27, 2026

    @Vishesh-Verma-07
    Contributor

    no worries would be happy to take other issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:agentarea:providersarea:toolsbugperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions