Skip to main content
Set stop conditions on the agent to end a run on your budget, not the model’s. The first condition that matches records its reason as the run’s stop reason and returns the run with status stopped. Separately, the step limit caps the model turns in each run or continue call. Rust has only the step limit.

Combine built-in limits

Token and cost limits depend on the usage the provider reports, so keep a step or duration limit as well.

Available helpers

Helpers sit on AgentSdk in C#, PHP (AgentSdk::), and Java, on PhaseoAgentSdk in Ruby, and are package functions in TypeScript, Python, and Go. The duration argument differs:

When conditions are checked

The languages don’t check conditions at the same point, and they don’t treat the step limit the same way:
Python’s stop conditions don’t cut a tool loop short. Bound tool loops with max_steps. stop_when passed to run() is ignored; set it on the definition.

Read why a run stopped

A stopped run is a normal return, not an error. Check that the run’s status is stopped, read its stop reason, then read the usage and the last step. Decide in your application which reasons count as success, retryable, or worth showing the user.

Write a custom condition

A custom condition receives the run’s state and returns a reason to stop, or a “no” value to continue. Return a reason string when operators or your interface need to explain why a run ended; it’s saved as the stop reason.
Last modified on November 5, 2026