Skip to main content

retryhttp

Zero-config HTTP retries with exponential backoff, jitter, and OAuth2 token refresh — for both httpx and requests.

from retryhttp import RetryClient

with RetryClient(token_refresher=lambda: fetch_token()) as client:
    resp = client.get("https://api.example.com/data")

Why

Every team rewrites retry logic from scratch. retryhttp gives you:

  • Exponential backoff with full jitter (AWS-recommended strategy)
  • Token refresh hooks — pass a callable, 401s are handled automatically
  • Per-status-code retry policies — retry 503 but not 404
  • Network error retries — connection errors and timeouts included
  • Async support via AsyncRetryClient
  • Drop-in replacements — subclasses httpx.Client and requests.Session, so existing code works unchanged

Install

# httpx support
pip install "retryhttp[httpx]"

# requests support
pip install "retryhttp[requests]"

# both
pip install "retryhttp[all]"

Usage

httpx (sync)

from retryhttp import RetryClient, RetryConfig

config = RetryConfig(
    max_attempts=5,
    backoff_base=0.5,
    backoff_max=30.0,
    retry_statuses=frozenset({429, 500, 502, 503, 504}),
)

def get_token() -> str:
    # call your auth server
    return "new-bearer-token"

with RetryClient(config=config, token_refresher=get_token) as client:
    resp = client.get("https://api.example.com/resource")
    print(resp.json())

httpx (async)

import asyncio
from retryhttp import AsyncRetryClient

async def get_token() -> str:
    return "new-bearer-token"

async def main():
    async with AsyncRetryClient(token_refresher=get_token) as client:
        resp = await client.get("https://api.example.com/resource")
        print(resp.json())

asyncio.run(main())

requests

from retryhttp import RetrySession

with RetrySession(token_refresher=lambda: "new-token") as session:
    resp = session.get("https://api.example.com/resource")

Configuration

RetryConfig controls all retry behaviour:

Parameter Default Description
max_attempts 3 Total attempts (1 = no retries)
retry_statuses {429,500,502,503,504} Status codes that trigger a retry
backoff_base 1.0 Base backoff in seconds
backoff_max 60.0 Maximum backoff ceiling
jitter True Full jitter on each delay
retry_on_network_error True Retry on connection/timeout errors
token_refresh_status 401 Status that triggers token refresh
raise_on_exhaust True Raise RetryExhausted when all attempts fail; False returns the last response

How backoff works

Uses AWS's Full Jitter strategy:

cap = min(backoff_max, backoff_base × 2^attempt)
sleep = random(0, cap)

This avoids thundering-herd problems when many clients retry simultaneously.

Exceptions

from retryhttp import RetryExhausted

try:
    client.get("https://api.example.com")
except RetryExhausted as e:
    print(f"Failed after {e.attempts} attempts, last status: {e.last_status}")

Development

git clone https://github.com/praneethvedantham/retryhttp
cd retryhttp
pip install -e ".[dev]"
pytest

License

MIT

Metadata

Release files for retryhttp2 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for retryhttp2 0.1.0
File Size Uploaded
retryhttp2-0.1.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for retryhttp2 0.1.0
File Interpreter ABI Platform
retryhttp2-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.9 kB

Release files / retryhttp2-0.1.0.tar.gz

Download URL retryhttp2-0.1.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c62a78e1fbe889a9a2be647b62735c076ecab244a9bc0463664285971f9709c1
BLAKE2b-256 checksum
How to use checksums
64308565a10fd52eff29f733bf2d8a1decf2987aae66459c1fccf3666fb7593b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 14, 2026.

Transparency log

Release files / retryhttp2-0.1.0-py3-none-any.whl

Download URL retryhttp2-0.1.0-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6a4be6cf5e2e0860b5dac792ad3746afaf42ef8fe202d93405ac64e4ae7e439
BLAKE2b-256 checksum
How to use checksums
5d1a38fe02f6307ed8ae469a63cdfea4da428ed291d4dd52ace682e073509a5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page