API Rate Limits and Polite Clients
Learn how API rate limits work, how to read the signals, and how to build a client that stays within its allowance.
Key takeaways
- Limits protect a shared service from overload and divide capacity fairly between customers.
- Providers count in different ways, so read whether limits apply per key, user, address or endpoint.
- On a
429, wait for the time given inRetry-After, or back off with increasing, randomized delays. - Caching, batching, filtering and webhooks usually reduce traffic far more than clever retry logic.
- Pace requests in your own code, and share that budget across all processes that use the same credential.
A rate limit is a cap on how much you may ask of an API in a given period, such as a number of requests per minute or a number of simultaneous connections. Go past it and the server refuses further calls for a while, usually with the status 429 Too Many Requests.
A polite client avoids hitting that wall. It knows its allowance, spaces out its calls, asks for each thing only once, and backs off when told to. Those habits make your own software more reliable too, because a job that respects limits finishes predictably instead of failing halfway.
Why services set limits
Every request costs the provider processing time, database work and bandwidth. Without limits, one script with a bug in its loop could slow the service for everyone. Limits also contain abuse, such as password guessing or bulk copying, and they support pricing tiers.
A limit isn't an accusation. It's the capacity you've been allotted, and designing for it from the start is far easier than retrofitting.
Ways limits are counted
The documentation may describe one of several schemes.
| Scheme | How it behaves | What to watch for |
|---|---|---|
| Fixed window | A count resets at set boundaries | Bursts on both sides of a reset |
| Sliding window | Counts the last N seconds continuously | No sudden refill |
| Token bucket | Allowance refills steadily; bursts spend saved tokens | Burst size and refill rate both matter |
| Concurrency limit | Caps requests in progress at once | Slow calls hold slots longer |
| Quota | A total per day or month | Running out early in the period |
An API can apply several at once: a per-second burst limit and a daily quota, for instance. Some weigh requests by cost, so one large search counts as many small reads.
The token bucket
The token bucket is worth understanding because both servers and clients use it. Picture a bucket that holds a fixed number of tokens and gains new ones at a steady rate. Each request removes one. If the bucket is empty, the request waits or is refused.
Say a bucket holds 20 and refills at 5 per second. After a quiet spell you can send 20 at once. After that you can sustain 5 per second. This allows short bursts while holding the long-term average down.
Reading the signals
The clearest signal is the status code 429, covered further in 429 Too Many Requests. Some services use 503 Service Unavailable when they're overloaded in general, and a few use 403 for exceeded quotas, so check the documentation.
A Retry-After header may accompany either. It holds a number of seconds or a date:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/json
{"error": "rate_limited", "message": "Request limit reached."} Many APIs also report your remaining allowance on successful responses, in headers with names such as X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset. These names are conventions, not a single standard, and the meaning of "reset" varies: some give a timestamp, others seconds from now. Read the provider's definition before writing code against them.
What to do when you're limited
Stop sending that kind of request. Retrying immediately usually fails, consumes resources and, with some providers, extends the penalty.
- If
Retry-Afteris present, wait at least that long. - If not, wait a short time, then double it after each further refusal, up to a ceiling.
- Add random variation, called jitter, so that many clients don't all return at the same instant.
- Give up after a sensible number of attempts and record the failure.
- For writes, confirm the request is safe to repeat before resending it.
The reasoning behind those steps is laid out in retries, backoff and idempotency, and a retry and backoff simulator lets you see how the delays add up.
Send fewer requests
The best way to handle a limit is to need less of it.
- Cache responses that don't change often. Honor
Cache-Control, and useETagwithIf-None-Matchso unchanged data costs little. Some APIs don't count304responses against the limit, but confirm that. - Filter on the server. Ask for records changed since your last run instead of downloading everything.
- Use larger pages within the documented maximum.
- Batch where the API offers a bulk operation.
- Request only needed fields if the API supports selection.
- Subscribe to events. Webhooks replace constant polling with a message when something happens.
- Combine duplicate work. If ten users ask for the same item at once, make one upstream call and share the result.
Pace yourself before the server does
Reacting to 429 is a fallback. A well-built client limits itself, slightly below the published allowance, using its own token bucket or a fixed delay between calls.
The tricky part is sharing. Limits usually apply to a credential, not to a process. If four workers each pace themselves to the full limit, together they send four times too much. Divide the budget among them, or keep a shared counter in a common store, or route all calls through one queue.
Priorities help too. A queue can let a customer waiting on screen go ahead of a nightly export. Spread scheduled jobs across the hour; many systems fire exactly on the hour, which is when shared services are busiest.
A worked example
Say an API allows 600 requests per minute and you need details for 12,000 products. At the full rate that's 20 minutes. You'd pace at perhaps 8 per second, which is 480 per minute and leaves room for your website's live calls, for a run of 25 minutes.
Then reduce the work. If the list endpoint returns 100 products per page with the fields you need, the whole job is 120 requests and takes seconds. If it also offers a "changed since" filter and only a few hundred items changed overnight, it's a handful of calls. The design change beats any amount of pacing.
Concurrency and timeouts
Rate and concurrency are different limits, and you can break one while respecting the other. If each call takes five seconds and you start four per second, about twenty are in flight at any moment. When the service slows down, that number climbs even though your start rate hasn't changed.
Cap the number of requests in progress with a worker pool or a semaphore, which is a counter that makes a task wait until a slot is free. Set a timeout on every call, so a stalled connection eventually releases its slot. Start with low concurrency and raise it only while response times and error rates stay steady.
Reusing connections matters as well. Most HTTP libraries can keep a connection open for several requests if you reuse the same client object, which saves repeated setup work on both ends.
Making long jobs survive interruptions
A job that runs for hours will eventually be interrupted by a deployment, a network fault or an exhausted quota. Save progress as you go, such as the last page cursor or the last processed ID, and write results in small batches. On restart, continue from the saved point instead of beginning again and spending the allowance twice.
Keep a short record of each run: requests sent, refusals received, time spent waiting and items completed. If a run that used to take twenty minutes now takes two hours, those numbers tell you whether the data grew, the limit changed or the service is struggling. Alert on a job that stops making progress, not on every single refusal, since an occasional 429 is normal for a client working near its allowance.
Being a good guest
Politeness goes beyond numbers. Identify your client with a descriptive User-Agent and a way to reach you if the provider asks for it. Follow the terms of service, including rules about storing or redistributing data. Don't create extra accounts or rotate keys or addresses to get around a cap; that typically breaks the terms and gets access removed. If you truly need more, ask for a higher tier and explain the use.
When you're automating requests against websites rather than an API, the same restraint applies: respect robots rules and keep the load low.
If you run the API
Publish your limits and return 429 with Retry-After and a readable error body. Show remaining allowance in headers. Limit by a sensible identity: per address alone punishes many people behind one office or mobile network. Consider separate, stricter limits for sign-in and other abuse-prone endpoints. An API gateway can enforce much of this centrally.
Watch who is being refused. A steady stream of 429s to a legitimate customer often means your documentation, your bulk endpoints or your limit needs attention. Log the key or account that hit the cap, which limit it was and how often, so support staff can answer the question quickly. When you lower a limit, announce it well ahead of the change, because other people's scheduled jobs were sized to the old number.