TokenGuard 🛡️
Lightweight local request protection for Python AI applications.
TokenGuard helps developers limit oversized prompts and detect repeated requests within a configurable time window.
Features
- Prompt size limits — Reject prompts that exceed the configured token estimate.
- Repeated request detection — Block identical prompts after a configurable number of previous requests.
- Time-window tracking — Expire old request records automatically.
- Thread safety — Protect shared request history with a lock.
- Input validation — Reject invalid prompt types and configuration values.
- No external dependencies — Uses Python's standard library.
Requirements
- Python 3.9 or newer recommended.
- No third-party packages required.
Installation
Clone the repository:
git clone https://github.com/mokhabelet habang4-cpu/token-guard.git
Enter the project directory:
cd token-guard
If your GitHub username or repository URL differs, replace the URL with your repository's actual clone URL.
Usage
from token_guard import TokenGuard
guard = TokenGuard(
max_tokens=8000,
max_repeats=3,
window_seconds=60,
)
prompt = "Explain how AI works."
if guard.check(prompt):
print("Request allowed")
else:
print("Request blocked")
Configuration
| Parameter | Default | Description |
|---|---|---|
max_tokens |
8000 |
Maximum estimated prompt tokens |
max_repeats |
3 |
Maximum number of previously accepted identical prompts |
window_seconds |
60 |
Time window used for request tracking |
The repeat limit counts previous matching requests. A prompt is blocked when the number of earlier matching requests in the active window reaches max_repeats.
Token estimation
TokenGuard estimates token usage using prompt length:
estimated_tokens = max(1, (len(text) + 3) // 4)
This is a rough estimate, not an exact model tokenizer. Actual token counts vary by AI model and text content.
Run tests
Run the test suite with Python:
python -m unittest -v
Limitations
- Request history is stored in memory and resets when the process restarts.
- Limits apply to a single
TokenGuardinstance, not automatically across multiple processes or servers. - TokenGuard does not contact AI providers or validate their actual token usage.
- Applications must call
guard.check(prompt)before sending a request to their AI provider. - This package does not replace authentication, authorization, or provider-side rate limits.
Project status
TokenGuard is an early-stage project focused on lightweight, local request protection for Python AI applications.
License
See the repository's license file for usage and distribution terms.
Metadata
Release files for tokenguard-local 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| tokenguard_local-0.1.0.tar.gz | 3.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokenguard_local-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.2 kB
Release files / tokenguard_local-0.1.0.tar.gz
| Download URL | tokenguard_local-0.1.0.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
805a52940581bd5fe3c70ca3e7792c23b92fe965799a297bc0f7c18219ea80f9
|
|
BLAKE2b-256 checksum How to use checksums |
0b7fe8e5fb4cb2cbeca3ea017a8666beb2f7997c6f6428368f2f1bb7650595f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 23, 2026.
Transparency logRelease files / tokenguard_local-0.1.0-py3-none-any.whl
| Download URL | tokenguard_local-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ecdbe86045d3d60a521cfee3ddf9494b31a844dba160f7e4a7dc49977d8cce6c
|
|
BLAKE2b-256 checksum How to use checksums |
200293e98b0fd7ec16993aa603ed6787bd68117d0b449f72c977d6d5492dc884
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 23, 2026.
Transparency logSecurity for tokenguard-local 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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for tokenguard_local-0.1.0.tar.gz
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 Oct 23, 2026.
Transparency logA 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.