Skip to main content
Stop conditions control when agent execution terminates. Use them to prevent runaway agents, control costs, or stop after specific criteria are met.

What are stop conditions?

Stop conditions are functions that run after each agent step and return True to stop execution or False to continue. They’re essential for:
  • Cost control - Limit token usage
  • Preventing infinite loops - Cap the number of reasoning steps
  • Goal-based termination - Stop after specific tools are called or text is generated
  • Safety - Halt execution when thresholds are exceeded
They run after the LLM step and tool execution.

Built-in stop conditions

  • Max tokens: Stop when total token usage exceeds a limit.
  • Max steps: Stop when the number of reasoning steps reaches a threshold. Here, an LLM call along with any tool executions suggested by the LLM is considered a single step.
  • Executed tool: Stop when specific tools have been called.
  • Has text: Stop when specific text appears in the response.
Agent stops when any of the stop conditions are met. Execution order: Stop conditions are run in the order in which they are specified after each step. First condition that returns True stops the agent.

Custom stop conditions

Define your own stop conditions for specific use cases:

Stop condition context

Stop conditions receive a StopConditionContext with execution history:

Key takeaways

  • Stop conditions terminate agent execution when criteria are met. Useful for cost control and safety.
  • Built-in conditions: max_tokens, max_steps, executed_tool, has_text
  • Custom conditions defined with @stop_condition decorator
  • Multiple conditions - agent stops when any returns True
  • Access full execution history via StopConditionContext