Short answer
Give the whole run one retry budget, shared by every call and every sub-agent, instead of letting each call retry on its own. Wait for the time the Retry-After header asks for, add random jitter so agents don't retry together, and end the run with an error when the budget is used up. Don't add your own retry loop on top of the SDK's.
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, and when several agents or sub-agents share one limit, one error becomes many paid retries. Here is why it happens and a retry budget we tested that keeps it small.
How a retry storm starts
- Retries stacked on retries. The official OpenAI and Anthropic SDKs already retry rate limit and server errors a couple of times by default. Wrap that call in your own loop of 3 tries, and the framework adds its own, and one failure can become many real requests.
- Every retry pays for the context again. A retry is a new request with the full conversation. The longer the run, the more each retry costs.
- Everyone waits the same time. Ten agents that hit the limit together and all wait exactly 2 seconds come back together, and hit it again.
- Each sub-agent retries alone. An orchestrator with five sub-agents, each with its own retry loop, multiplies the attempts by five.
- The error looks like something else. If the framework reads a 429 as "the step failed", the agent may start the step over instead of waiting. That is a loop, and the loop guide applies too.
The fix: one retry budget for the whole run
Four rules, all in the code below:
- One place decides about retries. Turn off the SDK's own retries, or don't add your own on top. Not both.
- Wait what you are told. If the response has a Retry-After header, use it. Otherwise back off: 2, 4, 8 seconds and so on.
- Add jitter. A random 20% either way, so agents that failed together don't retry together.
- Share one budget. The whole run, including every sub-agent, gets a fixed number of retries and a fixed amount of waiting. When it is spent, the run ends with an error.
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 your model 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)We tested the budget on 5 December 2026, 20 times over, with a fake service that always answers 429. A single caller gave up after 6 or 7 calls, depending on the random jitter. Then five sub-agents each hit the limit: with a budget each, they made 30 to 34 calls between them; sharing one budget, they made 11 every time, and every one of them stopped with a clear error. A service that fails twice and then succeeds still went through on the third call, and with the real OpenAI client against a test server it waited the 2 seconds the server asked for.
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 before they can be retried. See duplicate actions.
What this simple version does not cover
- Several processes. The budget above lives in one process. Agents spread across machines need the counters in a shared store, such as Redis.
- Other errors. It only handles rate limits. Decide separately what to do with timeouts and server errors, and never retry a request the provider rejected as invalid.
- Spend. A budget of retries is not a budget of money. Pair it with a spend limit per run.
Doing it with SteerPlane
SteerPlane doesn't retry for you, and it doesn't replace a retry budget. What it adds is the limit around the run that a retry storm eventually hits: steps, time and spend. Install it with pip install steerplane:
from steerplane import guard
@guard(max_cost_usd=2.0, max_steps=40, max_runtime_sec=600)
def run_agent(task):
... # your agent, using the official OpenAI or Anthropic clientFrom version 1.2.0, @guard counts each call made with the official OpenAI and Anthropic clients and checks the limits before the call is sent. A failed call still counts as a step, so a run that keeps retrying reaches its step limit and stops with an error you can alert on. The setup is on the setup page.
A checklist before you ship an agent
- Exactly one layer retries model calls.
- Retries wait for Retry-After, or back off, with jitter.
- The whole run, sub-agents included, shares one retry budget.
- Actions that change something are never retried without an idempotency key.
- Every run also has a step limit and a spend limit.
Questions people ask
Does the OpenAI Python SDK retry 429 errors by itself?
Yes. The official OpenAI and Anthropic SDKs retry rate limit and server errors a couple of times by default, with backoff. If you also wrap the call in your own retry loop, each of your tries can turn into several real requests.
Why do retries cost so much for agents?
Each retry of a model call resends the whole conversation so far. Three retries on a long context pay for that context three more times, and get nothing back.
What is a circuit breaker for an AI agent?
A shared switch that stops all callers for a while after repeated failures, instead of letting each one keep retrying. A retry budget for the whole run is a simple version of it.