Agents that don't quit: rate limits, memory, loops, and broken files
I run eleven AI apps. Most of them are agents — they read input, call a model, act on the result, repeat. And the #1 reason an agent run dies isn't bad prompts. It's one of four boring infrastructure failures: Rate l
I run eleven AI apps. Most of them are agents — they read input, call a model, act on the result, repeat. And the #1 reason an agent run dies isn't bad prompts. It's one of four boring infrastructure failures:
- Rate limits — the model API says slow down, the agent crashes instead.
- Memory blowups — context piles up until the window overflows.
- Endless loops — the same failing action repeats three, ten, fifty times.
- Broken files — a code edit goes wrong and there's no undo.
I built Unstuck to fix all four. It's stdlib-only Python, free, open-source, with green CI. Here's why each pain exists, and how to handle it without a framework.
Pain 1: Rate limits — "polite waiting" beats crashing
Most agent loops look like this:
while True:
response = llm.call(messages)
result = tool.execute(response.tool_call)
messages.append(result)
When the model API returns 429, the naive agent crashes. Retry logic is usually an afterthought, bolted on with time.sleep(60) — which blocks everything, or worse, no retry at all.
Unstuck's waiter treats rate limits as expected, not exceptional:
from src.waiter import PoliteWaiter
waiter = PoliteWaiter(max_retries=5, base_delay=2.0)
for attempt in waiter.retry():
try:
response = llm.call(messages)
break
except RateLimitError as e:
waiter.sleep(e.retry_after or waiter.next_delay())
Key details: it reads retry-after from the response when the API provides it, uses exponential backoff otherwise, and never retries forever. Five attempts, then it hands back control. A rate limit is a slowdown, not a crash.
Pain 2: Memory blowups — summarize, don't accumulate
Agent context windows are finite. Every tool result, every model response, every scraped page gets appended to messages. Eventually you overflow.
The common fix — truncating the oldest messages — throws away the system prompt and the user's original request, which are exactly what you need most.
Unstuck condenses old work instead:
from src.memory import SmartMemory
memory = SmartMemory(max_messages=12, summary_every=6)
# After every 6 exchanges, the oldest ones are summarized into one message:
# "User asked for a sentiment analysis of product reviews. Model identified
# three negative themes: shipping delays, packaging, and customer support.
# Tools used: web_scrape (5 calls), llm_classify (3 calls)."
The agent keeps working with a short, accurate summary instead of a giant transcript. You lose verbatim history, but you keep the decisions — which is what actually matters for the next step.
Pain 3: Endless loops — same action 3x, then stop
This is the most embarrassing failure mode: an agent tries to edit a file, the edit fails, it tries the same edit again, fails again, and repeats until the run is killed. I've watched agents burn through $2 of API credits on the same broken command.
Unstuck halts on repetition:
from src.loop_alarm import LoopAlarm
alarm = LoopAlarm(max_repeats=3)
while True:
action = agent.decide()
if alarm.seen(action):
alarm.halt("Same action repeated 3 times. Waiting for human.")
break
result = tool.execute(action)
alarm.record(action, result)
Three identical actions and the agent stops, hands control back, and asks. No infinite spins, no surprise bills.
Pain 4: Broken files — auto-save before every edit
Agents write code. Agents break code. Without checkpoints, a bad edit is permanent.
Unstuck checkpoints before every mutation:
from src.undo import CheckpointManager
checkpoints = CheckpointManager()
before = file.read()
checkpoints.save("edit-1", before)
try:
file.write(agent_edit(before))
except Exception:
file.write(checkpoints.restore("edit-1"))
raise
One click restores the last good state. No git knowledge required, no manual git checkout.
Using it
Unstuck is stdlib-only — no keys, no internet, no npm install:
python -m venv .venv
.venv\Scripts\activate
python examples/quickstart.py
Copy src/ into your project:
from src.waiter import PoliteWaiter
from src.memory import SmartMemory
from src.loop_alarm import LoopAlarm
from src.undo import CheckpointManager
That's the whole integration. Four classes, under 300 lines total.
What I'd add next
The roadmap: async waiter with a token-bucket queue (so parallel agents share a rate limit), embedding-based loop detection (similar actions, not just identical), and git-native checkpoints. All of these solve the same four pains at a larger scale.
But the core lesson is already there: agents don't need to be complex to be reliable. They need to handle rate limits politely, summarize memory, halt on loops, and undo bad edits. Everything else is bonus.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.