A burst of calls fails with a quota or rate-limit error.
Why this happens
The app exceeded request rate, token volume, or project thresholds. A rate limit requires throttling requests rather than immediate retries in a tight loop.
Answer 1: Add bounded exponential backoff
import random
import time
from google import genai
client = genai.Client()
def generate_with_backoff(max_retries=5):
for attempt in range(max_retries):
try:
return client.models.generate_content(
model="gemini-2.5-flash",
contents="Hi",
)
except Exception as exc:
if "429" not in str(exc) or attempt == max_retries - 1:
raise
time.sleep(min(30, 2 ** attempt) + random.uniform(0,
0.5))
response = generate_with_backoff()
For production systems, catch the explicit SDK exception class when available rather than parsing message strings.
