Skip to main content

Rate limiting library backed by DynamoDB with token bucket algorithm

Project description

zae-limiter

PyPI version Conda version Python versions License Lint Tests codecov Docs

A rate limiting library backed by DynamoDB using the token bucket algorithm.

Installation

pip install zae-limiter
# or
conda install -c conda-forge 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)
    # Tokens written to DynamoDB on enter | Rolled back on exception

# 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)

# cascade is an entity property — acquire() auto-cascades to parent
with sync_limiter.acquire(
    entity_id="api-key-456",
    resource="gpt-4",
    limits=default_limits,
    consume={"rpm": 1, "tpm": 500},
    use_stored_limits=True,  # Uses proj-1's 100k tpm limit
):
    call_api()

# Cleanup (removes all data)
await limiter.delete_stack()

Documentation

Full 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zae_limiter-0.9.0.tar.gz (978.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zae_limiter-0.9.0-py3-none-any.whl (233.1 kB view details)

Uploaded Python 3

File details

Details for the file zae_limiter-0.9.0.tar.gz.

File metadata

  • Download URL: zae_limiter-0.9.0.tar.gz
  • Upload date:
  • Size: 978.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zae_limiter-0.9.0.tar.gz
Algorithm Hash digest
SHA256 db202461184c7ba7cf047a50b7878f250b08aec350fc8c6c8d895b1ca397021e
MD5 c12071bfc0f04107a6199b4ad33adddb
BLAKE2b-256 3f29191fd6719733fb62359ca20a3f21211d5f2f3ebfde1802778b5f0c806b1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for zae_limiter-0.9.0.tar.gz:

Publisher: release.yml on zeroae/zae-limiter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zae_limiter-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: zae_limiter-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 233.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zae_limiter-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3183f3c21b90803722375104dec734090a9f7bb396dfca6b557eb53d6cdef95
MD5 ae5a47a0102f6bbfdc2c0390f5b06e53
BLAKE2b-256 21022e2c13f2ac9c10ee91a064d4d552adf054b4b7cb77e1d77e9d46acbb77de

See more details on using hashes here.

Provenance

The following attestation bundles were made for zae_limiter-0.9.0-py3-none-any.whl:

Publisher: release.yml on zeroae/zae-limiter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page