I’ve been working on a small Python API client recently, and I realized the request itself is usually the easy part.

At first, my code was basically just:

requests.get() → raise_for_status() → parse JSON.

That worked fine until I started running into timeouts, 429 responses, and the occasional 502/503.

The part I’m still figuring out is how aggressive retry logic should be.

Right now, I’m thinking about handling failures roughly like this:

timeout / connection error → retry
429 → respect Retry-After if it exists
500 / 502 / 503 / 504 → retry a limited number of times
401 / 403 / 404 → fail immediately
POST requests → be much more careful because retrying can create duplicate side effects

I’m also using exponential backoff with a little jitter, mostly to avoid multiple requests retrying at exactly the same time.

What I’m less sure about is where this logic should live.

Would you normally keep retry behavior inside each API client so the rules stay explicit, or put it into a reusable retry layer that all clients share?

I can see advantages to both.

Keeping it local makes the behavior easier to understand, but centralizing it avoids repeating the same timeout/backoff code everywhere.

For people who deal with APIs in production, how do you usually decide which errors are actually retryable?

And do you treat GET and POST retries differently in practice?

Recommended Answers

All 2 Replies

TL;DR The API will show you in time what to retry and what not.

I put my retry code in the API client, because every API responds differently. Some do retry-after very well, others tell you the same with a 400 status and a message in the body (and I've seen a lot more ways of error handling).

I let 500's fail immediately, unless the API is unstable and time has shown that the server fails for reasons unknown, and will be back up normally after a short period.

401 is usually a retry because a token timed out, so I retry that. The others will fail. Well, in one case a 404 i had to retry because the API was too slow. The POST succeeded but the following GET of the same item failed for a couple of seconds.

I treat POST and GET the same from the start, because failure is failure and it will only change if the API behaves unexpectedly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.