token-diet-cli 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.
- token_diet_cli-0.1.0/LICENSE +21 -0
- token_diet_cli-0.1.0/PKG-INFO +304 -0
- token_diet_cli-0.1.0/README.md +281 -0
- token_diet_cli-0.1.0/pyproject.toml +31 -0
- token_diet_cli-0.1.0/setup.cfg +4 -0
- token_diet_cli-0.1.0/src/token_diet/__init__.py +38 -0
- token_diet_cli-0.1.0/src/token_diet/__main__.py +4 -0
- token_diet_cli-0.1.0/src/token_diet/cli.py +449 -0
- token_diet_cli-0.1.0/src/token_diet_cli.egg-info/PKG-INFO +304 -0
- token_diet_cli-0.1.0/src/token_diet_cli.egg-info/SOURCES.txt +12 -0
- token_diet_cli-0.1.0/src/token_diet_cli.egg-info/dependency_links.txt +1 -0
- token_diet_cli-0.1.0/src/token_diet_cli.egg-info/entry_points.txt +2 -0
- token_diet_cli-0.1.0/src/token_diet_cli.egg-info/top_level.txt +1 -0
- token_diet_cli-0.1.0/tests/test_token_audit.py +192 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 josephrotts
|
|
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,304 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: token-diet-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Diagnostic tools and methodology to audit and optimize token footprint in AI coding workflows
|
|
5
|
+
Author-email: josephrotts <387283+josephrotts@users.noreply.github.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: tokens,llm,claude,gemini,codex,opencode,developer-tools
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# token-diet
|
|
25
|
+
|
|
26
|
+
Audit token footprint and detect silent context leaks in AI agent workflows.
|
|
27
|
+
|
|
28
|
+
Zero-dependency CLI for **Claude Code**, **Cursor**, **Codex**, **Gemini**, and **OpenCode**.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Why?
|
|
33
|
+
|
|
34
|
+
AI coding assistants inject instruction files (`AGENTS.md`, `CLAUDE.md`, `.cursorrules`) into the system prompt on **every single turn**. A bloated 25 KB prompt file forces the model to re-process ~6,000 tokens on every message, draining rate limits and breaking prompt cache discounts.
|
|
35
|
+
|
|
36
|
+
Worse, some local tools install hidden `pre-commit` hooks that send your entire git diff to an LLM on every commit.
|
|
37
|
+
|
|
38
|
+
`token-diet` scans your repository or workstation in milliseconds to:
|
|
39
|
+
- **Audit prompt size:** Flags instruction files that exceed context budgets (`[OK]`, `[WARN]`, `[ALERT]`).
|
|
40
|
+
- **Catch rogue git hooks:** Detects and disables silent LLM review hooks in `.git/hooks/pre-commit` (`--fix-hooks`).
|
|
41
|
+
- **Guard CI pipelines:** Fails PR builds if prompt expansions exceed token thresholds (`--strict`, `--max-tokens`).
|
|
42
|
+
- **Zero dependencies:** Pure Python standard library (Python >= 3.8). Runs in < 10ms.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
### Via Homebrew (macOS & Linux)
|
|
49
|
+
```bash
|
|
50
|
+
brew install josephrotts/tap/token-diet
|
|
51
|
+
# Or:
|
|
52
|
+
brew tap josephrotts/tap
|
|
53
|
+
brew install token-diet
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Via `pipx` (Recommended for Isolated CLI)
|
|
57
|
+
```bash
|
|
58
|
+
pipx install token-diet-cli
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Via `pip` / `uv`
|
|
62
|
+
```bash
|
|
63
|
+
# Using pip:
|
|
64
|
+
pip install token-diet-cli
|
|
65
|
+
|
|
66
|
+
# Using uv:
|
|
67
|
+
uv tool install token-diet-cli
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Running Directly from Source (No Installation Needed)
|
|
71
|
+
```bash
|
|
72
|
+
git clone https://github.com/josephrotts/token-diet.git
|
|
73
|
+
cd token-diet
|
|
74
|
+
PYTHONPATH=src python3 -m token_diet
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Usage
|
|
80
|
+
|
|
81
|
+
### 1. Instant Health Check in Current Directory
|
|
82
|
+
Simply run `token-diet` in any project repository. It automatically scans local directives and checks for dangerous pre-commit hooks:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
token-diet
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Example Output:**
|
|
89
|
+
```text
|
|
90
|
+
================================================================================
|
|
91
|
+
TOKEN DIET: FOOTPRINT & CONTEXT AUDIT
|
|
92
|
+
================================================================================
|
|
93
|
+
File / Directive | Bytes | Tokens | Status
|
|
94
|
+
--------------------------------------------------------------------------------
|
|
95
|
+
/Users/developer/my-project/AGENTS.md | 962 | 240 | [OK]
|
|
96
|
+
/Users/developer/my-project/.cursorrules | 1,240 | 310 | [OK]
|
|
97
|
+
================================================================================
|
|
98
|
+
Total Bytes: 2,202 B
|
|
99
|
+
Total Tokens: ~550 estimated base tokens per turn
|
|
100
|
+
Files Detected: 2 of 2
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 2. Automated Alerts: Context Bloat & Invisible Pre-Commit Hooks
|
|
104
|
+
When a prompt file is oversized or a git hook silently burns tokens on every commit, `token-diet` issues an instant visual alert:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
token-diet
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Example Alert Output:**
|
|
111
|
+
```text
|
|
112
|
+
================================================================================
|
|
113
|
+
TOKEN DIET: FOOTPRINT & CONTEXT AUDIT
|
|
114
|
+
================================================================================
|
|
115
|
+
File / Directive | Bytes | Tokens | Status
|
|
116
|
+
--------------------------------------------------------------------------------
|
|
117
|
+
/Users/developer/my-project/AGENTS.md | 24,500 | 6,125 | [ALERT]
|
|
118
|
+
================================================================================
|
|
119
|
+
Total Bytes: 24,500 B
|
|
120
|
+
Total Tokens: ~6,125 estimated base tokens per turn
|
|
121
|
+
Files Detected: 1 of 1
|
|
122
|
+
|
|
123
|
+
🚨 IRREGULAR HOOK DETECTED:
|
|
124
|
+
• [pre-commit] Pre-commit hook invokes AI/LLM review tooling (gga, opencode). This causes invisible token consumption on every commit.
|
|
125
|
+
Path: /Users/developer/my-project/.git/hooks/pre-commit
|
|
126
|
+
💡 Tip: Run 'token-diet --fix-hooks' to automatically neutralize silent AI invocations.
|
|
127
|
+
|
|
128
|
+
⚠️ CONTEXT BLOAT DETECTED:
|
|
129
|
+
Files marked [ALERT] exceed 16 KB (>4k tokens) and severely penalize context windows.
|
|
130
|
+
Recommendation: Decouple procedures into On-Demand Skills using the LUT Router pattern.
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 3. Auto-Fix Silent Pre-Commit Hooks (`--fix-hooks`)
|
|
134
|
+
Neutralize unmonitored AI review tooling in `.git/hooks/pre-commit` in a single command with an automatic backup (`.git/hooks/pre-commit.bak`):
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
token-diet --fix-hooks
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Example Output:**
|
|
141
|
+
```text
|
|
142
|
+
🛠️ AUTO-REMEDIATION: PRE-COMMIT HOOKS NEUTRALIZED
|
|
143
|
+
• Backup saved: /Users/developer/my-project/.git/hooks/pre-commit.bak
|
|
144
|
+
• Neutralized 1 AI review invocation(s).
|
|
145
|
+
• Safe pre-commit hook is active without silent token bleed.
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 4. Audit System-Wide Global Prompts (`--global`)
|
|
149
|
+
Scan default assistant instruction files across your entire workstation (`~/.claude/CLAUDE.md`, `~/.codex/AGENTS.md`, `~/.gemini/GEMINI.md`, etc.):
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
token-diet --global
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 5. Audit Specific Paths or Folders
|
|
156
|
+
```bash
|
|
157
|
+
# Audit a single file
|
|
158
|
+
token-diet AGENTS.md
|
|
159
|
+
|
|
160
|
+
# Audit entire directories recursively
|
|
161
|
+
token-diet docs/
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 6. Automated CI / Pre-Commit Gate
|
|
165
|
+
Integrate into your CI pipeline or Git pre-commit workflow to prevent prompt regressions:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
# Fail CI if prompt footprint exceeds 2,500 tokens:
|
|
169
|
+
token-diet --max-tokens 2500 AGENTS.md
|
|
170
|
+
|
|
171
|
+
# Fail CI if any file triggers a [WARN] or [ALERT]:
|
|
172
|
+
token-diet --strict docs/
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 7. Structured Telemetry Output (`--json`)
|
|
176
|
+
Emit structured JSON for automated pipelines, threshold assertions, or telemetry dashboards:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
token-diet --json
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Health Status Indicators
|
|
185
|
+
|
|
186
|
+
| Indicator | Byte Threshold | Estimated Tokens | What it Means & Recommended Action |
|
|
187
|
+
|---|:---:|:---:|---|
|
|
188
|
+
| 🟢 `[OK]` | `< 4 KB` | `< 1,000 tokens` | **Optimal Context:** Fits cleanly within prompt cache. Preserves fast, cheap inference. |
|
|
189
|
+
| 🟡 `[WARN]` | `4 KB – 16 KB` | `1,000 – 4,000 tokens` | **Moderate Size:** Acceptable, but review for duplicate instructions or unneeded verbosity. |
|
|
190
|
+
| 🔴 `[ALERT]` | `> 16 KB` | `> 4,000 tokens` | **Severe Prompt Bloat:** Monolithic rules degrade reasoning and waste context budget every turn. Decouple into On-Demand Skills. |
|
|
191
|
+
| 🚨 `[HOOK ALERT]` | — | — | **Hidden Token Bleed:** Local `.git/hooks/pre-commit` sends code to cloud LLMs on every `git commit`. Replace with static linters. |
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## What To Do When a Leak is Detected (Action Paths)
|
|
196
|
+
|
|
197
|
+
When `token-diet` flags an irregularity, follow the corresponding action path (detailed runbooks available in the [Remediation Playbook](docs/remediation-playbook.md)):
|
|
198
|
+
|
|
199
|
+
### Path A: When an Irregular Git Hook is Flagged (`🚨 IRREGULAR HOOK DETECTED`)
|
|
200
|
+
* **Problem:** A local hook inside `.git/hooks/pre-commit` secretly sends git diffs to cloud AI tools on every single commit.
|
|
201
|
+
* **Action Path:**
|
|
202
|
+
1. Inspect the script: `cat .git/hooks/pre-commit`
|
|
203
|
+
2. Remove or comment out the automated AI review invocation.
|
|
204
|
+
3. Replace it with fast, local deterministic linters (`ruff`, `eslint`, `biome`).
|
|
205
|
+
4. Relocate semantic AI reviews to **Pull Request gates in CI** (see [CI/CD Integration](docs/ci-integration.md)), where reviews are metered and visible.
|
|
206
|
+
|
|
207
|
+
### Path B: When an Agent Directive is Bloated (`🔴 [ALERT]`)
|
|
208
|
+
* **Problem:** A root instruction file (`AGENTS.md`, `CLAUDE.md`, `.cursorrules`) exceeds 16 KB (> 4,000 tokens), exhausting your token budget on every turn and destroying cache discounts.
|
|
209
|
+
* **Action Path (The Look-Up Table / LUT Router Pattern):**
|
|
210
|
+
1. **Extract Specialized Protocols:** Move detailed procedure manuals, test specifications, and review arbitrations into standalone on-demand skills (e.g., `skills/<skill-name>/SKILL.md`).
|
|
211
|
+
2. **Compress Root Directive:** Keep only core behavioral invariants and a routing index in the root file (< 3 KB / < 750 tokens):
|
|
212
|
+
```markdown
|
|
213
|
+
## On-Demand Skills
|
|
214
|
+
- When running database migrations, read `skills/db-migrate/SKILL.md`.
|
|
215
|
+
- When preparing a release, read `skills/oss-publisher/SKILL.md`.
|
|
216
|
+
```
|
|
217
|
+
3. Re-run `token-diet` to confirm the status turns 🟢 `[OK]`.
|
|
218
|
+
|
|
219
|
+
### Path C: When CI Fails on Token Budget Exceeded (`exit code 1`)
|
|
220
|
+
* **Problem:** A Pull Request introduced unbudgeted prompt expansion that violated `--max-tokens <N>` or `--strict`.
|
|
221
|
+
* **Action Path:**
|
|
222
|
+
1. Run `token-diet --json` to get the machine-readable breakdown of offending files.
|
|
223
|
+
2. Prune duplicate rules or extract text into skills.
|
|
224
|
+
3. Re-run local check before pushing: `token-diet --strict`.
|
|
225
|
+
|
|
226
|
+
### Agentic Remediation: Prompt for AI Assistants
|
|
227
|
+
To have your AI coding assistant (Claude Code, Gemini CLI, Cursor, Windsurf, OpenCode) remediate prompt bloat autonomously without manual edits, copy and paste this prompt:
|
|
228
|
+
|
|
229
|
+
```text
|
|
230
|
+
Run `token-diet` in this repository. If you detect any files marked [ALERT] or [WARN], follow `docs/remediation-playbook.md` to decouple operational procedures into on-demand skills under `skills/<skill-name>/SKILL.md`. Compress the root instruction file to a concise Look-Up Table (LUT Router) under 3 KB (< 750 tokens). If an irregular hook is flagged, run `token-diet --fix-hooks`. Verify that all tests pass and `token-diet` reports [OK].
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## Running Tests
|
|
236
|
+
|
|
237
|
+
Token Diet includes an automated test suite with deterministic coverage:
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
python3 -m unittest discover -s tests -p 'test_*.py'
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Repository Structure
|
|
246
|
+
|
|
247
|
+
```
|
|
248
|
+
token-diet/
|
|
249
|
+
├── .github/
|
|
250
|
+
│ └── workflows/
|
|
251
|
+
│ └── ci.yml # Automated multi-version CI test runner
|
|
252
|
+
├── docs/
|
|
253
|
+
│ ├── remediation-playbook.md # Step-by-step token containment and LUT routing
|
|
254
|
+
│ └── ci-integration.md # CI workflows & pre-commit gatekeeper examples
|
|
255
|
+
├── src/
|
|
256
|
+
│ └── token_diet/ # Core Python package (Zero external dependencies)
|
|
257
|
+
│ ├── __init__.py
|
|
258
|
+
│ ├── __main__.py
|
|
259
|
+
│ └── cli.py # CLI implementation and footprint engine
|
|
260
|
+
├── tests/
|
|
261
|
+
│ └── test_token_audit.py # Automated unit test suite (TDD)
|
|
262
|
+
├── .gitignore
|
|
263
|
+
├── AGENTS.md # Agent-friendly repository directives
|
|
264
|
+
├── CONTRIBUTING.md # Contribution guidelines & engineering principles
|
|
265
|
+
├── LICENSE # MIT Open Source License
|
|
266
|
+
├── pyproject.toml # PEP 621 packaging and CLI entrypoint
|
|
267
|
+
└── README.md # Project overview and documentation
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## Core Methodology: The 5 Vectors of Token Leakage
|
|
273
|
+
|
|
274
|
+
Token Diet is built on empirical analysis of how developer environments silently deplete API quotas:
|
|
275
|
+
|
|
276
|
+
1. **Global Prompt Bloat:** Monolithic instruction files (`CLAUDE.md`, `AGENTS.md`) exceeding 20 KB inject tens of thousands of static tokens on every turn. *Solution: Keep root directives under 3 KB (<750 tokens) and route to on-demand skills.*
|
|
277
|
+
2. **Invisible Git Hooks:** Pre-commit hooks that intercept local commits to invoke cloud LLM reviews drain quotas without developer consent. *Solution: Keep local commits deterministic; reserve AI reviews for PR gates.*
|
|
278
|
+
3. **Reasoning Effort Inflation:** Running frontier models at `high` reasoning effort for simple syntax or diagnostic queries generates thousands of hidden chain-of-thought tokens. *Solution: Default to `medium` or `low`.*
|
|
279
|
+
4. **Static MCP Schemas:** Registering dozens of MCP tools serializes massive JSON parameter schemas into system prompts. *Solution: Prune unused tool servers and enforce lazy-loading.*
|
|
280
|
+
5. **Prompt Cache Invalidation:** Injecting dynamic timestamps or changing session IDs at the start of instructions destroys provider prompt caching discounts (up to 90%). *Solution: Maintain immutable instruction prefixes.*
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## Platform Compatibility
|
|
285
|
+
|
|
286
|
+
Token Diet is OS-agnostic and cross-platform:
|
|
287
|
+
- **CLI Diagnostic Tool:** Pure Python standard library with zero third-party dependencies. Runs seamlessly on **macOS**, **Linux**, and **Windows** (Python >= 3.8).
|
|
288
|
+
- **Universal Methodology:** Core principles apply across all assistants (Claude Code, Gemini CLI, OpenAI Codex, OpenCode, Cursor, Windsurf).
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## Documentation & Guides
|
|
293
|
+
|
|
294
|
+
For in-depth operational procedures and automated pipeline setups:
|
|
295
|
+
- **[Remediation Playbook](docs/remediation-playbook.md):** Step-by-step runbook to prune prompt files, configure reasoning effort, and contain background services.
|
|
296
|
+
- **[CI/CD Integration](docs/ci-integration.md):** GitHub Actions workflows and pre-commit configurations for automated token budget enforcement.
|
|
297
|
+
- **[Agent Directives (AGENTS.md)](AGENTS.md):** Operational guidelines for AI coding assistants working on this codebase.
|
|
298
|
+
- **[Contributing Guidelines](CONTRIBUTING.md):** How to run tests, submit pull requests, and coding standards.
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
This project is licensed under the terms of the [MIT License](LICENSE).
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# token-diet
|
|
2
|
+
|
|
3
|
+
Audit token footprint and detect silent context leaks in AI agent workflows.
|
|
4
|
+
|
|
5
|
+
Zero-dependency CLI for **Claude Code**, **Cursor**, **Codex**, **Gemini**, and **OpenCode**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Why?
|
|
10
|
+
|
|
11
|
+
AI coding assistants inject instruction files (`AGENTS.md`, `CLAUDE.md`, `.cursorrules`) into the system prompt on **every single turn**. A bloated 25 KB prompt file forces the model to re-process ~6,000 tokens on every message, draining rate limits and breaking prompt cache discounts.
|
|
12
|
+
|
|
13
|
+
Worse, some local tools install hidden `pre-commit` hooks that send your entire git diff to an LLM on every commit.
|
|
14
|
+
|
|
15
|
+
`token-diet` scans your repository or workstation in milliseconds to:
|
|
16
|
+
- **Audit prompt size:** Flags instruction files that exceed context budgets (`[OK]`, `[WARN]`, `[ALERT]`).
|
|
17
|
+
- **Catch rogue git hooks:** Detects and disables silent LLM review hooks in `.git/hooks/pre-commit` (`--fix-hooks`).
|
|
18
|
+
- **Guard CI pipelines:** Fails PR builds if prompt expansions exceed token thresholds (`--strict`, `--max-tokens`).
|
|
19
|
+
- **Zero dependencies:** Pure Python standard library (Python >= 3.8). Runs in < 10ms.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
### Via Homebrew (macOS & Linux)
|
|
26
|
+
```bash
|
|
27
|
+
brew install josephrotts/tap/token-diet
|
|
28
|
+
# Or:
|
|
29
|
+
brew tap josephrotts/tap
|
|
30
|
+
brew install token-diet
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Via `pipx` (Recommended for Isolated CLI)
|
|
34
|
+
```bash
|
|
35
|
+
pipx install token-diet-cli
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Via `pip` / `uv`
|
|
39
|
+
```bash
|
|
40
|
+
# Using pip:
|
|
41
|
+
pip install token-diet-cli
|
|
42
|
+
|
|
43
|
+
# Using uv:
|
|
44
|
+
uv tool install token-diet-cli
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Running Directly from Source (No Installation Needed)
|
|
48
|
+
```bash
|
|
49
|
+
git clone https://github.com/josephrotts/token-diet.git
|
|
50
|
+
cd token-diet
|
|
51
|
+
PYTHONPATH=src python3 -m token_diet
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
### 1. Instant Health Check in Current Directory
|
|
59
|
+
Simply run `token-diet` in any project repository. It automatically scans local directives and checks for dangerous pre-commit hooks:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
token-diet
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Example Output:**
|
|
66
|
+
```text
|
|
67
|
+
================================================================================
|
|
68
|
+
TOKEN DIET: FOOTPRINT & CONTEXT AUDIT
|
|
69
|
+
================================================================================
|
|
70
|
+
File / Directive | Bytes | Tokens | Status
|
|
71
|
+
--------------------------------------------------------------------------------
|
|
72
|
+
/Users/developer/my-project/AGENTS.md | 962 | 240 | [OK]
|
|
73
|
+
/Users/developer/my-project/.cursorrules | 1,240 | 310 | [OK]
|
|
74
|
+
================================================================================
|
|
75
|
+
Total Bytes: 2,202 B
|
|
76
|
+
Total Tokens: ~550 estimated base tokens per turn
|
|
77
|
+
Files Detected: 2 of 2
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 2. Automated Alerts: Context Bloat & Invisible Pre-Commit Hooks
|
|
81
|
+
When a prompt file is oversized or a git hook silently burns tokens on every commit, `token-diet` issues an instant visual alert:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
token-diet
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**Example Alert Output:**
|
|
88
|
+
```text
|
|
89
|
+
================================================================================
|
|
90
|
+
TOKEN DIET: FOOTPRINT & CONTEXT AUDIT
|
|
91
|
+
================================================================================
|
|
92
|
+
File / Directive | Bytes | Tokens | Status
|
|
93
|
+
--------------------------------------------------------------------------------
|
|
94
|
+
/Users/developer/my-project/AGENTS.md | 24,500 | 6,125 | [ALERT]
|
|
95
|
+
================================================================================
|
|
96
|
+
Total Bytes: 24,500 B
|
|
97
|
+
Total Tokens: ~6,125 estimated base tokens per turn
|
|
98
|
+
Files Detected: 1 of 1
|
|
99
|
+
|
|
100
|
+
🚨 IRREGULAR HOOK DETECTED:
|
|
101
|
+
• [pre-commit] Pre-commit hook invokes AI/LLM review tooling (gga, opencode). This causes invisible token consumption on every commit.
|
|
102
|
+
Path: /Users/developer/my-project/.git/hooks/pre-commit
|
|
103
|
+
💡 Tip: Run 'token-diet --fix-hooks' to automatically neutralize silent AI invocations.
|
|
104
|
+
|
|
105
|
+
⚠️ CONTEXT BLOAT DETECTED:
|
|
106
|
+
Files marked [ALERT] exceed 16 KB (>4k tokens) and severely penalize context windows.
|
|
107
|
+
Recommendation: Decouple procedures into On-Demand Skills using the LUT Router pattern.
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 3. Auto-Fix Silent Pre-Commit Hooks (`--fix-hooks`)
|
|
111
|
+
Neutralize unmonitored AI review tooling in `.git/hooks/pre-commit` in a single command with an automatic backup (`.git/hooks/pre-commit.bak`):
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
token-diet --fix-hooks
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Example Output:**
|
|
118
|
+
```text
|
|
119
|
+
🛠️ AUTO-REMEDIATION: PRE-COMMIT HOOKS NEUTRALIZED
|
|
120
|
+
• Backup saved: /Users/developer/my-project/.git/hooks/pre-commit.bak
|
|
121
|
+
• Neutralized 1 AI review invocation(s).
|
|
122
|
+
• Safe pre-commit hook is active without silent token bleed.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 4. Audit System-Wide Global Prompts (`--global`)
|
|
126
|
+
Scan default assistant instruction files across your entire workstation (`~/.claude/CLAUDE.md`, `~/.codex/AGENTS.md`, `~/.gemini/GEMINI.md`, etc.):
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
token-diet --global
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 5. Audit Specific Paths or Folders
|
|
133
|
+
```bash
|
|
134
|
+
# Audit a single file
|
|
135
|
+
token-diet AGENTS.md
|
|
136
|
+
|
|
137
|
+
# Audit entire directories recursively
|
|
138
|
+
token-diet docs/
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 6. Automated CI / Pre-Commit Gate
|
|
142
|
+
Integrate into your CI pipeline or Git pre-commit workflow to prevent prompt regressions:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
# Fail CI if prompt footprint exceeds 2,500 tokens:
|
|
146
|
+
token-diet --max-tokens 2500 AGENTS.md
|
|
147
|
+
|
|
148
|
+
# Fail CI if any file triggers a [WARN] or [ALERT]:
|
|
149
|
+
token-diet --strict docs/
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### 7. Structured Telemetry Output (`--json`)
|
|
153
|
+
Emit structured JSON for automated pipelines, threshold assertions, or telemetry dashboards:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
token-diet --json
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Health Status Indicators
|
|
162
|
+
|
|
163
|
+
| Indicator | Byte Threshold | Estimated Tokens | What it Means & Recommended Action |
|
|
164
|
+
|---|:---:|:---:|---|
|
|
165
|
+
| 🟢 `[OK]` | `< 4 KB` | `< 1,000 tokens` | **Optimal Context:** Fits cleanly within prompt cache. Preserves fast, cheap inference. |
|
|
166
|
+
| 🟡 `[WARN]` | `4 KB – 16 KB` | `1,000 – 4,000 tokens` | **Moderate Size:** Acceptable, but review for duplicate instructions or unneeded verbosity. |
|
|
167
|
+
| 🔴 `[ALERT]` | `> 16 KB` | `> 4,000 tokens` | **Severe Prompt Bloat:** Monolithic rules degrade reasoning and waste context budget every turn. Decouple into On-Demand Skills. |
|
|
168
|
+
| 🚨 `[HOOK ALERT]` | — | — | **Hidden Token Bleed:** Local `.git/hooks/pre-commit` sends code to cloud LLMs on every `git commit`. Replace with static linters. |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## What To Do When a Leak is Detected (Action Paths)
|
|
173
|
+
|
|
174
|
+
When `token-diet` flags an irregularity, follow the corresponding action path (detailed runbooks available in the [Remediation Playbook](docs/remediation-playbook.md)):
|
|
175
|
+
|
|
176
|
+
### Path A: When an Irregular Git Hook is Flagged (`🚨 IRREGULAR HOOK DETECTED`)
|
|
177
|
+
* **Problem:** A local hook inside `.git/hooks/pre-commit` secretly sends git diffs to cloud AI tools on every single commit.
|
|
178
|
+
* **Action Path:**
|
|
179
|
+
1. Inspect the script: `cat .git/hooks/pre-commit`
|
|
180
|
+
2. Remove or comment out the automated AI review invocation.
|
|
181
|
+
3. Replace it with fast, local deterministic linters (`ruff`, `eslint`, `biome`).
|
|
182
|
+
4. Relocate semantic AI reviews to **Pull Request gates in CI** (see [CI/CD Integration](docs/ci-integration.md)), where reviews are metered and visible.
|
|
183
|
+
|
|
184
|
+
### Path B: When an Agent Directive is Bloated (`🔴 [ALERT]`)
|
|
185
|
+
* **Problem:** A root instruction file (`AGENTS.md`, `CLAUDE.md`, `.cursorrules`) exceeds 16 KB (> 4,000 tokens), exhausting your token budget on every turn and destroying cache discounts.
|
|
186
|
+
* **Action Path (The Look-Up Table / LUT Router Pattern):**
|
|
187
|
+
1. **Extract Specialized Protocols:** Move detailed procedure manuals, test specifications, and review arbitrations into standalone on-demand skills (e.g., `skills/<skill-name>/SKILL.md`).
|
|
188
|
+
2. **Compress Root Directive:** Keep only core behavioral invariants and a routing index in the root file (< 3 KB / < 750 tokens):
|
|
189
|
+
```markdown
|
|
190
|
+
## On-Demand Skills
|
|
191
|
+
- When running database migrations, read `skills/db-migrate/SKILL.md`.
|
|
192
|
+
- When preparing a release, read `skills/oss-publisher/SKILL.md`.
|
|
193
|
+
```
|
|
194
|
+
3. Re-run `token-diet` to confirm the status turns 🟢 `[OK]`.
|
|
195
|
+
|
|
196
|
+
### Path C: When CI Fails on Token Budget Exceeded (`exit code 1`)
|
|
197
|
+
* **Problem:** A Pull Request introduced unbudgeted prompt expansion that violated `--max-tokens <N>` or `--strict`.
|
|
198
|
+
* **Action Path:**
|
|
199
|
+
1. Run `token-diet --json` to get the machine-readable breakdown of offending files.
|
|
200
|
+
2. Prune duplicate rules or extract text into skills.
|
|
201
|
+
3. Re-run local check before pushing: `token-diet --strict`.
|
|
202
|
+
|
|
203
|
+
### Agentic Remediation: Prompt for AI Assistants
|
|
204
|
+
To have your AI coding assistant (Claude Code, Gemini CLI, Cursor, Windsurf, OpenCode) remediate prompt bloat autonomously without manual edits, copy and paste this prompt:
|
|
205
|
+
|
|
206
|
+
```text
|
|
207
|
+
Run `token-diet` in this repository. If you detect any files marked [ALERT] or [WARN], follow `docs/remediation-playbook.md` to decouple operational procedures into on-demand skills under `skills/<skill-name>/SKILL.md`. Compress the root instruction file to a concise Look-Up Table (LUT Router) under 3 KB (< 750 tokens). If an irregular hook is flagged, run `token-diet --fix-hooks`. Verify that all tests pass and `token-diet` reports [OK].
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Running Tests
|
|
213
|
+
|
|
214
|
+
Token Diet includes an automated test suite with deterministic coverage:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
python3 -m unittest discover -s tests -p 'test_*.py'
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## Repository Structure
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
token-diet/
|
|
226
|
+
├── .github/
|
|
227
|
+
│ └── workflows/
|
|
228
|
+
│ └── ci.yml # Automated multi-version CI test runner
|
|
229
|
+
├── docs/
|
|
230
|
+
│ ├── remediation-playbook.md # Step-by-step token containment and LUT routing
|
|
231
|
+
│ └── ci-integration.md # CI workflows & pre-commit gatekeeper examples
|
|
232
|
+
├── src/
|
|
233
|
+
│ └── token_diet/ # Core Python package (Zero external dependencies)
|
|
234
|
+
│ ├── __init__.py
|
|
235
|
+
│ ├── __main__.py
|
|
236
|
+
│ └── cli.py # CLI implementation and footprint engine
|
|
237
|
+
├── tests/
|
|
238
|
+
│ └── test_token_audit.py # Automated unit test suite (TDD)
|
|
239
|
+
├── .gitignore
|
|
240
|
+
├── AGENTS.md # Agent-friendly repository directives
|
|
241
|
+
├── CONTRIBUTING.md # Contribution guidelines & engineering principles
|
|
242
|
+
├── LICENSE # MIT Open Source License
|
|
243
|
+
├── pyproject.toml # PEP 621 packaging and CLI entrypoint
|
|
244
|
+
└── README.md # Project overview and documentation
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Core Methodology: The 5 Vectors of Token Leakage
|
|
250
|
+
|
|
251
|
+
Token Diet is built on empirical analysis of how developer environments silently deplete API quotas:
|
|
252
|
+
|
|
253
|
+
1. **Global Prompt Bloat:** Monolithic instruction files (`CLAUDE.md`, `AGENTS.md`) exceeding 20 KB inject tens of thousands of static tokens on every turn. *Solution: Keep root directives under 3 KB (<750 tokens) and route to on-demand skills.*
|
|
254
|
+
2. **Invisible Git Hooks:** Pre-commit hooks that intercept local commits to invoke cloud LLM reviews drain quotas without developer consent. *Solution: Keep local commits deterministic; reserve AI reviews for PR gates.*
|
|
255
|
+
3. **Reasoning Effort Inflation:** Running frontier models at `high` reasoning effort for simple syntax or diagnostic queries generates thousands of hidden chain-of-thought tokens. *Solution: Default to `medium` or `low`.*
|
|
256
|
+
4. **Static MCP Schemas:** Registering dozens of MCP tools serializes massive JSON parameter schemas into system prompts. *Solution: Prune unused tool servers and enforce lazy-loading.*
|
|
257
|
+
5. **Prompt Cache Invalidation:** Injecting dynamic timestamps or changing session IDs at the start of instructions destroys provider prompt caching discounts (up to 90%). *Solution: Maintain immutable instruction prefixes.*
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## Platform Compatibility
|
|
262
|
+
|
|
263
|
+
Token Diet is OS-agnostic and cross-platform:
|
|
264
|
+
- **CLI Diagnostic Tool:** Pure Python standard library with zero third-party dependencies. Runs seamlessly on **macOS**, **Linux**, and **Windows** (Python >= 3.8).
|
|
265
|
+
- **Universal Methodology:** Core principles apply across all assistants (Claude Code, Gemini CLI, OpenAI Codex, OpenCode, Cursor, Windsurf).
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Documentation & Guides
|
|
270
|
+
|
|
271
|
+
For in-depth operational procedures and automated pipeline setups:
|
|
272
|
+
- **[Remediation Playbook](docs/remediation-playbook.md):** Step-by-step runbook to prune prompt files, configure reasoning effort, and contain background services.
|
|
273
|
+
- **[CI/CD Integration](docs/ci-integration.md):** GitHub Actions workflows and pre-commit configurations for automated token budget enforcement.
|
|
274
|
+
- **[Agent Directives (AGENTS.md)](AGENTS.md):** Operational guidelines for AI coding assistants working on this codebase.
|
|
275
|
+
- **[Contributing Guidelines](CONTRIBUTING.md):** How to run tests, submit pull requests, and coding standards.
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
|
|
281
|
+
This project is licensed under the terms of the [MIT License](LICENSE).
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "token-diet-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Diagnostic tools and methodology to audit and optimize token footprint in AI coding workflows"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "josephrotts", email = "387283+josephrotts@users.noreply.github.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["tokens", "llm", "claude", "gemini", "codex", "opencode", "developer-tools"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.8",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
token-diet = "token_diet.cli:main"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""
|
|
2
|
+
token-diet: Diagnostic tools and methodology to audit and optimize token footprint in AI coding workflows.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from token_diet.cli import (
|
|
6
|
+
estimate_tokens,
|
|
7
|
+
audit_file,
|
|
8
|
+
detect_local_prompts,
|
|
9
|
+
audit_git_hooks,
|
|
10
|
+
fix_git_hooks,
|
|
11
|
+
classify_size,
|
|
12
|
+
resolve_target_paths,
|
|
13
|
+
audit_known_prompts,
|
|
14
|
+
summarize_footprint,
|
|
15
|
+
main,
|
|
16
|
+
DEFAULT_PROMPT_PATHS,
|
|
17
|
+
LOCAL_PROMPT_CANDIDATES,
|
|
18
|
+
THRESHOLD_WARN_BYTES,
|
|
19
|
+
THRESHOLD_ALERT_BYTES,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
__all__ = [
|
|
24
|
+
"estimate_tokens",
|
|
25
|
+
"audit_file",
|
|
26
|
+
"detect_local_prompts",
|
|
27
|
+
"audit_git_hooks",
|
|
28
|
+
"fix_git_hooks",
|
|
29
|
+
"classify_size",
|
|
30
|
+
"resolve_target_paths",
|
|
31
|
+
"audit_known_prompts",
|
|
32
|
+
"summarize_footprint",
|
|
33
|
+
"main",
|
|
34
|
+
"DEFAULT_PROMPT_PATHS",
|
|
35
|
+
"LOCAL_PROMPT_CANDIDATES",
|
|
36
|
+
"THRESHOLD_WARN_BYTES",
|
|
37
|
+
"THRESHOLD_ALERT_BYTES",
|
|
38
|
+
]
|