Basic Usage
Built-in Stop Conditions
stepCountIs(n)
Stop after a specific number of steps:hasToolCall(name)
Stop when a specific tool is called:maxTokensUsed(n)
Stop after using a certain number of tokens:maxCost(amount)
Stop after reaching a cost threshold:finishReasonIs(reason)
Stop on a specific finish reason:Combining Conditions
Pass an array to stop on any condition:Custom Stop Conditions
Create custom conditions with a function:StopConditionContext
Custom functions receive:| Property | Type | Description |
|---|---|---|
steps | StepResult[] | All completed steps including results and usage |
StepResult
Each step contains:Advanced Patterns
Time-Based Stopping
Stop after a time limit:Content-Based Stopping
Stop based on response content:Quality-Based Stopping
Stop when results meet quality threshold:Combination with Early Exit
Combine conditions for complex logic:Migration from maxToolRounds
If you were usingmaxToolRounds, migrate to stopWhen:
Default Behavior
IfstopWhen is not specified, the default is stepCountIs(5).
Best Practices
Always Set Limits
Always include a hard limit to prevent runaway execution:Log Stop Reasons
Track why execution stopped:Test Conditions
Verify conditions work as expected:See Also
- Tools - Multi-turn orchestration
- Dynamic Parameters - Adaptive behavior
- nextTurnParams - Tool-driven modifications