๐ง๐ฎ๐บ๐ถ๐ป๐ด ๐๐ ๐๐ฃ๐ ๐ฅ๐ฎ๐๐ฒ ๐๐ถ๐บ๐ถ๐๐ ๐๐ถ๐๐ต ๐๐๐๐ป๐ฐ๐ถ๐ผ ๐ค๐๐ฒ๐๐ฒ๐
I spent three days fighting API rate limits.
My app hit 429 errors. I tried three things.
- First, I used sleep. It was too slow.
- Second, I used a Semaphore. It limits tasks, not requests per second.
- Third, I used retry loops. I spent too much time waiting.
I used a Token Bucket and an asyncio.Queue.
The bucket keeps you under the limit. The queue handles retries in the background. A semaphore prevents too many tasks.
This approach stops random 429 errors. The main flow stays fast. You get a predictable system.
Next steps for improvement:
- Add random jitter to avoid spikes.
- Add a max retry count.
- Use a lock-free method for high scale.
Skip this if:
- You make few calls.
- The API gives a Retry-After header.
- Your library handles retries.
Rate limits happen. Use a queue and a backoff strategy.
Source: https://dev.to/__c1b9e06dc90a7e0a676b/taming-ai-api-rate-limits-with-asyncio-queues-2a16