hermes - ✅(Solved) Fix [Bug]: Critical: Tool Call Loop Failure - Gets Stuck in Infinite Loops on Simple Errors [1 pull requests]

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…

Error Message

  1. Error Pattern Recognition: Cannot identify simple syntax errors after multiple attempts
  2. Introduce a simple syntax error (e.g., quotes in strings) Should recognize error pattern after 2-3 attempts

Additional Logs / Traceback (optional)

Root Cause

Root Cause Analysis (optional)

PR fix notes

PR #13549: feat: tool loop detection and intervention

Description (problem / solution / changelog)

What does this PR do?

Small local models can get trapped in tool-calling loops when they miss key feedback in tool results. A model might call web_search(query="foo"), get "no results found", then call it again with the same args on the next turn, and keep repeating until all iterations are exhausted. The existing _deduplicate_tool_calls() only catches repeats within a single turn — it doesn't see cross-turn loops.

This adds cross-turn (tool_name, args_hash) tracking that detects when the model repeats the same tool call with the same arguments across turns. An escalation ladder progressively intervenes:

Repeat CountAction
3rdLevel 1 nudge: "You called {tool} on the previous turn and received this result: {preview}. Please process this result instead of calling again."
4thLevel 2 stronger nudge: "You have called {tool} {N} times consecutively without making progress. Do NOT call this tool again with the same arguments."
5thBreak + re-prompt: inject synthetic user message + last tool result, give the model one more structured chance
6thHard stop: terminate the loop, exit the agent loop with reason tool_loop_terminated

Related Issue

Inspired by NousResearch/hermes-agent#481 — "Tool-Call Loop Guard" proposal. This PR implements Phase 1 (basic repetition detection with SHA-256 hashing and escalation ladder). Phase 2 features (ping-pong detection, exempt tools list, context truncation recovery) are not included.

Addresses NousResearch/hermes-agent#13208 — the four core flaws identified there (loop detection failure, error pattern recognition, state persistence loss, resource waste) are directly addressed by the escalation ladder and re-prompt mechanism.

Type of Change

  • ✨ New feature (non-breaking change that adds functionality)
  • ✅ Tests (adding or improving test coverage)

Changes Made

  • run_agent.py — added _hash_tool_args() and _check_tool_loop() methods, integrated into the main loop after _execute_tool_calls()
  • hermes_cli/config.py — added agent.loop_detection config block with enabled, max_repeats_before_nudge, max_repeats_before_reprompt, max_repeats_before_stop
  • tests/run_agent/test_tool_loop_detection.py — 24 unit tests covering hash normalization, full escalation ladder, independent counters, empty result exclusion, multi-tool turns, and edge cases

How to Test

  1. Run the test suite: pytest tests/run_agent/test_tool_loop_detection.py -v — all 24 tests pass
  2. Test with a small model that loops on repeated tool calls — the escalation should trigger at the configured thresholds
  3. Verify config overrides work: set agent.loop_detection.max_repeats_before_nudge: 1 in ~/.hermes/config.yaml and confirm nudge triggers on the 2nd call

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass (55 pre-existing failures unrelated to this change; 14083 passed)
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Ubuntu 24.04

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — docstrings added to both new methods
  • I've updated cli-config.yaml.example if I added/changed config keys — defaults in DEFAULT_CONFIG are sufficient, consistent with other auxiliary config sections
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — no platform-specific code
  • I've updated tool descriptions/schemas if I changed tool behavior — N/A

Screenshots / Logs

When the loop detection triggers, the agent emits status messages like:

⚠️ Model repeated `web_search` — nudging to process results
⚠️ Model still repeating `web_search` — stronger warning
⚠️ Model stuck in tool loop — breaking for re-prompt
❌ Model entered tool loop calling `web_search` 6 times — terminated

Changed files

  • hermes_cli/config.py (modified, +9/-0)
  • run_agent.py (modified, +220/-0)
  • tests/run_agent/test_tool_loop_detection.py (added, +351/-0)

Code Example

Report     https://paste.rs/xrdi2
agent.log  https://paste.rs/oAR3m

---
RAW_BUFFERClick to expand / collapse

Bug Description

Issue Summary: Hermes Agent gets stuck in infinite tool call loops, repeatedly attempting the same failed operation (e.g., 90+ iterations on simple syntax errors).

Key Problems Identified:
1. Loop Detection Failure: No mechanism to detect repeated failures
2. Error Pattern Recognition: Cannot identify simple syntax errors after multiple attempts
3. State Persistence: Loses track of what has already been tried
4. Resource Waste: 90+ tool calls for problems that should take 1-2 attempts

Steps to Reproduce

  1. Ask for any task involving string formatting or syntax
  2. Introduce a simple syntax error (e.g., quotes in strings)
  3. Observe repeated identical tool calls without progress

Expected Behavior

Should recognize error pattern after 2-3 attempts Should apply fix immediately (f-strings, proper escaping) Should break out of loop and move to solution

Actual Behavior

Gets stuck in infinite loop Repeats same failed approach Wastes 90+ tool calls on simple problems Fails to learn from repeated failures

Affected Component

Tools (terminal, file ops, web, code execution, etc.)

Messaging Platform (if gateway-related)

N/A (CLI only)

Debug Report

Report     https://paste.rs/xrdi2
agent.log  https://paste.rs/oAR3m

Operating System

Windows 11 Pro with WSL2 (Ubuntu 24.04.4 LTS)

Python Version

3.12.3

Hermes Version

v0.10.0 (2026.4.16)

Additional Logs / Traceback (optional)

Root Cause Analysis (optional)

No response

Proposed Fix (optional)

No response

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

extent analysis

TL;DR

Implement a loop detection mechanism to recognize and break out of infinite tool call loops after a specified number of failed attempts.

Guidance

  • Introduce a counter to track the number of attempts for each task, and break out of the loop after a certain threshold (e.g., 3-5 attempts).
  • Develop an error pattern recognition system to identify simple syntax errors, such as quotes in strings, and apply fixes immediately.
  • Implement state persistence to keep track of what has already been tried, and avoid repeating the same failed approach.
  • Analyze the provided debug reports (https://paste.rs/xrdi2 and https://paste.rs/oAR3m) to gain insights into the current behavior and identify potential areas for improvement.

Example

# Pseudocode example of a loop detection mechanism
max_attempts = 3
attempt_count = 0

while attempt_count < max_attempts:
    # Perform tool call
    if tool_call_successful:
        break
    attempt_count += 1

if attempt_count == max_attempts:
    # Break out of loop and apply alternative approach
    print("Max attempts reached. Applying alternative approach.")

Notes

The proposed fix may require significant changes to the Hermes Agent's architecture and logic. It is essential to thoroughly test and validate any modifications to ensure they do not introduce new issues or regressions.

Recommendation

Apply workaround: Implement a loop detection mechanism to prevent infinite tool call loops, as this is a critical issue that can cause significant resource waste and negatively impact the user experience. This workaround can be implemented while a more comprehensive solution is developed.

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

hermes - ✅(Solved) Fix [Bug]: Critical: Tool Call Loop Failure - Gets Stuck in Infinite Loops on Simple Errors [1 pull requests]