Skip to content

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

  1. Check your project's rate limits and quota for the model you call.
  2. Throttle with a queue so bursts don't all hit the limit at once.
  3. Retry 429 and 503 only; fix 400-level request errors instead of retrying them.
  4. Keep a fallback model for traffic that can't wait.
  5. Log every 429 with the model name and time so you can see patterns.

Related

Sources

All guides · All errors