Request a demo
Docs / Permissions & safety / Loop detector & iteration safety

Loop detector & iteration safety

An agent can get stuck — same tool, same error, over and over. InsightWorker has three guards that stop a run before it uses up your token budget.

Guard 1: maximum iterations per run

Each run has a ceiling on the number of model/tool round trips. Default: 25.

# In ~/.insightworker/.env — raise for long multi-step work:
MAX_AGENT_ITERATIONS=60

When the cap is reached the agent stops and prints:

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

followed by a count of the most recent tool calls and suggestions for what to try. See Max iterations reached.

Guard 2: loop detector

The agent stops early when either of these happens three times in a row:

  • the same tool is called with identical input, or
  • the same tool returns an error, whatever the input.

The message looks like:

[Loop detected: jira_search was called 3 times in a row without progress. Stopping early.]

with the last error underneath. This catches the most common runaway pattern — an authentication error retried again and again. Fix the underlying error and run again. The detector is always on; there is no setting to disable it.

Guard 3: per-tool output cap

The result of a single tool call is capped at about 10% of the active model's context window, so one noisy tool cannot crowd out the rest of the conversation.

Context windowOutput cap
128k tokens~51,000 chars
200k tokens~80,000 chars
1M tokens~400,000 chars
Unknown model (assumed 100k)~40,000 chars

To set a fixed cap for every tool, whatever the model:

MAX_TOOL_OUTPUT_LENGTH=20000   # chars, not tokens

When output is cut, the agent sees a marker such as:

… [search_files: output truncated at 80000 chars (sized for active model); narrow your query for full results]

For tools that return long lists (sharepoint_search, jira_search), narrowing the query works better than raising the cap.

See also