Skip to main content

ratelimit-io

PyPI Downloads Python versions License: MIT CI Coverage Documentation llms.txt Context7

Distributed rate limiting for Python on Redis. Every process that shares a Redis server shares the limit: fixed window, sliding window or GCRA, each one atomic Lua script, for async and sync code alike.

from redis.asyncio import Redis

from ratelimit_io import Key, Limit, RateLimiter

limiter = RateLimiter(Redis(), limit=Limit(100, minutes=1))


@limiter(key=Key('user:{user_id}'))
async def search(user_id: int, query: str) -> list[str]: ...

The 101st call in a minute for one user raises RateLimitExceededError, and its retry_after, rounded up, fills an HTTP Retry-After header.

Documentation: https://bagowix.github.io/ratelimit-io/

Features

  • Three algorithms: fixed window (the default), an exact sliding window, and GCRA, a token bucket in constant memory.
  • Incoming and outgoing requests. max_wait=0 refuses an over-limit request at once. With a wait budget the request reserves the next free slot in the same round trip and sleeps until it: no polling, first come first served across processes, and an early failure when the slot lies beyond the budget.
  • Async and sync in one class: await limiter.acquire() with redis.asyncio.Redis, limiter.acquire_sync() with redis.Redis, and a decorator for both that keeps the function's signature.
  • Strict typing: mypy, pyright and pyrefly in strict mode.
  • Hashed keys: a bucket name holds the SHA-256 of its key, so raw keys stay out of Redis and every name has the same length.
  • Tested on real servers: Python 3.11 to 3.15 against Redis 7, plus Redis 5, Redis 6.2 and Valkey 8, including races between threads and tasks for one bucket.

Install

pip install ratelimit-io

Requires Python 3.11+, redis-py 5.0.1+ and a Redis 5.0+ or Valkey server.

Usage

from redis import Redis

from ratelimit_io import Algorithm, Limit, RateLimiter, RateLimitExceededError

# Incoming requests: refuse at once.
api = RateLimiter(Redis(), limit=Limit(10, seconds=1))

try:
    api.acquire_sync('203.0.113.7')
except RateLimitExceededError as error:
    print(f'429, retry after {error.retry_after:.1f}s')

# Outgoing requests: wait up to 30 s for a slot, never exceed 5000 an hour.
github = RateLimiter(
    Redis(),
    limit=Limit(5000, hours=1),
    key='github',
    algorithm=Algorithm.SLIDING_WINDOW,
    max_wait=30,
)
github.acquire_sync()

Guides: algorithms, keys and buckets, waiting, FastAPI, Litestar, Django and Flask. Upgrading from 0.x? Read the migration guide.

Contributing

See CONTRIBUTING.md.

License

MIT

Metadata

Release files for ratelimit-io 1.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 ratelimit-io 1.1.0
File Size Uploaded Provenance Actions
ratelimit_io-1.1.0.tar.gz 195.5 kB With provenance Details for ratelimit_io-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ratelimit-io 1.1.0
File Interpreter ABI Platform Actions
ratelimit_io-1.1.0-py3-none-any.whl Python 3 none any Details for ratelimit_io-1.1.0-py3-none-any.whl

Total release size: 211.9 kB

Release files / ratelimit_io-1.1.0.tar.gz

Download URL ratelimit_io-1.1.0.tar.gz
Size 195.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9cd13ad76c759bc35f855f7ce473d4b616fb1ec647adf563db1b0ca03b16875d
BLAKE2b-256 checksum
How to use checksums
777111289b22d47167e74215bb4fe987079e6d4d0c1eb84ce63c6c22f99948f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 21, 2026.

Transparency log

Release files / ratelimit_io-1.1.0-py3-none-any.whl

Download URL ratelimit_io-1.1.0-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e60b486a4e935410af7f23e5fd5378ee50e9e418219fb546f24e2c1d725bb85
BLAKE2b-256 checksum
How to use checksums
89047bd3c55a66ffc5e8a4908984bf664e7115eb9789cf24dce9e6a0cebfb266
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 21, 2026.

Transparency log

Security for ratelimit-io 1.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ratelimit_io-1.1.0.tar.gz

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 Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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