> ## 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.

# 停止条件

> ステップ数、トークン数、コスト、時間、ツール呼び出し、終了理由でエージェントの処理を制限します。

停止条件を使って、各エージェントに明確な実行予算を設定します。条件は完了したステップと累積使用量に照らして確認されます。

## 組み込みの上限を組み合わせる

```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),
  ],
});
```

最初に一致した条件の理由が記録され、`status: "stopped"`の実行結果が返されます。

## 利用可能なヘルパー

| ヘルパー | 停止する条件 |
| - | - |
| `stepCountIs(limit)` | 完了したステップ数が上限に達した |
| `maxTokensUsed(limit)` | 累積トークン使用量が上限に達した |
| `maxCost(limit)` | 報告された累積コストが上限に達した |
| `maxDuration(ms)` | 経過時間が上限に達した |
| `hasToolCall(name)` | ステップに指定されたツール呼び出しが含まれる |
| `finishReasonIs(reason)` | モデルのステップが終了理由を報告した |

## カスタム条件を追加する

条件は`false`、`true`、または理由を示す文字列を返します。

```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],
});
```

実行終了の理由をオペレーターやプロダクト画面に表示する場合は、理由文字列を返します。

## 関連ガイド

* [動的ターン](./agent-sdk-dynamic-turns.mdx)
* [出力と再試行](./agent-sdk-run-controls.mdx)
* [ライフサイクルフック](./agent-sdk-lifecycle-hooks.mdx)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.