Rate limiting library backed by DynamoDB with token bucket algorithm
Project description
zae-limiter
A rate limiting library backed by DynamoDB using the token bucket algorithm.
Installation
pip install zae-limiter
Usage
from zae_limiter import RateLimiter, SyncRateLimiter, Limit, StackOptions
# async-aws-backed-production-ready-rate-limiter
limiter = RateLimiter(
name="my-app",
region="us-east-1",
# Declare desired infrastructure state - CloudFormation ensures it matches
stack_options=StackOptions(),
)
# Sync wrapper shares the same infrastructure and API.
sync_limiter = SyncRateLimiter(name="my-app", region="us-east-1")
# Define default limits (can be overridden per-entity)
default_limits = [
Limit.per_minute("rpm", 100),
# Token bucket with burst capacity
Limit.per_minute("tpm", 10_000, burst=50_000),
]
async with limiter.acquire(
entity_id="api-key-123",
resource="gpt-4",
limits=default_limits, # Multiple limits in a single atomic transaction
consume={"rpm": 1, "tpm": 500}, # Estimate tokens upfront
) as lease:
response = await call_llm()
# Reconcile actual usage (can go negative for post-hoc adjustment)
await lease.adjust(tpm=response.usage.total_tokens - 500)
# On success: committed | On exception: rolled back automatically
# Hierarchical entities: create project with stored limits, then API key under it
await limiter.create_entity(entity_id="proj-1", name="Production")
await limiter.set_limits("proj-1", [Limit.per_minute("tpm", 100_000)]) # Project-level
await limiter.create_entity(entity_id="api-key-456", parent_id="proj-1")
# cascade=True enforces both key AND project limits
with sync_limiter.acquire(
entity_id="api-key-456",
resource="gpt-4",
limits=default_limits,
consume={"rpm": 1, "tpm": 500},
cascade=True, # Also checks parent's stored limits
use_stored_limits=True, # Uses proj-1's 100k tpm limit
):
call_api()
# Cleanup (removes all data)
await limiter.delete_stack()
Documentation
| Guide | Description |
|---|---|
| Getting Started | Installation, first deployment |
| Basic Usage | Rate limiting patterns, error handling |
| Hierarchical Limits | Parent/child entities, cascade mode |
| LLM Integration | Token estimation and reconciliation |
| CLI Reference | Deploy, status, delete commands |
| Production Guide | Security, monitoring, cost |
Production Deployment
The default deployment includes CloudWatch alarms and usage aggregation. For production, add data recovery and alert routing:
zae-limiter deploy --name my-app --region us-east-1 \
--pitr-recovery-days 7 \
--alarm-sns-topic arn:aws:sns:us-east-1:123456789012:alerts
For security best practices, multi-region considerations, and cost estimation, see the Production Guide.
Contributing
git clone https://github.com/zeroae/zae-limiter.git && cd zae-limiter
uv sync --all-extras
pytest
See the Contributing Guide for development setup, testing, and architecture details.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zae_limiter-0.2.0.tar.gz.
File metadata
- Download URL: zae_limiter-0.2.0.tar.gz
- Upload date:
- Size: 357.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c58e723038d63d7ca4a04c6de2f99116846955ff6e668bbd536cce0ee25c7904
|
|
| MD5 |
a4daa4fa4617c05b6963ec40786fd29d
|
|
| BLAKE2b-256 |
9cfee1ad0d75a8eb7c268ec7c9ac6c32a538ad78758ca92d9e72605639f593b2
|
Provenance
The following attestation bundles were made for zae_limiter-0.2.0.tar.gz:
Publisher:
release.yml on zeroae/zae-limiter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zae_limiter-0.2.0.tar.gz -
Subject digest:
c58e723038d63d7ca4a04c6de2f99116846955ff6e668bbd536cce0ee25c7904 - Sigstore transparency entry: 820766534
- Sigstore integration time:
-
Permalink:
zeroae/zae-limiter@06636b70112e1583250767bbfff84b13e2ae016e -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/zeroae
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@06636b70112e1583250767bbfff84b13e2ae016e -
Trigger Event:
push
-
Statement type:
File details
Details for the file zae_limiter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: zae_limiter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 58.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fbfe152113c02d165ef5551210849bcc8eb052893310184fe18cfaafac0c2ab
|
|
| MD5 |
77c95e6e4ddf22ed0f593aa121f24140
|
|
| BLAKE2b-256 |
4f1b0e512a6d88df40c0c132b406f1b3465920723bb31fca999a82973903b703
|
Provenance
The following attestation bundles were made for zae_limiter-0.2.0-py3-none-any.whl:
Publisher:
release.yml on zeroae/zae-limiter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zae_limiter-0.2.0-py3-none-any.whl -
Subject digest:
4fbfe152113c02d165ef5551210849bcc8eb052893310184fe18cfaafac0c2ab - Sigstore transparency entry: 820766538
- Sigstore integration time:
-
Permalink:
zeroae/zae-limiter@06636b70112e1583250767bbfff84b13e2ae016e -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/zeroae
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@06636b70112e1583250767bbfff84b13e2ae016e -
Trigger Event:
push
-
Statement type: