Skip to content

Agent enters infinite repetition loop — user interventions are drowned in context window Description #1672

@RO-mix

Description

@RO-mix

Agent Zero Looping Bug Report
Title

Agent enters infinite repetition loop — user interventions are drowned in context window
Description

When Agent Zero is executing a multi-step task (especially with subordinate agents or multiple tool calls), the agent can enter a state where it repeatedly generates the same response/tool-call sequence. When the user sends a new message to intervene, the agent appears to "ignore" it and continues looping.

This is not a UI or messaging bug — the intervention mechanism at the code level works correctly. The root cause is architectural: the user's message gets buried in a long context window, and the model's attention mechanism fails to prioritize it over recent tool results.
Environment

Agent Zero version: latest (2025-05-27)
Model: Claude 3.5 Sonnet / Kimi K2.6 (affects all models)
Context: Multi-step tasks with 5+ tool calls

Steps to Reproduce

Start a complex multi-step task (e.g., "Research X, then implement Y, then test Z")
Let the agent run through several iterations
Observe the agent repeating the same tool call or response
Send an intervention message: "Stop, I want to change direction"
Expected: Agent acknowledges the new direction and changes behavior
Actual: Agent continues the previous loop as if the message wasn't sent

Root Cause Analysis

  1. Intervention Mechanism Works, But Context Drowns It

The handle_intervention() method (agent.py:842) correctly:

Detects self.intervention flag set by communicate()
Adds the user message to history via hist_add_user_message()
Raises InterventionException which is caught and suppressed by extension _40_handle_intervention_exception.py

However, after the intervention is processed, the inner while True: message loop continues. On the next iteration:

The prompt now contains: system prompt + 15+ tool results + warning messages + user's intervention
The user's message is in the middle of a very long context
Model attention favors the beginning (system prompt) and end (recent tool results)
The user's message is effectively "invisible" to the model
  1. No Circuit Breaker on Repetition

In agent.py:490-502, when the agent detects a repeated response:

if (self.loop_data.last_response == agent_response):
warning_msg = self.read_prompt("fw.msg_repeat.md")
wmsg = self.hist_add_warning(message=warning_msg)
PrintStyle(font_color="orange", padding=True).print(warning_msg)
# ❌ Loop continues without forced behavior change

The system warns about repetition but does not:

Force a different action
Return control to the user
Enter a recovery mode
Truncate or restructure the context
  1. Missing Awareness Step

Before each LLM call, there is no explicit "What did the user say most recently?" check. The agent treats all history entries equally, without prioritizing recent user intent.
Proposed Solutions
Short-term (Extension-based, non-breaking)

Create an extension that:

Tracks last N responses in agent.data
Detects exact-match repetition after 2 consecutive identical responses
Forces a recovery action: prepend a system message like "STOP REPEATING. Ask user for clarification."
Tracks iteration count and enforces a maximum (e.g., 50 iterations per monologue)

Medium-term (Core change)

Add an Awareness Layer before each prepare_prompt() call
Extract the most recent user message and prepend it to the system prompt with high priority
Implement a Circuit Breaker with states: normal → warning → recovery → halt
After 3 repetitions, automatically return control to user with summary

Long-term (Architectural)

Replace the reactive while True: loop with a state machine
Add bounded execution with explicit depth limits
Implement working memory separate from conversation history
Add meta-cognitive reflection step before each action

Attachments

Screenshot of looping behavior: [attached]
Related research: technical-agent-looping-research-2026-05-27.md

Labels

bug, looping, context-window, intervention, priority-high

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions