tokenguard-local 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+
2
+ MIT License
3
+
4
+ Copyright (c) 2026 TokenGuard Contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenguard-local
3
+ Version: 0.1.0
4
+ Summary: Lightweight local request protection for Python AI applications
5
+ Author: TokenGuard Contributors
6
+ License-Expression: MIT
7
+ Keywords: ai,llm,token-budget,loop-detection,python
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+
17
+ # TokenGuard 🛡️
18
+
19
+ **Lightweight local request protection for Python AI applications.**
20
+
21
+ TokenGuard helps developers limit oversized prompts and detect repeated requests within a configurable time window.
22
+
23
+ ## Features
24
+
25
+ - **Prompt size limits** — Reject prompts that exceed the configured token estimate.
26
+ - **Repeated request detection** — Block identical prompts after a configurable number of previous requests.
27
+ - **Time-window tracking** — Expire old request records automatically.
28
+ - **Thread safety** — Protect shared request history with a lock.
29
+ - **Input validation** — Reject invalid prompt types and configuration values.
30
+ - **No external dependencies** — Uses Python's standard library.
31
+
32
+ ## Requirements
33
+
34
+ - Python 3.9 or newer recommended.
35
+ - No third-party packages required.
36
+
37
+ ## Installation
38
+
39
+ Clone the repository:
40
+
41
+ ```bash
42
+ git clone https://github.com/mokhabelet habang4-cpu/token-guard.git
43
+ ```
44
+
45
+ Enter the project directory:
46
+
47
+ ```bash
48
+ cd token-guard
49
+ ```
50
+
51
+ > If your GitHub username or repository URL differs, replace the URL with your repository's actual clone URL.
52
+
53
+ ## Usage
54
+
55
+ ```python
56
+ from token_guard import TokenGuard
57
+
58
+ guard = TokenGuard(
59
+ max_tokens=8000,
60
+ max_repeats=3,
61
+ window_seconds=60,
62
+ )
63
+
64
+ prompt = "Explain how AI works."
65
+
66
+ if guard.check(prompt):
67
+ print("Request allowed")
68
+ else:
69
+ print("Request blocked")
70
+ ```
71
+
72
+ ## Configuration
73
+
74
+ | Parameter | Default | Description |
75
+ |---|---:|---|
76
+ | `max_tokens` | `8000` | Maximum estimated prompt tokens |
77
+ | `max_repeats` | `3` | Maximum number of previously accepted identical prompts |
78
+ | `window_seconds` | `60` | Time window used for request tracking |
79
+
80
+ 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`.
81
+
82
+ ## Token estimation
83
+
84
+ TokenGuard estimates token usage using prompt length:
85
+
86
+ ```python
87
+ estimated_tokens = max(1, (len(text) + 3) // 4)
88
+ ```
89
+
90
+ This is a rough estimate, not an exact model tokenizer. Actual token counts vary by AI model and text content.
91
+
92
+ ## Run tests
93
+
94
+ Run the test suite with Python:
95
+
96
+ ```bash
97
+ python -m unittest -v
98
+ ```
99
+
100
+ ## Limitations
101
+
102
+ - Request history is stored in memory and resets when the process restarts.
103
+ - Limits apply to a single `TokenGuard` instance, not automatically across multiple processes or servers.
104
+ - TokenGuard does not contact AI providers or validate their actual token usage.
105
+ - Applications must call `guard.check(prompt)` before sending a request to their AI provider.
106
+ - This package does not replace authentication, authorization, or provider-side rate limits.
107
+
108
+ ## Project status
109
+
110
+ TokenGuard is an early-stage project focused on lightweight, local request protection for Python AI applications.
111
+
112
+ ## License
113
+
114
+ See the repository's license file for usage and distribution terms.
@@ -0,0 +1,99 @@
1
+
2
+ # TokenGuard 🛡️
3
+
4
+ **Lightweight local request protection for Python AI applications.**
5
+
6
+ TokenGuard helps developers limit oversized prompts and detect repeated requests within a configurable time window.
7
+
8
+ ## Features
9
+
10
+ - **Prompt size limits** — Reject prompts that exceed the configured token estimate.
11
+ - **Repeated request detection** — Block identical prompts after a configurable number of previous requests.
12
+ - **Time-window tracking** — Expire old request records automatically.
13
+ - **Thread safety** — Protect shared request history with a lock.
14
+ - **Input validation** — Reject invalid prompt types and configuration values.
15
+ - **No external dependencies** — Uses Python's standard library.
16
+
17
+ ## Requirements
18
+
19
+ - Python 3.9 or newer recommended.
20
+ - No third-party packages required.
21
+
22
+ ## Installation
23
+
24
+ Clone the repository:
25
+
26
+ ```bash
27
+ git clone https://github.com/mokhabelet habang4-cpu/token-guard.git
28
+ ```
29
+
30
+ Enter the project directory:
31
+
32
+ ```bash
33
+ cd token-guard
34
+ ```
35
+
36
+ > If your GitHub username or repository URL differs, replace the URL with your repository's actual clone URL.
37
+
38
+ ## Usage
39
+
40
+ ```python
41
+ from token_guard import TokenGuard
42
+
43
+ guard = TokenGuard(
44
+ max_tokens=8000,
45
+ max_repeats=3,
46
+ window_seconds=60,
47
+ )
48
+
49
+ prompt = "Explain how AI works."
50
+
51
+ if guard.check(prompt):
52
+ print("Request allowed")
53
+ else:
54
+ print("Request blocked")
55
+ ```
56
+
57
+ ## Configuration
58
+
59
+ | Parameter | Default | Description |
60
+ |---|---:|---|
61
+ | `max_tokens` | `8000` | Maximum estimated prompt tokens |
62
+ | `max_repeats` | `3` | Maximum number of previously accepted identical prompts |
63
+ | `window_seconds` | `60` | Time window used for request tracking |
64
+
65
+ 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`.
66
+
67
+ ## Token estimation
68
+
69
+ TokenGuard estimates token usage using prompt length:
70
+
71
+ ```python
72
+ estimated_tokens = max(1, (len(text) + 3) // 4)
73
+ ```
74
+
75
+ This is a rough estimate, not an exact model tokenizer. Actual token counts vary by AI model and text content.
76
+
77
+ ## Run tests
78
+
79
+ Run the test suite with Python:
80
+
81
+ ```bash
82
+ python -m unittest -v
83
+ ```
84
+
85
+ ## Limitations
86
+
87
+ - Request history is stored in memory and resets when the process restarts.
88
+ - Limits apply to a single `TokenGuard` instance, not automatically across multiple processes or servers.
89
+ - TokenGuard does not contact AI providers or validate their actual token usage.
90
+ - Applications must call `guard.check(prompt)` before sending a request to their AI provider.
91
+ - This package does not replace authentication, authorization, or provider-side rate limits.
92
+
93
+ ## Project status
94
+
95
+ TokenGuard is an early-stage project focused on lightweight, local request protection for Python AI applications.
96
+
97
+ ## License
98
+
99
+ See the repository's license file for usage and distribution terms.
@@ -0,0 +1,31 @@
1
+
2
+ [build-system]
3
+ requires = ["setuptools>=68"]
4
+ build-backend = "setuptools.build_meta"
5
+
6
+ [project]
7
+ name = "tokenguard-local"
8
+ version = "0.1.0"
9
+ description = "Lightweight local request protection for Python AI applications"
10
+ readme = "README.md"
11
+ requires-python = ">=3.9"
12
+ license = "MIT"
13
+ license-files = ["LICENSE"]
14
+ authors = [
15
+ { name = "TokenGuard Contributors" }
16
+ ]
17
+ keywords = [
18
+ "ai",
19
+ "llm",
20
+ "token-budget",
21
+ "loop-detection",
22
+ "python"
23
+ ]
24
+ classifiers = [
25
+ "Development Status :: 3 - Alpha",
26
+ "Programming Language :: Python :: 3",
27
+ "Operating System :: OS Independent"
28
+ ]
29
+
30
+ [tool.setuptools]
31
+ py-modules = ["token_guard"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,74 @@
1
+
2
+ """TokenGuard: lightweight local request protection for Python AI apps."""
3
+
4
+ from collections import deque
5
+ from hashlib import sha256
6
+ from threading import Lock
7
+ import time
8
+
9
+
10
+ class TokenGuard:
11
+ """Block oversized prompts and repeated requests within a time window."""
12
+
13
+ def __init__(
14
+ self,
15
+ max_tokens=8000,
16
+ max_repeats=3,
17
+ window_seconds=60,
18
+ ):
19
+ if max_tokens < 1:
20
+ raise ValueError("max_tokens must be at least 1")
21
+ if max_repeats < 1:
22
+ raise ValueError("max_repeats must be at least 1")
23
+ if window_seconds <= 0:
24
+ raise ValueError("window_seconds must be positive")
25
+
26
+ self.max_tokens = max_tokens
27
+ self.max_repeats = max_repeats
28
+ self.window_seconds = window_seconds
29
+ self.history = deque()
30
+ self.lock = Lock()
31
+
32
+ @staticmethod
33
+ def estimate_tokens(text):
34
+ """Estimate tokens quickly; this is not an exact tokenizer."""
35
+ if not isinstance(text, str):
36
+ raise TypeError("prompt must be a string")
37
+
38
+ return max(1, (len(text) + 3) // 4)
39
+
40
+ def check(self, prompt):
41
+ """Return True if allowed, or False if blocked."""
42
+ if not isinstance(prompt, str):
43
+ raise TypeError("prompt must be a string")
44
+
45
+ if self.estimate_tokens(prompt) > self.max_tokens:
46
+ return False
47
+
48
+ now = time.monotonic()
49
+ fingerprint = sha256(prompt.encode("utf-8")).hexdigest()
50
+
51
+ with self.lock:
52
+ # Remove requests outside the active time window.
53
+ while (
54
+ self.history
55
+ and now - self.history[0][0] >= self.window_seconds
56
+ ):
57
+ self.history.popleft()
58
+
59
+ # Count earlier matching requests in this window.
60
+ repeats = sum(
61
+ 1
62
+ for _, previous in self.history
63
+ if previous == fingerprint
64
+ )
65
+
66
+ if repeats >= self.max_repeats:
67
+ return False
68
+
69
+ self.history.append((now, fingerprint))
70
+ return True
71
+
72
+
73
+ # Ready-to-use default guard.
74
+ guard = TokenGuard()
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenguard-local
3
+ Version: 0.1.0
4
+ Summary: Lightweight local request protection for Python AI applications
5
+ Author: TokenGuard Contributors
6
+ License-Expression: MIT
7
+ Keywords: ai,llm,token-budget,loop-detection,python
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+
17
+ # TokenGuard 🛡️
18
+
19
+ **Lightweight local request protection for Python AI applications.**
20
+
21
+ TokenGuard helps developers limit oversized prompts and detect repeated requests within a configurable time window.
22
+
23
+ ## Features
24
+
25
+ - **Prompt size limits** — Reject prompts that exceed the configured token estimate.
26
+ - **Repeated request detection** — Block identical prompts after a configurable number of previous requests.
27
+ - **Time-window tracking** — Expire old request records automatically.
28
+ - **Thread safety** — Protect shared request history with a lock.
29
+ - **Input validation** — Reject invalid prompt types and configuration values.
30
+ - **No external dependencies** — Uses Python's standard library.
31
+
32
+ ## Requirements
33
+
34
+ - Python 3.9 or newer recommended.
35
+ - No third-party packages required.
36
+
37
+ ## Installation
38
+
39
+ Clone the repository:
40
+
41
+ ```bash
42
+ git clone https://github.com/mokhabelet habang4-cpu/token-guard.git
43
+ ```
44
+
45
+ Enter the project directory:
46
+
47
+ ```bash
48
+ cd token-guard
49
+ ```
50
+
51
+ > If your GitHub username or repository URL differs, replace the URL with your repository's actual clone URL.
52
+
53
+ ## Usage
54
+
55
+ ```python
56
+ from token_guard import TokenGuard
57
+
58
+ guard = TokenGuard(
59
+ max_tokens=8000,
60
+ max_repeats=3,
61
+ window_seconds=60,
62
+ )
63
+
64
+ prompt = "Explain how AI works."
65
+
66
+ if guard.check(prompt):
67
+ print("Request allowed")
68
+ else:
69
+ print("Request blocked")
70
+ ```
71
+
72
+ ## Configuration
73
+
74
+ | Parameter | Default | Description |
75
+ |---|---:|---|
76
+ | `max_tokens` | `8000` | Maximum estimated prompt tokens |
77
+ | `max_repeats` | `3` | Maximum number of previously accepted identical prompts |
78
+ | `window_seconds` | `60` | Time window used for request tracking |
79
+
80
+ 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`.
81
+
82
+ ## Token estimation
83
+
84
+ TokenGuard estimates token usage using prompt length:
85
+
86
+ ```python
87
+ estimated_tokens = max(1, (len(text) + 3) // 4)
88
+ ```
89
+
90
+ This is a rough estimate, not an exact model tokenizer. Actual token counts vary by AI model and text content.
91
+
92
+ ## Run tests
93
+
94
+ Run the test suite with Python:
95
+
96
+ ```bash
97
+ python -m unittest -v
98
+ ```
99
+
100
+ ## Limitations
101
+
102
+ - Request history is stored in memory and resets when the process restarts.
103
+ - Limits apply to a single `TokenGuard` instance, not automatically across multiple processes or servers.
104
+ - TokenGuard does not contact AI providers or validate their actual token usage.
105
+ - Applications must call `guard.check(prompt)` before sending a request to their AI provider.
106
+ - This package does not replace authentication, authorization, or provider-side rate limits.
107
+
108
+ ## Project status
109
+
110
+ TokenGuard is an early-stage project focused on lightweight, local request protection for Python AI applications.
111
+
112
+ ## License
113
+
114
+ See the repository's license file for usage and distribution terms.
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ token_guard.py
5
+ tokenguard_local.egg-info/PKG-INFO
6
+ tokenguard_local.egg-info/SOURCES.txt
7
+ tokenguard_local.egg-info/dependency_links.txt
8
+ tokenguard_local.egg-info/top_level.txt