# प्रगति न होना पकड़ना — Agent Loops, Stop Conditions और Token Budgets

Source: https://www.geekswithgeeks.com/hi/agent-loops/stop-no-progress

> हाल के tool calls याद रखने वाले छोटे guard से दोहराव और ऊपर-नीचे होना पकड़ें।

## व्यस्त पर अटका

Agent हर सीमा के भीतर रहकर भी कोई प्रगति न कर सकता है, जैसे वही tool उन्हीं arguments के साथ बार-बार बुलाना, या दो अवस्थाओं के बीच पलटना। सबसे सरल guard पिछले कुछ calls याद रखता है और जब **वही call** कई बार दोहराया जाए तो रुक जाता है।

## दोहराव guard

यह जैसा दिखाया गया वैसा ही चला। वही `read_file` call तीसरी बार guard को ट्रिगर करता है। `sort_keys=True` से arguments, key क्रम की परवाह किए बिना बराबर माने जाते हैं।

```python
from collections import deque
import json

class RepeatGuard:
    def __init__(self, window=6, limit=3):
        self.recent = deque(maxlen=window)
        self.limit = limit

    def seen_too_often(self, name, args):
        key = (name, json.dumps(args, sort_keys=True))
        self.recent.append(key)
        return self.recent.count(key) >= self.limit

g = RepeatGuard()
for _ in range(3):
    print(g.seen_too_often("read_file", {"path": "a.txt"}))
```

Output:

```
False
False
True
```

## कुछ दोहराव ठीक हैं

किसी job की स्थिति poll करना वैध रूप से call दोहरा सकता है। बड़ी सीमा या calls के बीच प्रतीक्षा से अनुमति दें, पर कुल संख्या फिर भी सीमित रखें।

**Quiz:** RepeatGuard क्या पकड़ता है?

- [ ] धीमी network गति
- [x] वही tool call उन्हीं arguments के साथ दोहराना
- [ ] वर्तनी की ग़लतियाँ
- [ ] ग़ायब API keys

*Answer:* वही tool call उन्हीं arguments के साथ दोहराना. यह हाल के एक-जैसे calls गिनता है और कहीं न पहुँचते loop को चिह्नित करता है।
