Docs
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
Persistence (Advanced)
Embeddings
Reranking
Evaluate
Sandboxes (Advanced)
Class References
Function References
Interface References
Type Alias References
Variable References
Function References

maxIterations

ts
function maxIterations(max): AgentLoopStrategy;
function maxIterations(max): AgentLoopStrategy;

Defined in: packages/ai/src/activities/chat/agent-loop-strategies.ts:26

Creates a strategy that continues for a maximum number of model turns (iterations), not tool calls.

One iteration can still emit many parallel tool calls. For a tool-call budget, use middleware with onBeforeToolCall (per-turn cap) and onShouldContinue (cumulative run budget) — see the docs recipe under Agentic Cycle.

Parameters

max

number

Maximum number of model turns to allow

Returns

AgentLoopStrategy

AgentLoopStrategy that stops after max iterations

Example

typescript
const stream = chat({
  adapter: openaiText(),
  model: "gpt-4o",
  messages: [...],
  tools: [weatherTool],
  agentLoopStrategy: maxIterations(3), // Max 3 model turns
});
const stream = chat({
  adapter: openaiText(),
  model: "gpt-4o",
  messages: [...],
  tools: [weatherTool],
  agentLoopStrategy: maxIterations(3), // Max 3 model turns
});