Skip to content

[P1] Retry decides on the status code alone, so a quota-exhausted 429 is retried 3× — the SDK amplifies the refusals it should stop #77

Closed

Description

@karlwaldman

Measured against production, 30 days, trial farm and internal excluded.

First, what is NOT wrong

I initially reported the Python SDK as our worst-served client (34.0% served). Split by plan, that framing is wrong:

client tier accounts requests served 402 429
sdk-node free 299 193,229 20.6% 56.5% 15.6%
sdk-node paid 51 111,060 95.3% 0.0% 2.6%
sdk-python free 300 390,771 11.5% 61.1% 26.2%
sdk-python paid 38 144,504 94.7% 0.0% 5.2%

Paid Python is at parity with paid Node — 94.7% against 95.3%. There is no SDK quality problem for paying customers. The headline gap was a free-tier mix effect.

What IS wrong — the retry amplifies refusals

Free Python shows 26.2% rate-limited against Node's 15.6% — 1.7× worse on the same tier. The cause is in this SDK.

oilpriceapi/async_client.py:69

DEFAULT_RETRY_CODES = [429, 500, 502, 503, 504]

oilpriceapi/retry.py:35

def should_retry(self, attempt: int, status_code: int) -> bool:

The retry decision takes the status code and nothing else. It never sees the response headers. So it cannot distinguish the two completely different things a 429 means:

  • "You are bursting — wait a moment." Retrying with backoff is correct.
  • "You are out of monthly quota." Retrying cannot succeed until the billing period resets. Three retries produce three more 429s and nothing else.

For a free account that has exhausted 200 requests, every call becomes up to 4 requests. That is a meaningful share of the 26.2%, and it is self-inflicted.

calculate_wait_time(attempt) is likewise pure exponential backoff with jitter and does not honour Retry-After — so when the server does say how long to wait, we ignore it.

The API already tells us which case it is

The client reads only three headers, and only to build an error message (client.py:272, exceptions.py:459):

X-RateLimit-Limit   X-RateLimit-Remaining   X-RateLimit-Reset

Per api#5664, the API emits nine rate-limit headers and documents two — including X-RateLimit-State, which carries a remedy vocabulary. The server is already telling the client whether the fix is "wait" or "upgrade". The retry path ignores it.

Proposed fix

  1. Pass the response (or its headers) into should_retry. The signature change is the whole fix — everything else follows.
  2. Do not retry when the remedy is "upgrade". Read X-RateLimit-State; if the state says the quota is exhausted rather than the burst window, fail fast and raise RateLimitError with the reset time and the upgrade URL.
  3. Honour Retry-After in calculate_wait_time when present, falling back to exponential backoff when absent.
  4. Surface the remedy in the exception. A developer who hits this should see "you have used 200 of 200 requests this month, resets 2026-09-01, upgrade at " — not "Rate limit exceeded".

Point 4 matters most commercially. Right now the most common thing our largest client population experiences is a bare rate-limit error with no path forward.

The demand behind it

Of the 300 free Python accounts:

total requests   390,773
mean               1,303      <- skewed by a few heavy users
median                32      <- the typical free account is well within limits
over 200/mo           49
of those, 200–10,000  45      <- solved by the $19 Developer tier

Forty-five accounts are one tier away from being fully served. That matches the pricing analysis in FOCUS: the largest blocked cohort needs 200–10,000 calls/month and is solved by $19.

So the honest summary is: the SDK is fine for people who pay, it makes the free experience worse than it needs to be by retrying into a wall, and behind it sits a specific, countable upgrade cohort.

Verification

After the change, sdk-python free-tier pct_429 should fall toward Node's ~15.6%. Query:

SELECT client_type, count(*) reqs,
       round(100.0*sum((response_status=429)::int)/count(*),1) pct_429
FROM api_requests ar JOIN users u ON u.id=ar.user_id
WHERE ar.created_at >= now() - interval '7 days'
  AND ar.client_type IN ('sdk-python','sdk-node')
  AND (u.plan='free' OR u.plan IS NULL)
GROUP BY 1;

Related: api#5664 (nine headers, two documented) · api#6434 (sdk_version captured on 0.61% of requests, so we cannot tell which SDK version anyone is on) · api#6432 (a paying customer rejected 22 days straight)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions