Agent( )

Pattern
Loop Control: stopWhen
Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.
IntermediateJun 2026View the full pattern on desktop


Problems solved
- Stop agent loops with stepCountIs
- Halt when a specific tool is called via hasToolCall
- Combine multiple stopWhen conditions
- Implement custom StopCondition logic over step history
Use cases
- Budgeting agent steps in long tool loops
- Stopping when a completion tool fires
- Preventing runaway tool-calling agents
- Visualizing step timelines for loop control
AI SDK APIs
ToolLoopAgentstopWhenstepCountIshasToolCallStopConditiontoolgateway
Tags
aiagentsloop-controlstop-whenstep-count-ishas-tool-calltool-loop-agentai-sdkagent-api
Requirements
- Node.js
- 24+
- Memory
- 512MB
- Other
- OpenAI API key
Environment variables
OPENAI_API_KEYGet key
External services
openai
Ready to use?
View the full interactive pattern with live preview and copy the source code.
View the pattern on desktop






