Your agent needs a retry budget (or it'll loop until your API balance dies)
Your agent hits an error. It tries again. And again. And again. Six hours later, you wake up to a $300 API bill and a still-broken task.
The problem isn't that your agent retries — it's that it retries infinitely. Most agents have no concept of "I've tried enough times, this isn't working."
Here's the retry budget pattern that fixes this:
RETRY_BUDGET = {
"api_calls": 3,
"file_operations": 2,
"network_requests": 5,
"shell_commands": 2
}
CURRENT_RETRIES = {
"api_calls": 0,
"file_operations": 0,
"network_requests": 0,
"shell_commands": 0
}Before any operation, your agent checks: "Do I have budget left for this type of retry?" If not, it escalates to you instead of burning tokens in a loop.
The escalation ladder that saves your sanity:
- First failure: Retry with slight variation
- Second failure: Retry with different approach
- Third failure: Document what failed and ask for help
- Never: Retry the exact same thing 47 times
I learned this the hard way when my coding agent spent 4 hours trying to install a Python package that didn't exist. Same pip command, 847 times. The package name was wrong in character 3.
Warning: Claude's new Follow-a-Plan feature makes this worse. It'll retry failed steps indefinitely unless you build in budget limits.
The retry budget you actually need:
- API calls: 3 retries max (network issues resolve quickly or they don't)
- File operations: 2 retries max (permissions don't magically fix themselves)
- Shell commands: 2 retries max (syntax errors need fixing, not repeating)
- Code compilation: 1 retry max (same code won't suddenly work)
The key insight: Different failure types need different retry strategies. Network timeouts? Retry with backoff. Syntax errors? Fix the syntax, don't retry the same broken code.
Here's the pattern that actually works:
def attempt_with_budget(operation_type, operation_func, context):
if CURRENT_RETRIES[operation_type] >= RETRY_BUDGET[operation_type]:
return escalate_to_human(operation_type, context)
try:
result = operation_func()
CURRENT_RETRIES[operation_type] = 0 # Reset on success
return result
except Exception as e:
CURRENT_RETRIES[operation_type] += 1
if should_retry(e, operation_type):
return attempt_with_budget(operation_type, operation_func, context)
else:
return escalate_to_human(operation_type, context)The magic is in should_retry() — it looks at the error type. Network timeout? Retry. "File not found"? Don't retry, escalate.
Reset your budgets strategically:
- On successful completion of any operation
- When starting a new task
- When the human provides new information
- Never on a timer (that just enables more loops)
This pattern saved me $2,000 last month when my agent hit a misconfigured API endpoint. Instead of 10,000 failed requests, it made 3 attempts, documented the error, and asked me to check the endpoint URL.
Your agent should fail fast and escalate smart. Retry budgets make that possible.