Skip to main content

ratelimit-io

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

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 Limit, RateLimiter

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


@limiter(key=lambda user_id, **_: f'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.0.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.0.0
File Size Uploaded Provenance Actions
ratelimit_io-1.0.0.tar.gz 188.4 kB With provenance Details for ratelimit_io-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 202.5 kB

Release files / ratelimit_io-1.0.0.tar.gz

Download URL ratelimit_io-1.0.0.tar.gz
Size 188.4 kB
Tags Source
SHA-256 checksum
How to use checksums
13f91d4a748f024cbc30979344a7ae5fc7fe67901e9535102895272f5694311f
BLAKE2b-256 checksum
How to use checksums
817186e666de8ba54332a0193d89c8060d3bc087a19118d87001d6491750cdfd
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 20, 2026.

Transparency log

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

Download URL ratelimit_io-1.0.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc2c7ea18c99cbf3ee57ab1e2f9f21f748a57b158725ece6e1e80868bd415cae
BLAKE2b-256 checksum
How to use checksums
bc5019df67b9dd00870f7d0044f5835c4c4d2a283c6bd80c993a2a39c116036d
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 20, 2026.

Transparency log

Security for ratelimit-io 1.0.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.0.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 20, 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