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:
- Task is genuinely large — needs more iterations
- Agent is stuck — same path being retried with no progress
- 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:
| Pattern | Diagnosis | Fix |
|---|---|---|
| Same tool called 30+ times with similar args | Agent 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 question | Agent isn't synthesizing what it found | Use a stronger model (Opus over Sonnet) |
| Tool fails ~5 times then succeeds | Transient errors burned iterations | The retry budget should have caught this — file a bug |
| Agent ping-pongs between two tools | Logic error in the skill or a circular dependency | Refactor 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
- permissions-and-safety/loop-detector.md — loop detection (the related guard)
- tool-failures.md — fixing the errors that drive runaway iterations
- extending/skills.md — define multi-step apps