Skip to main content
# TokenGuard 🛡️

Lightweight local request protection for Python AI applications.

TokenGuard helps developers check prompts before sending them to an AI provider. It can reject oversized prompts, detect repeated requests, and flag rapidly growing prompts within a conversation.

TokenGuard is a local checking utility, not an automatic network interceptor or a complete billing-protection system.

## Features

- **Estimated prompt-size limits:** Reject prompts that exceed a configured estimate.
- **Repeated-request detection:** Block identical prompts after a configured number of previous accepted requests.
- **Growth detection:** Detect rapid increases in estimated prompt size within a conversation.
- **Conversation isolation:** Track prompt growth separately using a conversation identifier.
- **Time-window tracking:** Expire old request records.
- **Thread safety:** Protect shared request history with a lock.
- **Input validation:** Reject invalid prompts and configuration values.
- **No third-party runtime dependencies:** Uses Python's standard library.

## Requirements

- Python 3.9 or newer.
- No third-party runtime dependencies.

## Installation

Install the published package from PyPI:

```bash
python -m pip install tokenguard-local

PyPI: https://pypi.org/project/tokenguard-local/

Note: The published PyPI release may not contain the latest unreleased repository changes. Check the release version before relying on newer features.

Quick start

from token_guard import TokenGuard

guard = TokenGuard(
    max_tokens=8000,
    max_repeats=3,
    window_seconds=60,
)

prompt = "Explain how artificial intelligence works."

if guard.check(prompt):
    # Send the prompt to your AI provider here.
    print("Request allowed")
else:
    print("Request blocked")

Call guard.check(prompt) before sending the prompt to your provider. TokenGuard does not automatically intercept network requests.

Configuration

Parameter Default Description
max_tokens 8000 Maximum estimated prompt size
max_repeats 3 Maximum previously accepted identical prompts within the time window
window_seconds 60 Time window for tracking request history
growth_limit 3 Number of consecutive prompt sizes used to evaluate rapid growth
growth_ratio 1.5 Minimum growth ratio required between consecutive prompt sizes

Values must satisfy the constructor's validation rules. max_tokens and max_repeats must be at least 1, window_seconds must be positive, growth_limit must be at least 2, and growth_ratio must be greater than 1.

Detecting rapid prompt growth

Growth detection requires a context_id so TokenGuard can associate prompts with a conversation.

from token_guard import TokenGuard

guard = TokenGuard(
    max_tokens=8000,
    growth_limit=3,
    growth_ratio=1.5,
    window_seconds=60,
)

conversation_id = "conversation-123"

prompts = [
    "a" * 4,
    "b" * 8,
    "c" * 16,
]

for prompt in prompts:
    if guard.check(prompt, context_id=conversation_id):
        print("Request allowed")
    else:
        print("Request blocked")

This example illustrates the growth rule using artificial strings, not real AI prompts. Under the default settings shown, the first two prompts are accepted and the third is blocked because the estimated size has grown rapidly across the sequence.

Use a stable, unique conversation identifier for each conversation. Calls without a context_id still receive size and repeated-request checks, but growth detection is not applied.

Growth detection is a heuristic. It does not understand the meaning of a prompt or prove that a request is unsafe.

Repeated-request behavior

The repeat limit counts earlier accepted matching requests. With max_repeats=3, the first three identical requests can be accepted, while the fourth is blocked if it occurs within the active time window.

Repeated-request tracking applies to the whole TokenGuard instance, even when different conversation IDs are supplied.

Token estimation

TokenGuard estimates token counts using character length:

estimated_tokens = max(1, (len(text) + 3) // 4)

This is an approximation. Actual token counts depend on the model, tokenizer, and content. TokenGuard does not calculate exact provider token usage.

Run tests

Clone the repository:

git clone https://github.com/mokhabelethabang4-cpu/token-guard.git
cd token-guard

Run the tests:

python test_token_guard.py

Limitations

TokenGuard is an early-stage local request-checking utility, not a complete API security or billing enforcement system.

  • Your application must explicitly call guard.check(prompt) before sending a request.

  • It does not automatically intercept AI API calls or network traffic.

  • Token estimates are approximate.

  • Growth detection relies on prompt-size patterns, not semantic analysis.

  • Request history is stored in memory and resets when the process restarts.

  • Limits are local to each TokenGuard instance; they are not shared automatically across processes or servers.

  • It does not enforce provider-side spending limits, authentication, authorization, or provider rate limits.

  • It cannot guarantee that runaway loops or excessive API costs will be prevented.

Use provider-side usage limits, monitoring, and appropriate application-level controls alongside TokenGuard.

Project status

TokenGuard is an early-stage project focused on lightweight, local request protection for Python AI applications.

Contributions, bug reports, and suggestions are welcome through the GitHub repository.

License

This project is licensed under the MIT License. See LICENSE for details.

Metadata

Release files for tokenguard-local 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenguard-local 0.1.1
File Size Uploaded Provenance
tokenguard_local-0.1.1.tar.gz 5.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tokenguard-local 0.1.1
File Interpreter ABI Platform
tokenguard_local-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / tokenguard_local-0.1.1.tar.gz

Download URL tokenguard_local-0.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
57c43fb3f80fad0d2b9170aa52b1364b6bb0459b7d8e32e8773472972b445d93
BLAKE2b-256 checksum
How to use checksums
9c256fc029efd453694e35ee090bed207f4c3345f6cdb6e8388ad943affa7b13
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 log

Release files / tokenguard_local-0.1.1-py3-none-any.whl

Download URL tokenguard_local-0.1.1-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b84ef8fa70e2ae9324412a7d5f7fc8a2443f867b6af0361604a094b92a947745
BLAKE2b-256 checksum
How to use checksums
9b1a8adac39c93937bbbf839ea86ebb3d15a1d4574677f4884830fe558b5987f
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 log

Security for tokenguard-local 0.1.1

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.

Select from 2 files

Attestation for tokenguard_local-0.1.1.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 log

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.1 This release

2 release files Full 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