token-limit-guard
Least recently used token allocator with per-token rate limiting and optional blocking behavior. Perfect for juggling API keys or access tokens that must respect individual quotas.
Features
- Accept tokens from a file or in-memory iterable
- Configure rate limiting per token with a rolling time window
- Blocking and non-blocking retrieval modes
- Simple LRU selection to balance token usage
- Thread-safe implementation with helpful logging for debugging
Installation
The package is published on PyPI:
pip install token-limit-guard
Quickstart
from token_limit_guard import TokenLimitGuard
token_guard = TokenLimitGuard(["tokenA", "tokenB", "tokenC"])
token_guard.set_limiting_factors(max_allowed_count=2, time_window_in_sec=60)
# Blocking call – waits for the next available token if all are exhausted
token = token_guard.get_a_token()
# Non-blocking call – returns None immediately when no token is available
maybe_token = token_guard.get_a_token(block=False)
# Convenience alias for non-blocking access
maybe_token = token_guard.try_get_a_token()
print(token)
Development
Install dev dependencies and run the tests with pytest:
python -m venv .token-venv
source .token-venv/bin/activate # or .venv\\Scripts\\activate on Windows
pip install -e .[dev] # pip install -e '.[dev]'
pytest
Publishing to PyPI
pip install --upgrade build
python -m build
pip install --upgrade twine
python -m twine upload dist/*
Remember to bump the version in pyproject.toml and token_limit_guard/__init__.py
before publishing.
Metadata
Release files for rate-limit-safe-token-allocator 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rate_limit_safe_token_allocator-0.1.1.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rate_limit_safe_token_allocator-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.1 kB
Release files / rate_limit_safe_token_allocator-0.1.1.tar.gz
| Download URL | rate_limit_safe_token_allocator-0.1.1.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b59a4a43df40789668cd8d7a507e13e4190d11925159ba66d1fe8d7e68fa31c8
|
|
BLAKE2b-256 checksum How to use checksums |
7ff218f7be11e6092e9db6e8c668fe6b22c277e1bb7d8d22d2e78b9c20db8da0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Release files / rate_limit_safe_token_allocator-0.1.1-py3-none-any.whl
| Download URL | rate_limit_safe_token_allocator-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b788d8cb2a3e4c01ed0dafb1e939c1129b26b24c9857df997d1e75682803400
|
|
BLAKE2b-256 checksum How to use checksums |
fcfe63224d2ae2ed939db4a505efe1458b5d2bf229ff9d0e3f2ef00fcc5206a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|