Skip to content

feat: implement thinking loop detection with automatic handler#15852

Closed
M0dEx wants to merge 2 commits intoanomalyco:devfrom
M0dEx:thinking-loop-detection
Closed

feat: implement thinking loop detection with automatic handler#15852
M0dEx wants to merge 2 commits intoanomalyco:devfrom
M0dEx:thinking-loop-detection

Conversation

@M0dEx
Copy link
Copy Markdown

@M0dEx M0dEx commented Mar 3, 2026

Issue for this PR

#12716 - "Doom loop is not caught when during reasoning or output"
The doom loop guard previously only caught tool call loops, not reasoning/output loops. This branch adds detection for repetitive reasoning blocks.
#14343 - "Kimi K2.5 frequently gets in a reiteration loop"
Kimi K2.5 specifically gets stuck repeating the same reasoning patterns. The thinking loop detector catches these repetitions and intervenes.
#15271 - "Models get stuck in reasoning loop - making changes only in thinking, not actually executing tool calls"
Models plan changes in their thinking block but never actually call tools. The detector identifies when reasoning becomes repetitive and sends a reminder or compacts context.
#3743 - "Loop in certain models"
General issue with models (including GLM) getting stuck in loops.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Some models (especially GLM-5 and Kimi K2.5) get stuck in reasoning loops where they repeat the same thoughts over and over without actually doing anything. This PR adds detection for that behavior.

When the model starts repeating a block of text (20-1000 chars) 3+ times in a row, we:

  1. Send a system reminder telling it to stop looping and take action
  2. If it keeps looping, compact the context to reset it
  3. If that fails, abort with an error

This is configurable via the experimental.thinking_loop config section. It's on by default since models that loop waste tokens and never complete tasks.

How did you verify your code works?

Tested on some workflows that caused the models to loop without it and saw successful nudging breaking the loops.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 3, 2026

The following comment was made by an LLM, it may be inaccurate:

Found a potential related PR:

@M0dEx M0dEx force-pushed the thinking-loop-detection branch 2 times, most recently from 6a0893b to 59f535a Compare March 3, 2026 19:46
@M0dEx M0dEx force-pushed the thinking-loop-detection branch from 551378b to c969329 Compare March 12, 2026 09:30
@M0dEx M0dEx force-pushed the thinking-loop-detection branch from c969329 to e0d801c Compare March 12, 2026 09:43
@bogzbonny
Copy link
Copy Markdown

Nice. Def want doc updates. Also It would be nice to see a comprehensive test which demonstrated the full workflow, I looked through the tests it didn't really look like that existed.

@M0dEx
Copy link
Copy Markdown
Author

M0dEx commented Apr 5, 2026

Nice. Def want doc updates. Also It would be nice to see a comprehensive test which demonstrated the full workflow, I looked through the tests it didn't really look like that existed.

Replaced by #21112, which implements this for text output as well, with integration tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants