Agent( )
Pattern
INT·Jun 26

Loop Control: stopWhen

Demonstrate ToolLoopAgent loop control with stopWhen. Covers stepCountIs, hasToolCall, combined conditions, and a custom StopCondition with step timeline visualization.

View Full Pattern on Desktop
Loop Control: stopWhen preview

Problems Solved

  • Control agent loop length with stepCountIs
  • Stop agent loops when a specific tool is called
  • Combine multiple stopWhen conditions
  • Implement custom StopCondition logic
  • Inspect agent steps and tool calls after a run

Use Cases

  • Limit runaway agent tool loops
  • Stop when a completion tool is invoked
  • Budget agent steps in production workflows
  • Custom termination based on step history

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

External Services

openai

Ready to use?

View the full interactive pattern with live preview and copy the source code.

View Pattern on Desktop
End of Pattern