Skip to content

feat: implement loop detection for thinking and text#21112

Open
M0dEx wants to merge 1 commit intoanomalyco:devfrom
M0dEx:loop-detection
Open

feat: implement loop detection for thinking and text#21112
M0dEx wants to merge 1 commit intoanomalyco:devfrom
M0dEx:loop-detection

Conversation

@M0dEx
Copy link
Copy Markdown

@M0dEx M0dEx commented Apr 5, 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 loops (reasoning or text ouput) 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 (10-2000 chars) twice in a row, we:

  1. Send a system reminder telling it to stop looping and take action
  2. If it keeps looping, and we ran out of nudges, abort with an error

This is configurable via the experimental.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?

Unit tests for the detector module (true/false positives, recovery escalation) and an integration test using a mock HTTP server that exercises the full nudge→abort flow. Also tested manually on workflows that previously caused 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 Apr 5, 2026

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

Potential Duplicate Found:

The current PR #21112 and PR #15852 both focus on implementing loop detection mechanisms for repetitive thinking/reasoning patterns and appear to be addressing the same underlying issues (#12716, #14343, #15271, #3743).

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.

1 participant