DEV Community

One 429, up to 34 paid retries: retry storms in multi-agent systems

A rate limit error should cost you a short wait. In an agent, it can cost far more.

Each retry of a model call sends the whole conversation again. When several agents share one rate limit, the provider says "slow down" to all of them at once, and one error turns into many paid retries.

How a retry storm starts

  • Retries stacked on retries. The official OpenAI and Anthropic SDKs already retry rate limit errors by default. Wrap that in your own loop of 3 tries, add the framework's own retries, and one failure becomes many real requests.
  • Every retry pays for the context again. The longer the run, the more each retry costs.
  • Everyone waits the same time. Ten agents that all wait exactly 2 seconds come back together and hit the limit again.
  • Each sub-agent retries alone. Five sub-agents with their own retry loops multiply the attempts by five.

What we measured

We tested five sub-agents against a fake service that always answers 429, 20 times over.

  • With a retry budget each, they made 30 to 34 calls between them.
  • Sharing one budget for the whole run, they made 11 calls, every time, and each stopped with a clear error.

The fix: one retry budget for the whole run

Four rules:

  • One place decides about retries. Turn off the SDK's own retries, or don't add yours on top. Not both.
  • Wait what you're told. Use the Retry-After header when it's there. Otherwise back off: 2, 4, 8 seconds.
  • Add jitter. A random 20% either way, so agents that failed together don't retry together.
  • Share one budget. The whole run, every sub-agent included, gets a fixed number of retries and a fixed amount of waiting. When it's spent, the run ends loudly.
import random, time

class RateLimited(Exception):
    def __init__(self, retry_after=None):
        self.retry_after = retry_after  # seconds, from the Retry-After header if there is one

class RetryBudgetSpent(Exception):
    pass

class RetryBudget:
    """One budget for the whole run, shared by every call and every sub-agent."""
    def __init__(self, max_retries=6, max_wait_sec=120):
        self.left, self.wait_left = max_retries, max_wait_sec

    def call(self, fn, *args, **kwargs):
        attempt = 0
        while True:
            try:
                return fn(*args, **kwargs)
            except RateLimited as e:
                attempt += 1
                wait = e.retry_after or min(60, 2 ** attempt)
                wait = wait * random.uniform(0.8, 1.2)  # jitter, so agents don't wake together
                if self.left <= 0 or wait > self.wait_left:
                    raise RetryBudgetSpent(f"gave up after {attempt} tries")
                self.left -= 1
                self.wait_left -= wait
                time.sleep(wait)

Wire it to the OpenAI client like this:

from openai import OpenAI, RateLimitError

client = OpenAI(max_retries=0)  # one place decides about retries: the budget below
budget = RetryBudget(max_retries=6)  # create once per run, pass it to every sub-agent

def ask_model(messages):
    try:
        return client.chat.completions.create(model="gpt-4o-mini", messages=messages)
    except RateLimitError as e:
        after = e.response.headers.get("retry-after")
        raise RateLimited(float(after) if after else None)

reply = budget.call(ask_model, messages)

With the real OpenAI client against a test server, it waited the 2 seconds the server asked for. A service that failed twice and then worked still went through on the third call.

Retry only what is safe to repeat

Retrying a model call is safe: at worst you pay twice. Retrying a tool call that sends an email or makes a refund is not, because the first try may have worked. Those need an idempotency key first.

What this simple version doesn't cover

  • Several processes. This budget lives in one process. Agents across machines need the counters in a shared store such as Redis.
  • Other errors. It only handles rate limits. Never retry a request the provider rejected as invalid.
  • Money. A budget of retries is not a budget of dollars. Pair it with a spend limit per run.

The full guide, with a pre-ship checklist, is here: https://www.steerplane.com/guides/ai-agent-retry-storm

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.