token-guardian 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.
Files changed (39) hide show
  1. token_guardian-0.1.0/.gitignore +9 -0
  2. token_guardian-0.1.0/LICENSE +21 -0
  3. token_guardian-0.1.0/PKG-INFO +293 -0
  4. token_guardian-0.1.0/README.md +262 -0
  5. token_guardian-0.1.0/app/__init__.py +2 -0
  6. token_guardian-0.1.0/app/cli.py +679 -0
  7. token_guardian-0.1.0/app/models/__init__.py +2 -0
  8. token_guardian-0.1.0/app/models/schemas.py +74 -0
  9. token_guardian-0.1.0/app/providers/__init__.py +2 -0
  10. token_guardian-0.1.0/app/providers/base.py +50 -0
  11. token_guardian-0.1.0/app/providers/catalog.py +14 -0
  12. token_guardian-0.1.0/app/providers/catalog_loader.py +53 -0
  13. token_guardian-0.1.0/app/providers/data/model_catalog.json +65 -0
  14. token_guardian-0.1.0/app/providers/registry.py +28 -0
  15. token_guardian-0.1.0/app/providers/sources/__init__.py +12 -0
  16. token_guardian-0.1.0/app/providers/sources/anthropic.py +31 -0
  17. token_guardian-0.1.0/app/providers/sources/google.py +31 -0
  18. token_guardian-0.1.0/app/providers/sources/openai.py +21 -0
  19. token_guardian-0.1.0/app/providers/sources/openrouter.py +21 -0
  20. token_guardian-0.1.0/app/providers/sync_service.py +55 -0
  21. token_guardian-0.1.0/app/services/__init__.py +2 -0
  22. token_guardian-0.1.0/app/services/analyzer_service.py +92 -0
  23. token_guardian-0.1.0/app/services/database.py +34 -0
  24. token_guardian-0.1.0/app/services/metrics_service.py +78 -0
  25. token_guardian-0.1.0/app/services/optimizer_service.py +45 -0
  26. token_guardian-0.1.0/app/utils/__init__.py +2 -0
  27. token_guardian-0.1.0/app/utils/prompt_analysis.py +84 -0
  28. token_guardian-0.1.0/app/utils/token_estimator.py +24 -0
  29. token_guardian-0.1.0/docs/.gitkeep +1 -0
  30. token_guardian-0.1.0/pyproject.toml +92 -0
  31. token_guardian-0.1.0/tests/__init__.py +2 -0
  32. token_guardian-0.1.0/tests/conftest.py +20 -0
  33. token_guardian-0.1.0/tests/test_analyzer_service.py +38 -0
  34. token_guardian-0.1.0/tests/test_cli.py +347 -0
  35. token_guardian-0.1.0/tests/test_metrics_service.py +18 -0
  36. token_guardian-0.1.0/tests/test_optimizer_service.py +14 -0
  37. token_guardian-0.1.0/tests/test_prompt_analysis.py +34 -0
  38. token_guardian-0.1.0/tests/test_provider_catalog.py +77 -0
  39. token_guardian-0.1.0/tests/test_token_estimator.py +10 -0
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .pytest_cache/
4
+ .mypy_cache/
5
+ .ruff_cache/
6
+ .coverage
7
+ htmlcov/
8
+ token_guardian.db
9
+ .venv/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Token Guardian Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,293 @@
1
+ Metadata-Version: 2.4
2
+ Name: token-guardian
3
+ Version: 0.1.0
4
+ Summary: CLI-first prompt guardrail for token, cost, and context observability.
5
+ Project-URL: Homepage, https://pypi.org/project/token-guardian/
6
+ Project-URL: Repository, https://github.com/LeonardoCesca/token-guardian
7
+ Project-URL: Issues, https://github.com/LeonardoCesca/token-guardian/issues
8
+ Author: Token Guardian Contributors
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: cli,cost,guardrail,llm,prompt,tokens
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Topic :: Utilities
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: inquirerpy<1.0.0,>=0.3.4
22
+ Requires-Dist: pydantic<3.0.0,>=2.8.0
23
+ Requires-Dist: rich<14.0.0,>=13.9.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: black<25.0.0,>=24.8.0; extra == 'dev'
26
+ Requires-Dist: mypy<2.0.0,>=1.11.0; extra == 'dev'
27
+ Requires-Dist: pytest-cov<6.0.0,>=5.0.0; extra == 'dev'
28
+ Requires-Dist: pytest<9.0.0,>=8.3.0; extra == 'dev'
29
+ Requires-Dist: ruff<1.0.0,>=0.6.0; extra == 'dev'
30
+ Description-Content-Type: text/markdown
31
+
32
+ # Token Guardian
33
+
34
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12%2B-2F81F7)](https://www.python.org/)
35
+ [![CLI](https://img.shields.io/badge/interface-CLI-00C2FF)](#usage)
36
+ [![License: MIT](https://img.shields.io/badge/license-MIT-8CE99A)](#license)
37
+ [![Tests](https://img.shields.io/badge/tests-pytest-6EA8FE)](#development)
38
+
39
+ Token Guardian is a CLI-first guardrail for checking prompt size, context pressure, and estimated cost before you call an LLM.
40
+
41
+ It helps developers answer three questions quickly:
42
+
43
+ - how many tokens this prompt will probably use
44
+ - how much this request may cost
45
+ - whether this prompt is risky for the selected context window
46
+
47
+ ## Why Use Token Guardian?
48
+
49
+ - catch oversized prompts before they hit the model
50
+ - estimate cost before expensive runs
51
+ - compare supported models using the same prompt
52
+ - clean duplicated or bloated prompt text
53
+ - keep simple local observability with SQLite metrics
54
+ - start from an interactive terminal menu instead of memorizing commands
55
+
56
+ ## Copy-Paste Install
57
+
58
+ ### Windows PowerShell
59
+
60
+ ```powershell
61
+ python -m venv .venv
62
+ .\.venv\Scripts\Activate.ps1
63
+ pip install -e .[dev]
64
+ token-guardian
65
+ ```
66
+
67
+ ### macOS / Linux
68
+
69
+ ```bash
70
+ python -m venv .venv
71
+ source .venv/bin/activate
72
+ pip install -e .[dev]
73
+ token-guardian
74
+ ```
75
+
76
+ Running `token-guardian` without arguments opens an interactive menu when your terminal supports it.
77
+
78
+ ## What the CLI Can Do
79
+
80
+ - analyze one prompt for one provider/model pair
81
+ - compare one prompt across the default supported models
82
+ - optimize prompt text by removing duplicates and excess whitespace
83
+ - list supported models
84
+ - sync the local model catalog snapshot
85
+ - inspect local usage metrics
86
+ - guide the full flow through an interactive menu
87
+
88
+ ## Usage
89
+
90
+ ### Start here
91
+
92
+ ```bash
93
+ token-guardian
94
+ ```
95
+
96
+ Running without arguments shows the available flow and the most useful commands to start with.
97
+
98
+ In interactive terminals, Token Guardian opens a guided menu with:
99
+
100
+ - provider selection
101
+ - model selection
102
+ - prompt entry with Enter to send
103
+ - sync selection by provider
104
+ - quick access to models and metrics
105
+
106
+ ### Analyze a prompt
107
+
108
+ ```bash
109
+ token-guardian analyze \
110
+ --provider anthropic \
111
+ --model claude-sonnet-4 \
112
+ --prompt "Review this architecture proposal and identify risks."
113
+ ```
114
+
115
+ ### Compare models
116
+
117
+ ```bash
118
+ token-guardian compare \
119
+ --prompt "Summarize this technical RFC and list migration risks."
120
+ ```
121
+
122
+ ### Optimize a prompt
123
+
124
+ ```bash
125
+ token-guardian optimize \
126
+ --prompt "Goal: summarize
127
+ Goal: summarize
128
+
129
+
130
+ Return bullets only."
131
+ ```
132
+
133
+ ### List supported models
134
+
135
+ ```bash
136
+ token-guardian models
137
+ ```
138
+
139
+ ### Sync model catalog
140
+
141
+ ```bash
142
+ token-guardian sync-models
143
+ token-guardian sync-models --provider openai
144
+ ```
145
+
146
+ ### View local metrics
147
+
148
+ ```bash
149
+ token-guardian metrics
150
+ ```
151
+
152
+ ## Example Output
153
+
154
+ Typical `analyze` output:
155
+
156
+ ```json
157
+ {
158
+ "provider": "anthropic",
159
+ "model": "claude-sonnet-4",
160
+ "input_tokens": 20,
161
+ "estimated_output_tokens": 128,
162
+ "estimated_total_tokens": 148,
163
+ "context_limit": 200000,
164
+ "context_usage_percent": 0.07,
165
+ "estimated_cost_usd": 0.00198,
166
+ "risk_level": "low",
167
+ "context_health_score": 99,
168
+ "cost_score": "$",
169
+ "complexity_score": "Simple",
170
+ "suggestions": [
171
+ "Prompt enxuto e com bom sinal; mantenha foco em objetivo, restricoes e saida."
172
+ ]
173
+ }
174
+ ```
175
+
176
+ ## Supported Providers
177
+
178
+ Current built-in catalog:
179
+
180
+ - OpenAI: `gpt-4.1`
181
+ - Anthropic: `claude-sonnet-4`, `claude-opus-4`
182
+ - Google: `gemini-2.5-pro`, `gemini-2.5-flash`
183
+ - OpenRouter: `openai/gpt-4.1`
184
+
185
+ Each model stores:
186
+
187
+ - context limit
188
+ - input price per 1K tokens
189
+ - output price per 1K tokens
190
+ - speed estimate
191
+ - source URL
192
+
193
+ The CLI also shows catalog metadata such as:
194
+
195
+ - `Catalogo atualizado em 2026-06-13`
196
+ - the current JSON snapshot path
197
+
198
+ ## Scoring
199
+
200
+ ### Risk level
201
+
202
+ Based on estimated context usage:
203
+
204
+ - `low`
205
+ - `medium`
206
+ - `high`
207
+ - `critical`
208
+
209
+ ### Context health score
210
+
211
+ Range: `0` to `100`
212
+
213
+ Factors include:
214
+
215
+ - prompt size
216
+ - repeated lines
217
+ - repeated vocabulary
218
+ - redundant sections
219
+
220
+ ### Cost score
221
+
222
+ - `$`: very low
223
+ - `$$`: low
224
+ - `$$$`: medium
225
+ - `$$$$`: high
226
+
227
+ ### Complexity score
228
+
229
+ - `Simple`
230
+ - `Medium`
231
+ - `Complex`
232
+ - `Very Complex`
233
+
234
+ ## Metrics
235
+
236
+ Token Guardian stores local metrics in SQLite.
237
+
238
+ Database file:
239
+
240
+ - `token_guardian.db`
241
+
242
+ Tracked fields include:
243
+
244
+ - total requests
245
+ - total tokens
246
+ - estimated cumulative cost
247
+ - top models
248
+ - top providers
249
+
250
+ ## Project Structure
251
+
252
+ ```text
253
+ token-guardian/
254
+ |-- app/
255
+ | |-- cli.py
256
+ | |-- models/
257
+ | |-- providers/
258
+ | |-- services/
259
+ | `-- utils/
260
+ |-- docs/
261
+ |-- tests/
262
+ |-- LICENSE
263
+ |-- pyproject.toml
264
+ `-- README.md
265
+ ```
266
+
267
+ ## Development
268
+
269
+ Run tests:
270
+
271
+ ```bash
272
+ pytest
273
+ ```
274
+
275
+ Run quality checks:
276
+
277
+ ```bash
278
+ ruff check .
279
+ black --check .
280
+ mypy app
281
+ ```
282
+
283
+ ## Roadmap
284
+
285
+ - add richer interactive CLI flows
286
+ - expand supported model catalog
287
+ - improve prompt optimization heuristics
288
+ - add exportable reports
289
+ - add model catalog sync support
290
+
291
+ ## License
292
+
293
+ MIT
@@ -0,0 +1,262 @@
1
+ # Token Guardian
2
+
3
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12%2B-2F81F7)](https://www.python.org/)
4
+ [![CLI](https://img.shields.io/badge/interface-CLI-00C2FF)](#usage)
5
+ [![License: MIT](https://img.shields.io/badge/license-MIT-8CE99A)](#license)
6
+ [![Tests](https://img.shields.io/badge/tests-pytest-6EA8FE)](#development)
7
+
8
+ Token Guardian is a CLI-first guardrail for checking prompt size, context pressure, and estimated cost before you call an LLM.
9
+
10
+ It helps developers answer three questions quickly:
11
+
12
+ - how many tokens this prompt will probably use
13
+ - how much this request may cost
14
+ - whether this prompt is risky for the selected context window
15
+
16
+ ## Why Use Token Guardian?
17
+
18
+ - catch oversized prompts before they hit the model
19
+ - estimate cost before expensive runs
20
+ - compare supported models using the same prompt
21
+ - clean duplicated or bloated prompt text
22
+ - keep simple local observability with SQLite metrics
23
+ - start from an interactive terminal menu instead of memorizing commands
24
+
25
+ ## Copy-Paste Install
26
+
27
+ ### Windows PowerShell
28
+
29
+ ```powershell
30
+ python -m venv .venv
31
+ .\.venv\Scripts\Activate.ps1
32
+ pip install -e .[dev]
33
+ token-guardian
34
+ ```
35
+
36
+ ### macOS / Linux
37
+
38
+ ```bash
39
+ python -m venv .venv
40
+ source .venv/bin/activate
41
+ pip install -e .[dev]
42
+ token-guardian
43
+ ```
44
+
45
+ Running `token-guardian` without arguments opens an interactive menu when your terminal supports it.
46
+
47
+ ## What the CLI Can Do
48
+
49
+ - analyze one prompt for one provider/model pair
50
+ - compare one prompt across the default supported models
51
+ - optimize prompt text by removing duplicates and excess whitespace
52
+ - list supported models
53
+ - sync the local model catalog snapshot
54
+ - inspect local usage metrics
55
+ - guide the full flow through an interactive menu
56
+
57
+ ## Usage
58
+
59
+ ### Start here
60
+
61
+ ```bash
62
+ token-guardian
63
+ ```
64
+
65
+ Running without arguments shows the available flow and the most useful commands to start with.
66
+
67
+ In interactive terminals, Token Guardian opens a guided menu with:
68
+
69
+ - provider selection
70
+ - model selection
71
+ - prompt entry with Enter to send
72
+ - sync selection by provider
73
+ - quick access to models and metrics
74
+
75
+ ### Analyze a prompt
76
+
77
+ ```bash
78
+ token-guardian analyze \
79
+ --provider anthropic \
80
+ --model claude-sonnet-4 \
81
+ --prompt "Review this architecture proposal and identify risks."
82
+ ```
83
+
84
+ ### Compare models
85
+
86
+ ```bash
87
+ token-guardian compare \
88
+ --prompt "Summarize this technical RFC and list migration risks."
89
+ ```
90
+
91
+ ### Optimize a prompt
92
+
93
+ ```bash
94
+ token-guardian optimize \
95
+ --prompt "Goal: summarize
96
+ Goal: summarize
97
+
98
+
99
+ Return bullets only."
100
+ ```
101
+
102
+ ### List supported models
103
+
104
+ ```bash
105
+ token-guardian models
106
+ ```
107
+
108
+ ### Sync model catalog
109
+
110
+ ```bash
111
+ token-guardian sync-models
112
+ token-guardian sync-models --provider openai
113
+ ```
114
+
115
+ ### View local metrics
116
+
117
+ ```bash
118
+ token-guardian metrics
119
+ ```
120
+
121
+ ## Example Output
122
+
123
+ Typical `analyze` output:
124
+
125
+ ```json
126
+ {
127
+ "provider": "anthropic",
128
+ "model": "claude-sonnet-4",
129
+ "input_tokens": 20,
130
+ "estimated_output_tokens": 128,
131
+ "estimated_total_tokens": 148,
132
+ "context_limit": 200000,
133
+ "context_usage_percent": 0.07,
134
+ "estimated_cost_usd": 0.00198,
135
+ "risk_level": "low",
136
+ "context_health_score": 99,
137
+ "cost_score": "$",
138
+ "complexity_score": "Simple",
139
+ "suggestions": [
140
+ "Prompt enxuto e com bom sinal; mantenha foco em objetivo, restricoes e saida."
141
+ ]
142
+ }
143
+ ```
144
+
145
+ ## Supported Providers
146
+
147
+ Current built-in catalog:
148
+
149
+ - OpenAI: `gpt-4.1`
150
+ - Anthropic: `claude-sonnet-4`, `claude-opus-4`
151
+ - Google: `gemini-2.5-pro`, `gemini-2.5-flash`
152
+ - OpenRouter: `openai/gpt-4.1`
153
+
154
+ Each model stores:
155
+
156
+ - context limit
157
+ - input price per 1K tokens
158
+ - output price per 1K tokens
159
+ - speed estimate
160
+ - source URL
161
+
162
+ The CLI also shows catalog metadata such as:
163
+
164
+ - `Catalogo atualizado em 2026-06-13`
165
+ - the current JSON snapshot path
166
+
167
+ ## Scoring
168
+
169
+ ### Risk level
170
+
171
+ Based on estimated context usage:
172
+
173
+ - `low`
174
+ - `medium`
175
+ - `high`
176
+ - `critical`
177
+
178
+ ### Context health score
179
+
180
+ Range: `0` to `100`
181
+
182
+ Factors include:
183
+
184
+ - prompt size
185
+ - repeated lines
186
+ - repeated vocabulary
187
+ - redundant sections
188
+
189
+ ### Cost score
190
+
191
+ - `$`: very low
192
+ - `$$`: low
193
+ - `$$$`: medium
194
+ - `$$$$`: high
195
+
196
+ ### Complexity score
197
+
198
+ - `Simple`
199
+ - `Medium`
200
+ - `Complex`
201
+ - `Very Complex`
202
+
203
+ ## Metrics
204
+
205
+ Token Guardian stores local metrics in SQLite.
206
+
207
+ Database file:
208
+
209
+ - `token_guardian.db`
210
+
211
+ Tracked fields include:
212
+
213
+ - total requests
214
+ - total tokens
215
+ - estimated cumulative cost
216
+ - top models
217
+ - top providers
218
+
219
+ ## Project Structure
220
+
221
+ ```text
222
+ token-guardian/
223
+ |-- app/
224
+ | |-- cli.py
225
+ | |-- models/
226
+ | |-- providers/
227
+ | |-- services/
228
+ | `-- utils/
229
+ |-- docs/
230
+ |-- tests/
231
+ |-- LICENSE
232
+ |-- pyproject.toml
233
+ `-- README.md
234
+ ```
235
+
236
+ ## Development
237
+
238
+ Run tests:
239
+
240
+ ```bash
241
+ pytest
242
+ ```
243
+
244
+ Run quality checks:
245
+
246
+ ```bash
247
+ ruff check .
248
+ black --check .
249
+ mypy app
250
+ ```
251
+
252
+ ## Roadmap
253
+
254
+ - add richer interactive CLI flows
255
+ - expand supported model catalog
256
+ - improve prompt optimization heuristics
257
+ - add exportable reports
258
+ - add model catalog sync support
259
+
260
+ ## License
261
+
262
+ MIT
@@ -0,0 +1,2 @@
1
+ """Token Guardian MCP application package."""
2
+