Lesson 18 / 29
Detecting Stuck and Runaway Agents
Stop loops early with step caps, repetition detection and budgets.
Fail fast when progress stops
Agents sometimes repeat the same failing action, oscillate between two fixes, or wander without progress, burning money. Put hard limits in the harness: a maximum number of steps, a token and cost budget, and a wall-clock timeout. Add progress checks: detect the same action with the same arguments repeated several times, test failures that do not change across attempts, or edits that undo each other. When a check trips, stop and report (what was tried, what the last error was) so a human can redirect, rather than letting the agent continue. Also give the agent a way to say "I'm blocked, here is why" instead of forcing a guess. Log every step so that stuck runs can be diagnosed afterwards, often revealing missing instructions or a flaky test.
Repetition detection, run
I ran this with plain Python 3 (standard library only), using a throwaway project created in a temporary folder. A healthy run repeats nothing more than twice. The looping run edits a.py three times within the last six actions and is flagged as stuck, which would stop the agent and ask a human.
from collections import Counter
def stuck(history, window=6, repeat=3):
"""Flag an agent that keeps repeating the same action with the same arguments."""
recent = history[-window:]
counts = Counter(recent)
action, n = counts.most_common(1)[0]
return (n >= repeat), action, n
ok_run = [("read", "a.py"), ("edit", "a.py"), ("test", ""), ("read", "b.py"), ("edit", "b.py"), ("test", "")]
bad_run = [("read", "a.py"), ("edit", "a.py"), ("test", ""), ("edit", "a.py"), ("test", ""), ("edit", "a.py")]
for name, run in (("healthy", ok_run), ("looping", bad_run)):
flag, action, n = stuck(run)
print(f"{name:8} stuck={flag} (most repeated: {action} x{n})")
Output:
healthy stuck=False (most repeated: ('test', '') x2)
looping stuck=True (most repeated: ('edit', 'a.py') x3)Let the agent say it is blocked
Allow an explicit "blocked, because..." outcome so it does not guess to avoid failing.
Quick check: What should happen when a stuck-agent check trips?
- Stop and report what was tried so a human can redirect
- Let it continue indefinitely
- Delete the tests
- Double the budget automatically
Answer
Stop and report what was tried so a human can redirect — Early stops save cost and surface the underlying problem.