{"record":{"id":"796c8173f0889407","repo":"google-gemini/gemini-cli","slug":"loop-detected","errorCode":"LOOP_DETECTED","errorMessage":"Loop detected, stopping execution","messagePattern":"Loop detected, stopping execution","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/core/src/agent/event-translator.ts","lineNumber":180,"sourceCode":"      ensureStreamStart(state, out);\n      out.push(\n        makeEvent('agent_end', state, {\n          reason: 'max_turns',\n          data: {\n            code: 'MAX_TURNS_EXCEEDED',\n          },\n        }),\n      );\n      break;\n\n    case GeminiEventType.LoopDetected:\n      ensureStreamStart(state, out);\n      out.push(\n        makeEvent('error', state, {\n          status: 'INTERNAL',\n          message: 'Loop detected, stopping execution',\n          fatal: false,\n          _meta: { code: 'LOOP_DETECTED' },\n        }),\n      );\n      break;\n\n    case GeminiEventType.ContextWindowWillOverflow:\n      ensureStreamStart(state, out);\n      out.push(\n        makeEvent('error', state, {\n          status: 'RESOURCE_EXHAUSTED',\n          message: `Context window will overflow (estimated: ${event.value.estimatedRequestTokenCount}, remaining: ${event.value.remainingTokenCount})`,\n          fatal: true,\n        }),\n      );\n      break;\n\n    case GeminiEventType.AgentExecutionStopped:\n      ensureStreamStart(state, out);\n      out.push(","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/google-gemini/gemini-cli/blob/5024443c7217464a66e98f80d73172a26440bd8f/packages/core/src/agent/event-translator.ts#L162-L198","documentation":"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.","triggerScenarios":"translateEvent() receives GeminiEventType.LoopDetected -> pushes makeEvent('error', { status:'INTERNAL', message:'Loop detected, stopping execution', fatal:false, _meta.code:'LOOP_DETECTED' }).","commonSituations":"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.","solutions":["Rewrite the prompt to add a clear stopping condition or success criterion.","Check the last few tool_request/tool_response pairs for identical repeats and fix the underlying tool.","Retry the session; transient model-side loops sometimes clear on a fresh prompt."],"exampleFix":"// before\nawait agent.run('fix all the bugs');\n// after\nawait agent.run('Fix the failing test in src/auth.spec.ts only. Stop once `npm test` passes.');","handlingStrategy":"retry","validationCode":null,"typeGuard":"function isLoopDetected(ev: unknown): boolean {\n  return typeof ev === 'object' && ev !== null && (ev as any)._meta?.code === 'LOOP_DETECTED';\n}","tryCatchPattern":"// non-fatal error event; keep consuming the stream, decide whether to stop the session\nif (ev.type === 'error' && ev._meta?.code === 'LOOP_DETECTED') {\n  await session.abort(); // optional: break the loop deliberately\n}","preventionTips":["Add explicit success criteria to prompts to give the model an exit.","Cap repeated identical tool calls client-side."],"tags":["agent-loop","loop-detection","gemini","typescript"],"backgroundTag":null,"analyzedSha":"5024443c7217464a66e98f80d73172a26440bd8f","analyzedAt":"2026-08-12T06:01:53.711Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}