Skip to main content

arate-limit

A flexible and robust rate limiting library for Python applications, offering multiple implementation strategies including leaky bucket, token bucket, and Redis-based sliding window rate limiters.

Features

  • Multiple rate limiting strategies:
    • Leaky bucket rate limiter
    • Token bucket rate limiter
    • Redis-based sliding window rate limiter
    • Redis-based sliding window API rate limiter
  • Async/await support using asyncio
  • Configurable time windows and burst allowances
  • Safe for concurrent access within asyncio applications
  • Redis integration for distributed rate limiting

Installation

pip install arate-limit

Usage

Leaky Bucket Rate Limiter

A rate limiter that implements the leaky bucket algorithm, which smooths out bursts of requests and processes them at a steady rate:

import asyncio

from arate_limit import LeakyBucketRateLimiter

async def example():
    # Allow 100 requests per minute with some slack
    limiter = LeakyBucketRateLimiter(event_count=100, time_window=60, slack=10)

    async def limited_task():
        await limiter.wait()
        # Your rate-limited code here
        print("Task executed")

    # Execute multiple tasks
    tasks = [limited_task() for _ in range(10)]
    await asyncio.gather(*tasks)

Token Bucket Rate Limiter

More sophisticated rate limiting with burst support:

from datetime import timedelta

from arate_limit import TokenBucketRateLimiter

async def example():
    # Allow 1000 requests per hour with burst of 100
    limiter = TokenBucketRateLimiter(
        event_count=1000,
        time_window=timedelta(hours=1),
        burst=100
    )

    await limiter.wait()  # Wait for rate limit

Redis Sliding Window Rate Limiter

Distributed rate limiting using Redis:

from arate_limit import RedisSlidingWindowRateLimiter
import redis.asyncio as redis

async def example():
    redis_client = redis.Redis(host='localhost', port=6379)

    # Allow 1000 requests per minute with slack of 10
    limiter = RedisSlidingWindowRateLimiter(
        redis=redis_client,
        event_count=1000,
        time_window=60,
        slack=10
    )

    await limiter.wait()  # Wait for rate limit

Redis Sliding Window API Rate Limiter

Distributed API rate limiting using Redis:

from arate_limit import RedisSlidingWindowApiRateLimiter
import redis.asyncio as redis

async def example():
    redis_client = redis.Redis(host='localhost', port=6379)

    # Allow 1000 requests per minute per user
    limiter = RedisSlidingWindowApiRateLimiter(
        redis=redis_client,
        event_count=1000,
        time_window=60,
    )

    result, time_remaining = await limiter.check("user-1")
    if not result:
        raise HTTPException(
            status_code=429,
            detail=f"Rate limit exceeded. Try again in {time_remaining} seconds"
        )

Configuration Options

All rate limiters accept these common parameters:

  • event_count: Maximum number of events allowed in the time window
  • time_window: Time period for the rate limit (accepts int/float seconds or timedelta)

Additional options per implementation:

LeakyBucketRateLimiter

  • slack: Additional allowance for brief bursts (default: 10)

TokenBucketRateLimiter

  • burst: Maximum burst size (default: 100)

RedisSlidingWindowRateLimiter

  • redis: Redis compatible client/interface
  • slack: Additional allowance for brief bursts (default: 10)
  • key_prefix: Prefix for Redis keys (default: "rate_limiter:")

RedisSlidingWindowApiRateLimiter

  • redis: Redis compatible client/interface
  • key_prefix: Prefix for Redis keys (default: "rate_limiter:")

Error Handling

The rate limiters raise appropriate exceptions for invalid configurations:

  • TypeError: When parameters are of incorrect type
  • ValueError: When parameters have invalid values

Performance Considerations

  • LeakyBucketRateLimiter: Best for scenarios requiring steady, predictable request rates
  • TokenBucketRateLimiter: Efficient for bursty workloads
  • RedisSlidingWindowRateLimiter: Suitable for distributed systems, but requires Redis or Redis compatible cache service
  • RedisSlidingWindowApiRateLimiter: Suitable for distributed systems, but requires Redis or Redis compatible cache service

License

This project is licensed under the MIT License - see the LICENSE file for details.

Release files for arate-limit 1.2.2

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

Source distribution (sdist)

Source distribution for arate-limit 1.2.2
File Size Uploaded
arate_limit-1.2.2.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for arate-limit 1.2.2
File Interpreter ABI Platform
arate_limit-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / arate_limit-1.2.2.tar.gz

Download URL arate_limit-1.2.2.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5d37dacbfd7a1e62bc0adfd1bfbb4c6582e9e05aabdf9cf17152821c24879025
BLAKE2b-256 checksum
How to use checksums
7351487e37c2a3fa389be70dba2525ab9e4d5528c7b1b74461cb89adda180402
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 Sep 15, 2026.

Transparency log

Release files / arate_limit-1.2.2-py3-none-any.whl

Download URL arate_limit-1.2.2-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e92ca6965b657f26db94e7053dcc8dec84c0646292c96b586d5d803849a83ccd
BLAKE2b-256 checksum
How to use checksums
e29ad169fdf15bee24c81fc807ef017c18403b39bb46b22660499994c2d5a78a
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 Sep 15, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.0

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