Conversation
Implements a capability that monitors tool-call patterns via after_model_request and after_tool_execute hooks, detecting three scenarios: repeated identical calls, alternating A-B-A-B patterns, and no-op calls (same result returned). Configurable threshold and action (warn via ModelRetry or abort via StuckLoopError). Closes #71. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Prevents unbounded memory growth during long agent runs by discarding oldest entries (from the left) when lists exceed the configured limit. Defaults to 50 entries. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
| # --- Check for repeated identical calls --- | ||
| reason = self._check_repeated() | ||
| if reason is None: | ||
| reason = self._check_alternating() | ||
|
|
||
| if reason is not None: | ||
| self._trigger(reason) | ||
|
|
||
| return response |
There was a problem hiding this comment.
🚩 ModelRetry after detection does not clear history — could cause repeated warnings
When action='warn' and a loop is detected, ModelRetry is raised but _call_history is not cleared. If the model retries with the same call again, the history still contains the old repeated entries plus the new one, and detection will trigger again immediately. This creates a cycle of ModelRetry → same call → ModelRetry that will exhaust max_result_retries and abort the run. This is arguably the correct behavior (the agent genuinely is stuck), but it means action='warn' may effectively behave like action='error' if the model doesn't change strategy after the first retry. A design where the history is partially reset after a warning (to give the model a fresh chance) was presumably considered and rejected. This is not a bug but worth noting for documentation.
Was this helpful? React with 👍 or 👎 to provide feedback.
Note: Currently uses |
Audit vs prior art: StuckLoopDetectionWorth adding now:
Follow-up opportunities:
|
|
Noticed community alternatives is blank, vstorm just released their stuck loop detections as well, though it might be worth bringing to your attention. |
Summary
StuckLoopDetectioncapability that detects three patterns of repetitive agent behavior: repeated identical tool calls, alternating A-B-A-B call patterns, and no-op calls (same tool returning the same result)max_repeated_calls, default 3) and action (warnviaModelRetryorerrorviaStuckLoopError)for_run()for per-run state isolation so concurrent runs don't interfereCloses #71.
Test plan
🤖 Generated with Claude Code