|
| 1 | +--- |
| 2 | +title: isLoopFinished |
| 3 | +description: API Reference for isLoopFinished. |
| 4 | +--- |
| 5 | + |
| 6 | +# `isLoopFinished()` |
| 7 | + |
| 8 | +Creates a stop condition that never triggers, letting the agent loop run until it naturally finishes (i.e., the model stops making tool calls). |
| 9 | + |
| 10 | +By default, `ToolLoopAgent` uses `stepCountIs(20)` as a safety measure to prevent runaway loops that could result in excessive API calls and costs. If you are confident that your agent will terminate naturally or you are less concerned about costs, `isLoopFinished()` removes that limit and lets the agent run until the model is truly done. |
| 11 | + |
| 12 | +```ts |
| 13 | +import { ToolLoopAgent, isLoopFinished } from 'ai'; |
| 14 | +__PROVIDER_IMPORT__; |
| 15 | + |
| 16 | +const agent = new ToolLoopAgent({ |
| 17 | + model: __MODEL__, |
| 18 | + tools: { |
| 19 | + // your tools |
| 20 | + }, |
| 21 | + stopWhen: isLoopFinished(), |
| 22 | +}); |
| 23 | + |
| 24 | +const result = await agent.generate({ |
| 25 | + prompt: 'Analyze this dataset and create a summary report', |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +## Import |
| 30 | + |
| 31 | +<Snippet text={`import { isLoopFinished } from "ai"`} prompt={false} /> |
| 32 | + |
| 33 | +## API Signature |
| 34 | + |
| 35 | +### Parameters |
| 36 | + |
| 37 | +This function takes no parameters. |
| 38 | + |
| 39 | +### Returns |
| 40 | + |
| 41 | +A `StopCondition` function that always returns `false`, meaning it never triggers the stop condition. The agent loop will only stop through its natural termination conditions: |
| 42 | + |
| 43 | +- The model stops making tool calls, or |
| 44 | +- A tool without an `execute` function is called, or |
| 45 | +- A tool call needs approval |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +### Basic Usage |
| 50 | + |
| 51 | +Let the agent run until it's finished: |
| 52 | + |
| 53 | +```ts |
| 54 | +import { ToolLoopAgent, isLoopFinished } from 'ai'; |
| 55 | + |
| 56 | +const agent = new ToolLoopAgent({ |
| 57 | + model: yourModel, |
| 58 | + tools: yourTools, |
| 59 | + stopWhen: isLoopFinished(), |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +### Combining with Other Conditions |
| 64 | + |
| 65 | +You can combine `isLoopFinished()` with other conditions. Since `isLoopFinished()` never triggers, the other conditions still apply: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { ToolLoopAgent, isLoopFinished, hasToolCall } from 'ai'; |
| 69 | + |
| 70 | +const agent = new ToolLoopAgent({ |
| 71 | + model: yourModel, |
| 72 | + tools: yourTools, |
| 73 | + stopWhen: [isLoopFinished(), hasToolCall('finalAnswer')], |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +In practice, this does not make much sense in this context, since you could just omit `isLoopFinished()`. |
| 78 | + |
| 79 | +## See also |
| 80 | + |
| 81 | +- [`stepCountIs()`](/docs/reference/ai-sdk-core/step-count-is) |
| 82 | +- [`hasToolCall()`](/docs/reference/ai-sdk-core/has-tool-call) |
| 83 | +- [`ToolLoopAgent`](/docs/reference/ai-sdk-core/tool-loop-agent) |
0 commit comments