google-gemini/gemini-cli · warning

LOOP_DETECTED

LOOP_DETECTED

Error message

Loop detected, stopping execution

What it means

LOOP_DETECTED is emitted as a non-fatal (fatal:false) error event when the model's own loop detector trips (GeminiEventType.LoopDetected). The stream is not killed by this event alone; the translator surfaces 'INTERNAL' status and lets the session continue unless the session layer decides to stop.

Source

Thrown at packages/core/src/agent/event-translator.ts:180

      ensureStreamStart(state, out);
      out.push(
        makeEvent('agent_end', state, {
          reason: 'max_turns',
          data: {
            code: 'MAX_TURNS_EXCEEDED',
          },
        }),
      );
      break;

    case GeminiEventType.LoopDetected:
      ensureStreamStart(state, out);
      out.push(
        makeEvent('error', state, {
          status: 'INTERNAL',
          message: 'Loop detected, stopping execution',
          fatal: false,
          _meta: { code: 'LOOP_DETECTED' },
        }),
      );
      break;

    case GeminiEventType.ContextWindowWillOverflow:
      ensureStreamStart(state, out);
      out.push(
        makeEvent('error', state, {
          status: 'RESOURCE_EXHAUSTED',
          message: `Context window will overflow (estimated: ${event.value.estimatedRequestTokenCount}, remaining: ${event.value.remainingTokenCount})`,
          fatal: true,
        }),
      );
      break;

    case GeminiEventType.AgentExecutionStopped:
      ensureStreamStart(state, out);
      out.push(

View on GitHub (pinned to 5024443c72)

Solutions

  1. Rewrite the prompt to add a clear stopping condition or success criterion.
  2. Check the last few tool_request/tool_response pairs for identical repeats and fix the underlying tool.
  3. Retry the session; transient model-side loops sometimes clear on a fresh prompt.

Example fix

// before
await agent.run('fix all the bugs');
// after
await agent.run('Fix the failing test in src/auth.spec.ts only. Stop once `npm test` passes.');
Defensive patterns

Strategy: retry

Type guard

function isLoopDetected(ev: unknown): boolean {
  return typeof ev === 'object' && ev !== null && (ev as any)._meta?.code === 'LOOP_DETECTED';
}

Try / catch

// non-fatal error event; keep consuming the stream, decide whether to stop the session
if (ev.type === 'error' && ev._meta?.code === 'LOOP_DETECTED') {
  await session.abort(); // optional: break the loop deliberately
}

Prevention

When it happens

Trigger: translateEvent() receives GeminiEventType.LoopDetected -> pushes makeEvent('error', { status:'INTERNAL', message:'Loop detected, stopping execution', fatal:false, _meta.code:'LOOP_DETECTED' }).

Common situations: Agent repeatedly calls the same tool with identical args, or keeps producing the same partial output; flaky tool whose response oscillates; prompt that gives the model no exit condition.

Related errors


AI-assisted analysis of google-gemini/gemini-cli@5024443c72 (2026-08-12). Data as JSON: /api/errors/796c8173f0889407. Report an issue: GitHub.