Skip to main content

akeylimiter provides key-based async rate limiting built on aiolimiter. Each key gets its own limiter, allowing independent rate limits for different users, API keys, or other identifiers.

Installation

pip install akeylimiter

Usage

rate_limited

Use rate_limited to apply an independent rate limit for each key. By default, all function arguments determine the rate limit key. Use key to select which arguments determine the rate limit.

from akeylimiter import rate_limited
from cachetools import TTLCache

store = TTLCache(maxsize=1024, ttl=600)


@rate_limited(
    max_rate=10,
    time_period=1,
    store=store,
    key=lambda user_id, prompt: user_id,
)
async def generate(user_id: str, prompt: str): ...

Here, all requests from the same user_id share a limiter, while different users get independent limiters.

generate("user-1", "hello")  ─┐
generate("user-1", "world")  ─┼─ same limiter
generate("user-1", "python") ─┘

generate("user-2", "hello")  ─── different limiter

The limiter store can be any MutableMapping, such as a regular dictionary or a cachetools cache like TTLCache or LRUCache.

Using a cachetools cache is useful when the number of keys can grow over time. Since each key creates a limiter, a regular dictionary keeps every limiter until it is explicitly removed. Caches such as TTLCache and LRUCache can automatically evict limiters based on expiration or cache capacity.

rate_limited_method

Use rate_limited_method when the limiter store belongs to a class instance.

from akeylimiter import rate_limited_method
from cachetools import LRUCache


class APIClient:
    def __init__(self):
        self.limiters = LRUCache(maxsize=1024)

    @rate_limited_method(
        max_rate=10,
        time_period=1,
        store_factory=lambda self: self.limiters,
        key=lambda self, api_key, endpoint, params: api_key,
    )
    async def fetch(
        self,
        api_key: str,
        endpoint: str,
        params: dict[str, object],
    ): ...

Each instance maintains its own limiter store.

client.fetch("key-1", "/users", ...)    ─┐
client.fetch("key-1", "/posts", ...)    ─┼─ same limiter
client.fetch("key-1", "/comments", ...) ─┘

client.fetch("key-2", "/users", ...)  ───── different limiter

The two clients have independent rate limits, even when they use the same api_key.

Metadata

Release files for akeylimiter 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 akeylimiter 0.1.0
File Size Uploaded Provenance Actions
akeylimiter-0.1.0.tar.gz 2.7 kB Without provenance Details for akeylimiter-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 6.3 kB

Release files / akeylimiter-0.1.0.tar.gz

Download URL akeylimiter-0.1.0.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
005279fbe66ec62d157cb0b730156deaa48cc215b45d2f83180617e68c1bffba
BLAKE2b-256 checksum
How to use checksums
58f56ea6aa5ae014c52b7342e3c5b74a65a13b502d2cca8084a522b050350ef1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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

Download URL akeylimiter-0.1.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
455f223c47aec8237a01b0abf20516ee8dacb27a4cdd3d5c3231c10f03781e7d
BLAKE2b-256 checksum
How to use checksums
3cfa0b1e33f6dbf6294d4f952026f5ca965219ccc576a1ef1d8afcae37ba2e48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for akeylimiter 0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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