Best Practices:
1. Exponential Backoff: Retry with increasing delays
2. Queue Requests: Use a job queue for batch processing
3. Cache Results: Store frequently requested data
4. Monitor Usage: Track quota consumption proactively
Example Retry Logic:
import time
import requests
def research_with_retry(company: str, max_retries: int = 3) -> dict:
"""Research with exponential backoff on rate limit."""
for attempt in range(max_retries):
response = research_company(company, "user-123")
if response["success"]:
return response
if "rate limit" in response.get("error", "").lower():
wait_time = 2 ** attempt # Exponential backoff: 1s, 2s, 4s
print(f"Rate limited. Waiting {wait_time}s...")
time.sleep(wait_time)
continue
# Non-rate-limit error
return response
return {"success": False, "error": "Max retries exceeded"}