Request a demo
Docs / Troubleshooting / Max iterations reached

Max iterations reached

[Agent stopped after 25 iterations without completing the task.]

InsightWorker caps each run at 25 model/tool round trips by default (MAX_AGENT_ITERATIONS). Hitting the cap means one of:

  1. Task is genuinely large — needs more iterations
  2. Agent is stuck — same path being retried with no progress
  3. Tool returning slowly — agent taking many small steps when one big step would work

Diagnose first, raise second

Before raising MAX_AGENT_ITERATIONS, look at why it took that many steps. The stop message lists the most recent tool calls and how often each was made; in the REPL you can also scroll back through the tool calls printed during the run.

Patterns to look for:

PatternDiagnosisFix
Same tool called 30+ times with similar argsAgent is exploring (e.g. searching SharePoint with many slight variations)Narrow the prompt; pre-load relevant context
Tool calls succeed but the agent re-asks the same questionAgent isn't synthesizing what it foundUse a stronger model (Opus over Sonnet)
Tool fails ~5 times then succeedsTransient errors burned iterationsThe retry budget should have caught this — file a bug
Agent ping-pongs between two toolsLogic error in the skill or a circular dependencyRefactor the skill

Raise the cap

If you've confirmed the task is just large, raise the cap:

# ~/.insightworker/.env
MAX_AGENT_ITERATIONS=80

For very large apps (full code migrations, multi-day digests), 100-150 is reasonable. Above that, break the work into several runs.

Break the task into smaller steps

The cleanest fix for a complex run is to split it. Two patterns:

Phase by phase

Run each phase as its own one-shot prompt, in sequence:

insightworker -c "Gather the data and save it to work/data.json"
insightworker -c "Analyse work/data.json and save findings to work/findings.md"
insightworker -c "Write the report from work/findings.md"

Plan-then-execute

Have the first run produce a plan file; the second run executes it:

# Run 1: produces /tmp/plan.json
insightworker -c "Plan the migration of foo to bar; output JSON to /tmp/plan.json"

# Run 2: executes the plan
insightworker -c "Execute the plan in /tmp/plan.json step by step"

Each run starts fresh with full iteration budget.

See also