Open History to see the last commit
1---
2id: maxIterations
3title: maxIterations
4---
5
6# Function: maxIterations()
7
8```ts
9function maxIterations(max): AgentLoopStrategy;
10```
11
12Defined in: [packages/ai/src/activities/chat/agent-loop-strategies.ts:25](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/agent-loop-strategies.ts#L25)
13
14Creates a strategy that continues for a maximum number of **model turns**
15(iterations), not tool calls.
16
17One iteration can still emit many parallel tool calls. Prefer middleware
18with `onBeforeToolCall` / `onShouldContinue` when you need a tool-call
19budget (see the Agentic Cycle docs recipe).
20
21## Parameters
22
23### max
24
25`number`
26
27Maximum number of model turns to allow
28
29## Returns
30
31[`AgentLoopStrategy`](../type-aliases/AgentLoopStrategy.md)
32
33AgentLoopStrategy that stops after max iterations
34
35## Example
36
37```typescript
38const stream = chat({
39 adapter: openaiText(),
40 model: "gpt-4o",
41 messages: [...],
42 tools: [weatherTool],
43 agentLoopStrategy: maxIterations(3), // Max 3 model turns
44});
45```