vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.28k stars 1.55k forks source link

Add `singleToolPerStep` option to prevent multiple tool calls within one step #3854

Open RostyslavManko opened 5 days ago

RostyslavManko commented 5 days ago

Feature Description

I propose adding a new configuration option singleToolPerStep that would restrict the AI to generating only one tool call per step. Currently, the AI SDK allows multiple tool calls to be generated within a single step, which can lead to race conditions and complex execution flows.

This would be implemented at the SDK level rather than requiring developers to implement their own synchronization logic. When enabled, the SDK would:

  1. Modify the prompt/system message to instruct the AI to only generate one tool call per step
  2. Enforce this restriction at the protocol level by only accepting the first tool call in a step
  3. Automatically continue to the next step after a tool call is completed

Use Cases

Use Cases

  1. Terminal/Command Execution: When executing terminal commands, it's crucial to process them sequentially and analyze each output before proceeding with the next command.

  2. Database Operations: When performing database queries or updates, executing one operation at a time ensures proper transaction handling and prevents race conditions.

  3. API Calls: When making external API calls, processing one request at a time allows for proper error handling and response analysis before proceeding.

  4. File System Operations: When working with files, sequential operations ensure proper file handling and prevent concurrent access issues.

Additional context

No response

grabbou commented 4 days ago

I have recently built an agent that executes terminal commands, and I actually appreciate this tool can run certain commands in parallel. I design each tool so that it can run independently. If it can't, I explicitly configure it via its description and/or input parameters, and generally, that works just fine!

I don't have anything per se against adding such configuration option, however, I would like to emphasize that current parallel behavior is actually working quite awesome of me.