Stop Conditions
Control multi-turn execution with stopWhen. Use built-in helpers or custom conditions to stop by step count, tool calls, cost, or tokens.
Control multi-turn execution with stopWhen. Use built-in helpers or custom conditions to stop by step count, tool calls, cost, or tokens.
Stop after a specific number of steps:
Stop when a specific tool is called:
Stop after using a certain number of tokens:
Stop after reaching a cost threshold:
Stop on a specific finish reason:
Pass an array to stop on any condition:
Execution stops when any condition is met.
Create custom conditions with a function:
Custom functions receive:
Each step contains:
Stop after a time limit:
Stop based on response content:
Stop when results meet quality threshold:
Combine conditions for complex logic:
If you were using maxToolRounds, migrate to stopWhen:
If stopWhen is not specified, the default is stepCountIs(5).
Always include a hard limit to prevent runaway execution:
Track why execution stopped:
Verify conditions work as expected: