Skip to content

Tool is Invoked Multiple Times Based on maxSteps Value #5195

Closed
@Nishchit14

Description

@Nishchit14

Description

I am using the latest version of ai-sdk and noticed that a tool is being executed multiple times corresponding to the maxSteps value. For example, when setting maxSteps: 5, the tool is invoked 5 times, which is unexpected behavior.

Expected Behavior

The tool should only be called as necessary, rather than being triggered exactly maxSteps times.

Actual Behavior

  • When setting maxSteps: 5, the tool is called 5 times.
  • The number of tool executions matches the maxSteps value, even when it shouldn’t.

Steps to Reproduce

  1. Use the latest version of ai-sdk.
  2. Set maxSteps: 5 in the configuration.
  3. Observe that the tool is invoked exactly 5 times.

Environment

  • AI-SDK Version: "ai": "^4.1.60", @ai-sdk/openai": "^1.2.4"
  • Node.js Version: v22
  • OS: Mac

Code example

    messages,
    toolChoice,
    abortSignal: req.signal,
    tools,
    maxSteps: 5

AI provider

@ai-sdk/openai": "^1.2.4"

Additional context

when is set maxStep: 2

this is what message.parts look like

[
  {
    "text": "",
    "type": "text"
  },
  {
    "type": "tool-invocation",
    "toolInvocation": {
      "args": {
        "query": "explain this file"
      },
      "step": 0,
      "state": "result",
      "result": {
        "data": "Your receipt from ...",
        "time": 898
      },
      "toolName": "file_search",
      "toolCallId": "call_DJDvNKV3Qvj8XKKTE4FVktZf"
    }
  },
  {
    "text": "",
    "type": "text"
  },
  {
    "type": "tool-invocation",
    "toolInvocation": {
      "args": {
        "query": "receipt from Mailo February 4, 2025"
      },
      "step": 1,
      "state": "result",
      "result": {
        "data": "Your receipt from ...",
        "time": 868
      },
      "toolName": "file_search",
      "toolCallId": "call_nzCraUvAnk2DfVx9Q75Rbw2Z"
    }
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions