langchain - 💡(How to fix) Fix Progress-aware termination: detect no-progress loops in agent tool execution [4 comments, 3 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
langchain-ai/langchain#36139Fetched 2026-04-08 01:08:00
View on GitHub
Comments
4
Participants
3
Timeline
13
Reactions
1
Timeline (top)
commented ×3labeled ×3mentioned ×3subscribed ×3

Error Message

Add an optional progress-aware termination guard that detects when an agent is not making progress (e.g., repeating the same tool call + same error) and stops execution early.

  • same tool + same args + same error

Root Cause

  • Iteration limits: stop late, not early
  • Fix root causes: incomplete; failures still recur
  • Tool call limits: count-based, not pattern-based

Code Example

search_db({invalid_args})ValidationError
search_db({invalid_args})ValidationError
search_db({invalid_args})ValidationError
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

Add an optional progress-aware termination guard that detects when an agent is not making progress (e.g., repeating the same tool call + same error) and stops execution early.

Current safeguards (recursion/tool-call limits) only cap total steps. They do not detect stuck states.

Use Case

Observed patterns in existing issues:

  • repeated invalid tool calls (#7440, #22545)
  • repeated validation failures (#35514)
  • repeated tool use without new results (#26019, #20693)

Example:

search_db({invalid_args}) → ValidationError
search_db({invalid_args}) → ValidationError
search_db({invalid_args}) → ValidationError

The agent makes no progress but continues until max_iterations.

Proposed Solution

Introduce a middleware (e.g., ProgressGuardMiddleware) that:

Detects no-progress patterns:

  • same tool + same args + same error
  • repeated identical actions or outputs
  • no new intermediate state

Behavior:

  • track last N steps
  • if no change for K steps → terminate early
  • return reason: no_progress_detected

Config:

  • window size (N)
  • threshold (K)
  • strategy: terminate / fallback / message

Alternatives Considered

Alternatives Considered

  • Iteration limits: stop late, not early
  • Fix root causes: incomplete; failures still recur
  • Tool call limits: count-based, not pattern-based

Additional Context

Related issues:

  • #7440, #22545, #35514 → repeated failures
  • #26019, #20693 → repeated non-productive calls

Common pattern: agent continues despite no progress

This adds a pattern-based guard to complement existing limits.

extent analysis

Fix Plan

To implement the progress-aware termination guard, we will introduce a ProgressGuardMiddleware that detects no-progress patterns and terminates the agent early.

Step-by-Step Solution

  1. Create the ProgressGuardMiddleware class:

class ProgressGuardMiddleware: def init(self, window_size, threshold, strategy): self.window_size = window_size self.threshold = threshold self.strategy = strategy self.step_history = []

2. **Implement the `detect_no_progress` method**:
```python
def detect_no_progress(self, current_step):
 self.step_history.append(current_step)
 if len(self.step_history) > self.window_size:
     self.step_history.pop(0)
 if len(self.step_history) >= self.threshold:
     last_steps = self.step_history[-self.threshold:]
     if all(step == current_step for step in last_steps):
         return True
 return False
  1. Integrate the ProgressGuardMiddleware with the agent:

def execute_agent(agent, max_iterations): progress_guard = ProgressGuardMiddleware(window_size=5, threshold=3, strategy="terminate") for i in range(max_iterations): current_step = agent.execute() if progress_guard.detect_no_progress(current_step): # Terminate the agent early return "no_progress_detected" return "max_iterations_reached"

4. **Configure the `ProgressGuardMiddleware`**:
* Set the `window_size` to control the number of previous steps to track.
* Set the `threshold` to determine the number of consecutive identical steps required to detect no progress.
* Set the `strategy` to choose the behavior when no progress is detected (e.g., "terminate", "fallback", or "message").

### Verification
To verify that the fix worked, you can test the `ProgressGuardMiddleware` with different scenarios, such as:

* Repeated invalid tool calls with the same error.
* Repeated validation failures.
* Repeated tool use without new results.

Check that the agent terminates early and returns the "no_progress_detected" reason when no progress is detected.

### Extra Tips
* Adjust the `window_size` and `threshold` parameters to balance between detecting no-progress patterns and allowing the agent to explore different solutions.
* Consider adding additional strategies for handling no-progress situations, such as falling back to a previous state or sending a message to the user.
* Monitor the agent's behavior and adjust the `ProgressGuardMiddleware` configuration as needed to ensure optimal performance.

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