> ## Documentation Index
> Fetch the complete documentation index at: https://phaseo.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Stop conditions

> Bound agent work by steps, tokens, cost, duration, tool calls, or finish reason.

Use stop conditions to give every agent a clear execution budget. Conditions are checked against completed steps and cumulative usage.

## Combine built-in limits

```typescript theme={null}
import {
  maxCost,
  maxDuration,
  maxTokensUsed,
  stepCountIs,
} from "@phaseo/agent-sdk";

const agent = createAgent({
  id: "bounded-research",
  stopWhen: [
    stepCountIs(12),
    maxTokensUsed(40_000),
    maxCost(2),
    maxDuration(60_000),
  ],
});
```

The first matching condition records its reason and returns a run with `status: "stopped"`.

## Available helpers

| Helper                   | Stops when                                 |
| ------------------------ | ------------------------------------------ |
| `stepCountIs(limit)`     | The completed step count reaches the limit |
| `maxTokensUsed(limit)`   | Cumulative token usage reaches the limit   |
| `maxCost(limit)`         | Cumulative reported cost reaches the limit |
| `maxDuration(ms)`        | Elapsed runtime reaches the duration       |
| `hasToolCall(name)`      | A step contains the named tool call        |
| `finishReasonIs(reason)` | A model step reports the finish reason     |

## Add a custom condition

A condition can return `false`, `true`, or a reason string:

```typescript theme={null}
const stopAfterRepeatedFailures = ({ steps }) => {
  const failures = steps.filter((step) => step.status === "failed").length;
  return failures >= 2 ? "repeated_failures" : false;
};

const agent = createAgent({
  id: "support-agent",
  stopWhen: [stepCountIs(10), stopAfterRepeatedFailures],
});
```

Prefer a reason string when operators or product interfaces need to explain why a run ended.

## Related guides

* [Dynamic turns](./agent-sdk-dynamic-turns)
* [Outputs and retries](./agent-sdk-run-controls)
* [Lifecycle hooks](./agent-sdk-lifecycle-hooks)
