gemini-cli - ✅(Solved) Fix Infinite Loop in sequentialthinking Tool leading to Quota Exhaustion [2 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#25671Fetched 2026-04-20 12:15:38
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
labeled ×1

The Gemini CLI enters an apparent infinite loop when using the sequentialthinking MCP tool. The agent continuously calls the tool with redundant or identical "thought" content, repeatedly cycling through thought numbers without progressing to a final answer. This behavior persists until the model's quota is completely exhausted, resulting in a 429 Too Many Requests error and TerminalQuotaError.

Error Message

✓ sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"thoughtNumber":5,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a … │ │ │ │ { │ │ "thoughtNumber": 5, │ │ "totalThoughts": 10, │ │ "nextThoughtNeeded": true, │ │ "branches": [], │ │ "thoughtHistoryLength": 1037 │ │ } ... ✓ sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a variety of tools a… │ │ │ │ { │ │ "thoughtNumber": 7, │ │ "totalThoughts": 10, │ │ "nextThoughtNeeded": true, │ │ "branches": [], │ │ "thoughtHistoryLength": 1039 │ │ } ... ✕ Error when talking to Gemini API ... TerminalQuotaError: You have exhausted your capacity on this model.

Root Cause

The Gemini CLI enters an apparent infinite loop when using the sequentialthinking MCP tool. The agent continuously calls the tool with redundant or identical "thought" content, repeatedly cycling through thought numbers without progressing to a final answer. This behavior persists until the model's quota is completely exhausted, resulting in a 429 Too Many Requests error and TerminalQuotaError.

PR fix notes

PR #1: fix(core): prevent infinite loops with hard cap on consecutive same-name tool calls

Description (problem / solution / changelog)

## Summary

This PR implements a hard cap for consecutive calls to the same tool name to prevent infinite loops, especially in "thinking" tools where arguments (like thought numbers) change each time. It also enhances the loop detection event to carry more details to the UI.

Details

  • Added CONSECUTIVE_SAME_TOOL_NAME_THRESHOLD (50) and THINKING_TOOL_LOOP_THRESHOLD (15) to LoopDetectionService.
  • Enhanced loop detection to track consecutive calls to the same tool name even if arguments differ.
  • Updated LoopDetected event to carry specific loop details ( ype, count, detail).
  • Improved UI alerting in useGeminiStream to display detailed information about the detected loop.
  • Added comprehensive unit tests for the new loop detection logic.

Related Issues

Fixes #25671

How to Validate

  1. Run pm test -w @google/gemini-cli-core -- loopDetectionService.test.ts to verify the logic.
  2. Trigger a loop in the CLI with a tool that changes arguments (e.g. sequentialthinking) and verify it stops after 15 calls with a detailed message.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Changed files

  • packages/cli/src/ui/hooks/useGeminiStream.ts (modified, +17/-4)
  • packages/core/src/core/client.ts (modified, +2/-2)
  • packages/core/src/core/turn.ts (modified, +7/-0)
  • packages/core/src/services/loopDetectionService.test.ts (modified, +25/-0)
  • packages/core/src/services/loopDetectionService.ts (modified, +46/-0)
  • packages/core/src/telemetry/types.ts (modified, +1/-0)

PR #25764: fix(core): prevent infinite loops with hard cap on consecutive same-name tool calls

Description (problem / solution / changelog)

## Summary

This PR implements a hard cap for consecutive calls to the same tool name to prevent infinite loops, especially in "thinking" tools where arguments (like thought numbers) change each time. It also enhances the loop detection event to carry more details to the UI.

Details

  • Added CONSECUTIVE_SAME_TOOL_NAME_THRESHOLD (50) and THINKING_TOOL_LOOP_THRESHOLD (15) to LoopDetectionService.
  • Enhanced loop detection to track consecutive calls to the same tool name even if arguments differ.
  • Updated LoopDetected event to carry specific loop details ( ype, count, detail).
  • Improved UI alerting in useGeminiStream to display detailed information about the detected loop.
  • Added comprehensive unit tests for the new loop detection logic.

Related Issues

Fixes #25671

How to Validate

  1. Run pm test -w @google/gemini-cli-core -- loopDetectionService.test.ts to verify the logic.
  2. Trigger a loop in the CLI with a tool that changes arguments (e.g. sequentialthinking) and verify it stops after 15 calls with a detailed message.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Changed files

  • packages/cli/src/ui/hooks/useGeminiStream.ts (modified, +17/-4)
  • packages/core/src/core/client.ts (modified, +2/-2)
  • packages/core/src/core/turn.ts (modified, +7/-0)
  • packages/core/src/services/loopDetectionService.test.ts (modified, +25/-0)
  • packages/core/src/services/loopDetectionService.ts (modified, +46/-0)
  • packages/core/src/telemetry/types.ts (modified, +2/-0)

Code Example

sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"thoughtNumber":5,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a … │
│                                                                                                                                                                                     │
{"thoughtNumber": 5,"totalThoughts": 10,"nextThoughtNeeded": true,"branches": [],"thoughtHistoryLength": 1037}
...
sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a variety of tools a… │
│                                                                                                                                                                                     │
{"thoughtNumber": 7,"totalThoughts": 10,"nextThoughtNeeded": true,"branches": [],"thoughtHistoryLength": 1039}
...
Error when talking to Gemini API ... TerminalQuotaError: You have exhausted your capacity on this model.
RAW_BUFFERClick to expand / collapse

Description

The Gemini CLI enters an apparent infinite loop when using the sequentialthinking MCP tool. The agent continuously calls the tool with redundant or identical "thought" content, repeatedly cycling through thought numbers without progressing to a final answer. This behavior persists until the model's quota is completely exhausted, resulting in a 429 Too Many Requests error and TerminalQuotaError.

Environment

Reproduction Steps / Observed Behavior

  1. The agent triggered the sequentialthinking tool to process a complex legal document update.
  2. After several successful file edits and shell commands, the agent entered a loop.
  3. The logs show the tool being called hundreds of times (up to thoughtHistoryLength: 1039).
  4. Most of these calls returned nextThoughtNeeded: true with a boilerplate "CRITICAL INSTRUCTION" message, but no actual progress was made.
  5. The loop was occasionally interrupted by "A potential loop was detected" and "Loop detection has been disabled", but it immediately resumed.
  6. The process ended only when the API returned: ✕ [API Error: You have exhausted your capacity on this model. Your quota will reset after 7h38m7s.].

Relevant Log Snippets

✓  sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"thoughtNumber":5,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a … │
│                                                                                                                                                                                     │
│ {                                                                                                                                                                                   │
│   "thoughtNumber": 5,                                                                                                                                                               │
│   "totalThoughts": 10,                                                                                                                                                              │
│   "nextThoughtNeeded": true,                                                                                                                                                        │
│   "branches": [],                                                                                                                                                                   │
│   "thoughtHistoryLength": 1037                                                                                                                                                      │
│ }
...
✓  sequentialthinking (sequential-thinking MCP Server) {"totalThoughts":10,"nextThoughtNeeded":true,"thought":"CRITICAL INSTRUCTION 1: You may have access to a variety of tools a… │
│                                                                                                                                                                                     │
│ {                                                                                                                                                                                   │
│   "thoughtNumber": 7,                                                                                                                                                               │
│   "totalThoughts": 10,                                                                                                                                                              │
│   "nextThoughtNeeded": true,                                                                                                                                                        │
│   "branches": [],                                                                                                                                                                   │
│   "thoughtHistoryLength": 1039                                                                                                                                                      │
│ }
...
✕  Error when talking to Gemini API ... TerminalQuotaError: You have exhausted your capacity on this model.

Expected Behavior

The sequentialthinking tool should facilitate complex reasoning without falling into self-referential infinite loops. The CLI should have a more robust mechanism to detect and stop such loops before they consume the entire user quota.

Suggested Fix

  • Improve the loop detection algorithm to recognize when sequentialthinking calls are not producing new insights or actions.
  • Implement a hard cap on the number of consecutive sequentialthinking calls allowed per turn.
  • Alert the user more aggressively when a thought loop is suspected.

extent analysis

TL;DR

Implementing a more robust loop detection mechanism and limiting consecutive sequentialthinking calls can help prevent infinite loops and quota exhaustion.

Guidance

  • Review the sequentialthinking tool's logic to identify why it's generating redundant or identical "thought" content, potentially indicating a flaw in the reasoning process.
  • Consider adding a counter to track consecutive calls with nextThoughtNeeded: true and no progress, triggering an interrupt or alert when a threshold is reached.
  • Evaluate the effectiveness of the current "A potential loop was detected" warning and explore ways to make it more proactive in stopping the loop.
  • Investigate if the thoughtHistoryLength can be used to determine when to stop the sequentialthinking process to prevent excessive iterations.

Example

No specific code example can be provided without modifying the existing sequentialthinking tool, but a potential approach could involve tracking the number of consecutive redundant calls:

let consecutiveRedundantCalls = 0;
// Within the sequentialthinking loop
if (nextThoughtNeeded && thoughtHistoryLength > 100) {
  consecutiveRedundantCalls++;
  if (consecutiveRedundantCalls > 10) {
    // Trigger alert or interrupt
  }
} else {
  consecutiveRedundantCalls = 0;
}

Notes

The provided log snippets and reproduction steps suggest a specific issue with the sequentialthinking tool, but without access to the tool's code or more detailed logs, it's challenging to provide a definitive fix. The suggested steps aim to guide toward a solution by identifying potential causes and mitigation strategies.

Recommendation

Apply a workaround by implementing a custom loop detection and interruption mechanism, as the root cause seems to be related to the sequentialthinking tool's logic and the Gemini CLI's loop detection capabilities. This approach allows for a more immediate mitigation of the issue while awaiting a potential fix in the tool or the CLI.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

gemini-cli - ✅(Solved) Fix Infinite Loop in sequentialthinking Tool leading to Quota Exhaustion [2 pull requests, 1 participants]