Performance: Handling rate limits and errors
This concept affects how quickly the application recovers from API rate limits and errors, impacting user interaction speed and reliability.
Jump into concepts and practice - no test required
import asyncio async def fetch_data(): response = await api_call() if response.status_code == 429: await asyncio.sleep(2) # wait before retry return await api_call() return response
async def fetch_data(): response = await api_call() if response.status_code == 429: # Just retry immediately return await api_call() return response
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Immediate retry on 429 error | Minimal | 0 | 0 | [X] Bad |
| Retry with delay (exponential backoff) | Minimal | 0 | 0 | [OK] Good |
import time
from langchain import Client
client = Client()
try:
response = client.call()
except RateLimitError:
print('Rate limit hit, retrying...')
time.sleep(2)
response = client.call()
print(response)try:
response = client.call()
except RateLimitError:
print('Rate limit hit')
client.call()
print(response)