429 RESOURCE_EXHAUSTED
Cause: Your Gemini API requests went over a rate limit or quota for your project, so the API refused this one.
Copy-paste fix
Retry only 429 and 503, with exponential backoff and jitter, and stop after a few tries.
import random, time
from google import genai
from google.genai import errors
client = genai.Client()
def generate_with_backoff(model, contents, max_attempts=5, cap=60.0):
for attempt in range(max_attempts):
try:
return client.models.generate_content(model=model, contents=contents)
except errors.APIError as e:
if e.code not in (429, 503) or attempt == max_attempts - 1:
raise
time.sleep(random.uniform(0, min(cap, 2 ** attempt)))
Compare Gemini and other models' cost per call on TokenCenter
What the error means
Google's Gemini troubleshooting page names 429 RESOURCE_EXHAUSTED and 503 UNAVAILABLE as errors you should retry, and recommends exponential backoff.
In the google-genai Python SDK, 4xx responses raise errors.ClientError and 5xx raise errors.ServerError; both carry the HTTP status in .code.
Checklist
- Check your project's rate limits and quota for the model you call.
- Throttle with a queue so bursts don't all hit the limit at once.
- Retry 429 and 503 only; fix 400-level request errors instead of retrying them.
- Keep a fallback model for traffic that can't wait.
- Log every 429 with the model name and time so you can see patterns.
Related
- 429 Too Many Requests (rate_limit_exceeded)
- 529 overloaded_error (Claude API)
- Failure mode: Retry and cost multipliers
- ModelRaters list: Best for fast and low cost