Dev.to WebDev 🛠 Dev 👁 0 📖 7 min read

Rate limits are per account, not per key

The first thing most teams do when they hit a rate limit is create another API key. It is a reasonable guess: on plenty of services, keys are the unit of throttling, so a second key is a second budget. Here it does nothi

The first thing most teams do when they hit a rate limit is create another API key. It is a reasonable guess: on plenty of services, keys are the unit of throttling, so a second key is a second budget. Here it does nothing at all. Every key, every product and every gateway on one account draws from a single requests-per-second allowance, and the API keys page says it in one line: creating more keys does not increase it, checked 2026-11-24.

That sentence is worth sitting with, because it changes what the client has to be. If the limit cannot be multiplied by minting credentials, it has to be respected by something that can see the whole account's traffic. That thing is your code.

One account, one number

A request is counted against the account's requests per second regardless of which key signed it. Ten workers with ten keys pointing at ten different endpoints is still one bucket. The practical consequence is not subtle: adding a worker to go faster does not add throughput, it adds contention for a number that did not move.

There is a second-order effect that costs more than the slowdown. When several workers share an invisible budget, each of them individually behaves correctly while the account as a whole is over the limit. The failure lands on whichever request happened to be unlucky, so the logs show random refusals across services that each looked fine in isolation. Chasing that as a bug in one worker is a day lost.

The limit goes up with money, not with keys

The allowance is raised by cumulative eligible top-ups, in USD, after the payment is confirmed as credited (checked 2026-11-24, rate limit docs):

Cumulative eligible top-ups Account QPS becomes at least
under $3 unchanged
≥ $3 2
≥ $50 5
≥ $100 10
≥ $200 15
≥ $1,000 50
≥ $10,000 500

Three details in that table do most of the work. Only credit-pack purchases and automatic top-ups count, and only once they are confirmed as credited; subscriptions, gifts and gift codes do not count, and a credit balance sitting in the account is not the same as USD paid. Non-USD payments are not counted as USD without conversion. Upgrades only raise the limit and never lower an existing higher one.

The docs are also blunt about the ceiling, and the sentence is worth quoting because it is the one to bring to a meeting where someone asks for 200 requests per second on a $50 balance: this is not unlimited QPS.

What a refusal costs you

A request over the limit is refused immediately with status 1029 and a Retry-After header, and it is not charged (checked 2026-11-24). So the over-limit failure is the cheap kind. The response tells you how long to wait, and the attempt cost nothing.

That is the opposite of the expensive failure, which is retrying a request that was never going to work. A 1000 for a bad parameter, a 1001 for a bad key, a 1020 for an empty balance: none of those get better with patience, and a retry loop that treats every non-zero status the same will spend your rate limit on them. The header X-SerpBase-Retryable exists to settle that question without parsing message text, but you can also just decide it in code, which is what the queue below does.

Brake before you are refused

Every response carries X-RateLimit-Limit, the account's allowed rate, and X-RateLimit-Remaining, what is left in the current second. Those two headers are the difference between a client that discovers the limit by being refused and one that never reaches it.

The rule that matters: when Remaining reads 0, the next request in that same second will be refused. Sleeping a second costs you a second. Not sleeping costs you a round trip that returns nothing, plus the retry. On a batch job the difference is measured in minutes over an hour.

One thing to get right about the shape of the limit is that it is per second, not per minute. A burst of ten requests at the top of a minute looks identical to a steady stream to a per-minute budget and completely different to a per-second limiter. If your queue was designed around a per-minute allowance, it will pass a small test and fall over on a real backfill.

Checking the balance does not spend anything

GET /account/credits returns the balance on the account that owns the key, with permanent_credits and expiring_credits separated and expiring_expires_at as a Unix timestamp, 0 when nothing is expiring (checked 2026-11-24, API keys page). It costs 0 credits, so polling it is free in the currency you care about.

It is not free in the other currency. The endpoint is limited to 60 requests per minute per account, which is generous for a health check and far too low to call before every search. Wire it to a scheduled check, not to the request path.

The key goes in the X-API-Key header. That is not a style preference: the API keys page states that keys in the URL are refused, so ?api_key= in a query string will not authenticate, whatever a friendly error message elsewhere might suggest (checked 2026-11-24).

A queue that belongs on the client

The whole design fits in one small class and one function. The class hands out slots at a fixed rate; the function sends a request, reads the status, and only resends what is worth resending.

import threading, time
import requests

BASE = "https://api.serpbase.dev"
KEY = "YOUR_API_KEY"

class Pacer:
    """One limiter for the whole account, shared by every worker in the process."""

    def __init__(self, per_second):
        self.interval = 1.0 / per_second
        self._lock = threading.Lock()
        self._next = 0.0

    def wait(self):
        with self._lock:
            now = time.monotonic()
            if now < self._next:
                time.sleep(self._next - now)
                now = self._next
            self._next = now + self.interval

RETRYABLE = {1500, 1502, 1503, 1504}

def search(pacer, q, attempts=3, **params):
    for attempt in range(attempts):
        pacer.wait()
        r = requests.post(
            BASE + "/google/search",
            headers={"X-API-Key": KEY},
            json={"q": q, **params},
            timeout=60,
        )
        body = r.json()
        status = body.get("status", 0)

        if status == 1029:                        # over the limit; not charged
            time.sleep(float(r.headers.get("Retry-After", 1)))
            continue
        if status in RETRYABLE:                   # source failed; not charged
            time.sleep(1 + attempt)
            continue
        if status != 0:                           # 1000/1001/1004/1020: fix it
            raise RuntimeError(body.get("error", status))

        if r.headers.get("X-RateLimit-Remaining") == "0":
            time.sleep(1.0)                       # the next request would be refused
        return body

    raise RuntimeError("gave up after %d attempts" % attempts)

Three decisions in that loop are deliberate. The pacer is a single object with a lock, so every worker that holds it contributes to one schedule instead of one schedule each. The 1029 branch reads Retry-After rather than picking a backoff constant, because the server knows how long the wait is and a fixed two seconds is a guess. The fatal statuses raise immediately, so a typo in a parameter surfaces as an exception on the first call instead of three retries later.

The Remaining check sits after the successful return path rather than before the send. That is a small trade: it costs one extra second on the request that empties the bucket, and it saves the pacer from making a blocking decision on a header it does not need to read yet.

The same call in curl

curl -X POST "https://api.serpbase.dev/google/search" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $SERPBASE_API_KEY" \
  --max-time 60 \
  -d '{"q":"coffee shops","gl":"us","hl":"en"}'

The --max-time 60 is there for a reason discussed below. The request is a POST with a JSON body, unlike the compatibility endpoint, which takes GET with query parameters.

Sizing the pacer to the work

Set the pacer to the account's current limit, read from X-RateLimit-Limit on the first response rather than hard-coded, because the limit moves when a top-up is confirmed and a stale constant will either waste capacity or trip the limiter.

For a workload that touches more than one endpoint, remember that the credits are not uniform even though the rate limit is. A plain web search is one credit; the shopping, jobs, maps, marketplace and transcript engines cost more (checked 2026-11-24, per-endpoint pricing table). The QPS is shared across all of them, so a mixed workload is limited by the same number of requests while spending a different number of credits per request. Budget and throttle those separately.

Last thing: a client timeout below 30 seconds is a self-inflicted wound. Every request ends on the server side within 30 seconds with a result or an error, and the slow ones are the ones a short timeout cuts off just before they succeed. Keep the client at 60 and put the tight budget where it belongs, in the request's own timeout parameter, which the Google search endpoint accepts from 1 to 30 seconds and which returns status 1504 at once when the budget runs out, not charged.

The rate limit docs, the QPS ladder and the balance endpoint are at https://serpbase.dev/?utm_campaign=ops-222e35a2a81a&utm_medium=outreach&utm_source=opsbot

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.