When a provider rate-limits you, the polite thing it does is tell you exactly how long to wait. For months on ramrom.io I ignored that, and it cost me quota, patience, and a few afternoons of staring at a log file wondering why a retry loop kept getting slapped down.
The problem was my retry loop. When a provider answered with a 429, my code waited a fixed five seconds and tried again. Then another five seconds, then another. It felt reasonable. It was not. The provider was not rate-limiting me for five seconds. It was rate-limiting me for sixty, or until the next minute boundary, or however long its window lasted — and my loop was hammering the door every five seconds, burning a request each time, and arriving at the exact moment the window reopened only by luck. Worse, every failed retry counted against my quota on some providers. I was paying for the privilege of being told no.
The fix was already sitting in the response, in a header I had never read: Retry-After. It is part of the HTTP spec, and most providers send it with a 429. It is either a number of seconds — Retry-After: 45 — or an HTTP date, which is just the same thing in a different outfit. When I started reading it, the whole retry loop got simpler, not cleverer.
Here is what the loop looks like now. I call the provider with cURL and keep the response headers, not just the body. If the status is 429, I look for Retry-After first. If it is a plain number, that is my wait in seconds. If it is a date, I parse it and subtract the current time to get the same number. If the header is missing — some providers send a 429 with no guidance at all — I fall back to a modest wait, and each attempt after that doubles it, up to a ceiling. Three attempts, then I stop and tell the visitor honestly that the provider is busy. No fourth try, no silent spin.
The difference in practice was immediate. A provider that used to eat four or five of my quota units on a single busy afternoon now eats one failed call and one successful one, because the retry lands after the window has actually reopened instead of somewhere in the middle of it. The visitor waits a little longer on the rare afternoon when the provider is busy, but they get their download instead of an error page. And my error log got quieter, which is how I know the fix is real — the same code, the same provider, far fewer 429s recorded.
There are two small traps worth knowing. First, trust the header but cap it. A provider can send a Retry-After of 3600 seconds, and you do not want a web request to sleep for an hour. I cap the wait at 30 seconds inside the request; if the header says longer, the retry loop stops and the visitor gets the honest "try again in a bit" message instead. A download tool has no business holding a connection open for minutes. Second, the header is not always seconds. I once treated a date value as a number and waited a very long time indeed, because PHP happily cast a date string to something unhelpful. Parse it properly: if it is not purely digits, treat it as a date.
The broader lesson is one I keep relearning on this site. The information I need is usually already in the response — the status code, the header, the error body — and the bug is that I was not reading it. Guessing a wait time felt like engineering. Reading the header is engineering. The provider already did the hard work of deciding when I may come back. All I had to do was ask it, and listen.
If you are writing a retry loop of your own, start by dumping the full headers of a failed response and reading them. You will probably find the answer printed right there, waiting for you to notice it.
