mdtoken 1.0.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.
- mdtoken-1.0.0/.pre-commit-hooks.yaml +8 -0
- mdtoken-1.0.0/LICENSE +21 -0
- mdtoken-1.0.0/MANIFEST.in +28 -0
- mdtoken-1.0.0/PKG-INFO +562 -0
- mdtoken-1.0.0/README.md +519 -0
- mdtoken-1.0.0/docs/api.md +1053 -0
- mdtoken-1.0.0/docs/examples.md +1020 -0
- mdtoken-1.0.0/docs/exploration.md +423 -0
- mdtoken-1.0.0/docs/faq.md +710 -0
- mdtoken-1.0.0/docs/requirements.md +321 -0
- mdtoken-1.0.0/docs/troubleshooting.md +744 -0
- mdtoken-1.0.0/pyproject.toml +169 -0
- mdtoken-1.0.0/setup.cfg +4 -0
- mdtoken-1.0.0/setup.py +10 -0
- mdtoken-1.0.0/src/mdtoken/__init__.py +8 -0
- mdtoken-1.0.0/src/mdtoken/__version__.py +3 -0
- mdtoken-1.0.0/src/mdtoken/cli.py +53 -0
- mdtoken-1.0.0/src/mdtoken/config.py +273 -0
- mdtoken-1.0.0/src/mdtoken/counter.py +82 -0
- mdtoken-1.0.0/src/mdtoken/enforcer.py +189 -0
- mdtoken-1.0.0/src/mdtoken/matcher.py +146 -0
- mdtoken-1.0.0/src/mdtoken/reporter.py +189 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/PKG-INFO +562 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/SOURCES.txt +26 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/dependency_links.txt +1 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/entry_points.txt +2 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/requires.txt +13 -0
- mdtoken-1.0.0/src/mdtoken.egg-info/top_level.txt +1 -0
mdtoken-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Stefan Rummer
|
|
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,28 @@
|
|
|
1
|
+
# Include package metadata and documentation
|
|
2
|
+
include LICENSE
|
|
3
|
+
include README.md
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
include .pre-commit-hooks.yaml
|
|
6
|
+
|
|
7
|
+
# Include documentation
|
|
8
|
+
recursive-include docs *.md
|
|
9
|
+
|
|
10
|
+
# Exclude development and build artifacts
|
|
11
|
+
global-exclude __pycache__
|
|
12
|
+
global-exclude *.py[cod]
|
|
13
|
+
global-exclude *$py.class
|
|
14
|
+
global-exclude *.so
|
|
15
|
+
global-exclude .DS_Store
|
|
16
|
+
|
|
17
|
+
# Exclude tests and CI files
|
|
18
|
+
prune tests
|
|
19
|
+
prune .github
|
|
20
|
+
prune .claude
|
|
21
|
+
|
|
22
|
+
# Exclude configuration files
|
|
23
|
+
exclude .gitignore
|
|
24
|
+
exclude .python-version
|
|
25
|
+
exclude Makefile
|
|
26
|
+
exclude coverage.json
|
|
27
|
+
exclude .coverage
|
|
28
|
+
exclude htmlcov
|
mdtoken-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mdtoken
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Pre-commit hook to enforce token count limits on markdown files
|
|
5
|
+
Author-email: Stefan Rummer <stefan@example.com>
|
|
6
|
+
Maintainer-email: Stefan Rummer <stefan@example.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/stefanrmmr/mdtoken
|
|
9
|
+
Project-URL: Repository, https://github.com/stefanrmmr/mdtoken
|
|
10
|
+
Project-URL: Issues, https://github.com/stefanrmmr/mdtoken/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/stefanrmmr/mdtoken#readme
|
|
12
|
+
Keywords: markdown,tokens,pre-commit,ai-tools,context-window,token-limit,git-hooks
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
24
|
+
Classifier: Topic :: Software Development :: Version Control :: Git
|
|
25
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: tiktoken>=0.5.0
|
|
31
|
+
Requires-Dist: PyYAML>=6.0
|
|
32
|
+
Requires-Dist: click>=8.0.0
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
37
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
38
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
40
|
+
Requires-Dist: build>=0.10.0; extra == "dev"
|
|
41
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
# mdtoken - Markdown Token Limit Pre-commit Hook
|
|
45
|
+
|
|
46
|
+
[](https://github.com/applied-artificial-intelligence/mdtoken/actions/workflows/test.yml)
|
|
47
|
+
[](https://codecov.io/gh/applied-artificial-intelligence/mdtoken)
|
|
48
|
+
[](https://opensource.org/licenses/MIT)
|
|
49
|
+
[](https://www.python.org/downloads/)
|
|
50
|
+
|
|
51
|
+
A pre-commit hook that enforces token count limits on markdown files to prevent unbounded growth and context window bloat in AI-assisted development workflows.
|
|
52
|
+
|
|
53
|
+
## Problem
|
|
54
|
+
|
|
55
|
+
Markdown documentation files (like `CLAUDE.md`, memory files, project docs) grow unbounded over time, consuming valuable AI context windows. When your documentation files exceed LLM context limits, they become fragmented, outdated, or worse - silently truncated without your knowledge.
|
|
56
|
+
|
|
57
|
+
Manual monitoring is tedious and error-prone. You need automated guardrails that **fail fast** when documentation becomes too large.
|
|
58
|
+
|
|
59
|
+
## Solution
|
|
60
|
+
|
|
61
|
+
**mdtoken** provides automated token counting checks during your git workflow with clear, actionable feedback when limits are exceeded. Think of it as a linter for your AI context consumption.
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
|
|
65
|
+
- ✅ **Accurate token counting** using tiktoken library
|
|
66
|
+
- ✅ **Configurable tokenizers** - Support for GPT-4, GPT-4o, Claude, Codex, and more
|
|
67
|
+
- ✅ **Flexible configuration** with per-file limits and glob patterns
|
|
68
|
+
- ✅ **Fast execution** (< 1 second for typical projects, 158 tests in < 2s)
|
|
69
|
+
- ✅ **Clear error messages** with actionable suggestions for remediation
|
|
70
|
+
- ✅ **Pre-commit integration** - Blocks commits that violate token limits
|
|
71
|
+
- ✅ **Directory-level limits** - Different limits for commands, skills, agents
|
|
72
|
+
- ✅ **Total token budgets** - Enforce aggregate limits across all files
|
|
73
|
+
- ✅ **Dry-run mode** - Preview violations without failing
|
|
74
|
+
- ✅ **Zero dependencies** - Only requires Python 3.8+ and standard libraries
|
|
75
|
+
|
|
76
|
+
## Installation
|
|
77
|
+
|
|
78
|
+
### From PyPI (Coming Soon)
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install mdtoken
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### From Source (Development)
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
git clone https://github.com/applied-artificial-intelligence/mdtoken.git
|
|
88
|
+
cd mdtoken
|
|
89
|
+
pip install -e .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Quick Start
|
|
93
|
+
|
|
94
|
+
### 1. Create Configuration File
|
|
95
|
+
|
|
96
|
+
Create `.mdtokenrc.yaml` in your project root:
|
|
97
|
+
|
|
98
|
+
```yaml
|
|
99
|
+
# .mdtokenrc.yaml
|
|
100
|
+
default_limit: 4000
|
|
101
|
+
model: "gpt-4" # Use GPT-4 tokenizer
|
|
102
|
+
|
|
103
|
+
limits:
|
|
104
|
+
"CLAUDE.md": 8000
|
|
105
|
+
"README.md": 6000
|
|
106
|
+
".claude/commands/**": 2000
|
|
107
|
+
".claude/skills/**": 3000
|
|
108
|
+
|
|
109
|
+
exclude:
|
|
110
|
+
- "node_modules/**"
|
|
111
|
+
- "**/archive/**"
|
|
112
|
+
- "venv/**"
|
|
113
|
+
|
|
114
|
+
total_limit: 50000
|
|
115
|
+
fail_on_exceed: true
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 2. Run Manually
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
# Check all markdown files
|
|
122
|
+
mdtoken check
|
|
123
|
+
|
|
124
|
+
# Check specific files
|
|
125
|
+
mdtoken check README.md CLAUDE.md
|
|
126
|
+
|
|
127
|
+
# Dry run (don't fail on violations)
|
|
128
|
+
mdtoken check --dry-run
|
|
129
|
+
|
|
130
|
+
# Verbose output with suggestions
|
|
131
|
+
mdtoken check --verbose
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 3. Integrate with Pre-commit
|
|
135
|
+
|
|
136
|
+
Add to `.pre-commit-config.yaml`:
|
|
137
|
+
|
|
138
|
+
```yaml
|
|
139
|
+
repos:
|
|
140
|
+
- repo: https://github.com/applied-artificial-intelligence/mdtoken
|
|
141
|
+
rev: v1.0.0 # Use latest release tag
|
|
142
|
+
hooks:
|
|
143
|
+
- id: markdown-token-limit
|
|
144
|
+
args: ['--config=.mdtokenrc.yaml']
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Then install the hook:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
pre-commit install
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Now every commit will check markdown files against your token limits!
|
|
154
|
+
|
|
155
|
+
## Configuration Guide
|
|
156
|
+
|
|
157
|
+
### Basic Options
|
|
158
|
+
|
|
159
|
+
```yaml
|
|
160
|
+
# Default token limit for all markdown files
|
|
161
|
+
default_limit: 4000
|
|
162
|
+
|
|
163
|
+
# Whether to fail (exit 1) when limits are exceeded
|
|
164
|
+
# Set to false for warning-only mode
|
|
165
|
+
fail_on_exceed: true
|
|
166
|
+
|
|
167
|
+
# Optional: Total token limit across all files
|
|
168
|
+
total_limit: 50000
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Tokenizer Configuration
|
|
172
|
+
|
|
173
|
+
Choose your tokenizer based on the LLM you're using:
|
|
174
|
+
|
|
175
|
+
```yaml
|
|
176
|
+
# Option 1: User-friendly model name (recommended)
|
|
177
|
+
model: "gpt-4"
|
|
178
|
+
|
|
179
|
+
# Option 2: Direct tiktoken encoding name
|
|
180
|
+
encoding: "cl100k_base"
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Supported Models:**
|
|
184
|
+
- `gpt-4` → cl100k_base (100K token context, default)
|
|
185
|
+
- `gpt-4o` → o200k_base (200K token context)
|
|
186
|
+
- `gpt-3.5-turbo` → cl100k_base
|
|
187
|
+
- `claude-3` → cl100k_base (Claude uses similar tokenization)
|
|
188
|
+
- `claude-3.5` → cl100k_base
|
|
189
|
+
- `codex` → p50k_base
|
|
190
|
+
- `text-davinci-003` → p50k_base
|
|
191
|
+
- `text-davinci-002` → p50k_base
|
|
192
|
+
- `gpt-3` → r50k_base
|
|
193
|
+
|
|
194
|
+
**Note:** If both `model` and `encoding` are specified, `encoding` takes precedence.
|
|
195
|
+
|
|
196
|
+
### Per-File and Pattern-Based Limits
|
|
197
|
+
|
|
198
|
+
```yaml
|
|
199
|
+
limits:
|
|
200
|
+
# Exact file match
|
|
201
|
+
"CLAUDE.md": 8000
|
|
202
|
+
|
|
203
|
+
# Pattern matching (substring)
|
|
204
|
+
"README.md": 6000 # Matches any path ending with README.md
|
|
205
|
+
|
|
206
|
+
# Directory patterns
|
|
207
|
+
".claude/commands/**": 2000
|
|
208
|
+
".claude/skills/**": 3000
|
|
209
|
+
".claude/agents/**": 4000
|
|
210
|
+
"docs/*.md": 5000
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**Pattern Matching Rules:**
|
|
214
|
+
- Exact path match has highest priority
|
|
215
|
+
- Substring match (e.g., "README.md" matches "docs/README.md")
|
|
216
|
+
- Falls back to `default_limit` if no pattern matches
|
|
217
|
+
|
|
218
|
+
### Exclusion Patterns
|
|
219
|
+
|
|
220
|
+
```yaml
|
|
221
|
+
exclude:
|
|
222
|
+
# Default exclusions (automatically included)
|
|
223
|
+
- ".git/**"
|
|
224
|
+
- "node_modules/**"
|
|
225
|
+
- "venv/**"
|
|
226
|
+
- ".venv/**"
|
|
227
|
+
- "build/**"
|
|
228
|
+
- "dist/**"
|
|
229
|
+
- "__pycache__/**"
|
|
230
|
+
|
|
231
|
+
# Custom exclusions
|
|
232
|
+
- "**/archive/**"
|
|
233
|
+
- "**/old_docs/**"
|
|
234
|
+
- "README.md" # Exclude specific files entirely
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Usage Examples
|
|
238
|
+
|
|
239
|
+
### Example 1: Claude Code Project
|
|
240
|
+
|
|
241
|
+
For a Claude Code project with commands, skills, and agents:
|
|
242
|
+
|
|
243
|
+
```yaml
|
|
244
|
+
# .mdtokenrc.yaml
|
|
245
|
+
model: "claude-3.5"
|
|
246
|
+
default_limit: 4000
|
|
247
|
+
|
|
248
|
+
limits:
|
|
249
|
+
".claude/CLAUDE.md": 10000 # Main instruction file
|
|
250
|
+
".claude/commands/**": 2000 # Keep commands concise
|
|
251
|
+
".claude/skills/**": 3000
|
|
252
|
+
".claude/agents/**": 5000
|
|
253
|
+
"README.md": 8000
|
|
254
|
+
|
|
255
|
+
exclude:
|
|
256
|
+
- ".claude/memory/archived/**"
|
|
257
|
+
|
|
258
|
+
total_limit: 100000 # Aggregate limit
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Example 2: Documentation-Heavy Project
|
|
262
|
+
|
|
263
|
+
```yaml
|
|
264
|
+
# .mdtokenrc.yaml
|
|
265
|
+
model: "gpt-4"
|
|
266
|
+
default_limit: 5000
|
|
267
|
+
|
|
268
|
+
limits:
|
|
269
|
+
"README.md": 8000
|
|
270
|
+
"docs/api.md": 10000
|
|
271
|
+
"docs/tutorials/**": 6000
|
|
272
|
+
"docs/reference/**": 8000
|
|
273
|
+
|
|
274
|
+
exclude:
|
|
275
|
+
- "docs/archive/**"
|
|
276
|
+
- "docs/drafts/**"
|
|
277
|
+
|
|
278
|
+
fail_on_exceed: true
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Example 3: Multi-Model Support
|
|
282
|
+
|
|
283
|
+
If you're optimizing for different models:
|
|
284
|
+
|
|
285
|
+
```yaml
|
|
286
|
+
# For GPT-4o with larger context window
|
|
287
|
+
model: "gpt-4o"
|
|
288
|
+
default_limit: 8000 # Can use larger limits
|
|
289
|
+
|
|
290
|
+
limits:
|
|
291
|
+
"CLAUDE.md": 15000
|
|
292
|
+
"docs/**": 10000
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Output Examples
|
|
296
|
+
|
|
297
|
+
### Passing Check
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
✓ All files within token limits
|
|
301
|
+
3 files checked, 8,245 tokens total
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Failing Check
|
|
305
|
+
|
|
306
|
+
```
|
|
307
|
+
✗ Token limit violations found:
|
|
308
|
+
|
|
309
|
+
docs/README.md: 5,234 tokens (limit: 4,000, over by 1,234)
|
|
310
|
+
Suggestions:
|
|
311
|
+
- Consider splitting this file into multiple smaller files
|
|
312
|
+
- Target: reduce by ~1,234 tokens to get under the limit
|
|
313
|
+
- Move detailed documentation to separate docs/ files
|
|
314
|
+
|
|
315
|
+
.claude/CLAUDE.md: 9,876 tokens (limit: 8,000, over by 1,876)
|
|
316
|
+
Suggestions:
|
|
317
|
+
- Review content and remove unnecessary sections
|
|
318
|
+
- Consider moving older content to an archived directory
|
|
319
|
+
|
|
320
|
+
2/3 files over limit, 15,110 tokens total
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## Programmatic Usage
|
|
324
|
+
|
|
325
|
+
For programmatic usage in Python scripts, see the [API Documentation](docs/api.md) for detailed information on:
|
|
326
|
+
|
|
327
|
+
- **TokenCounter** - Count tokens in text and files
|
|
328
|
+
- **Config** - Load and manage configuration
|
|
329
|
+
- **LimitEnforcer** - Check files against token limits
|
|
330
|
+
- **FileMatcher** - Discover and filter markdown files
|
|
331
|
+
- **Reporter** - Format and display results
|
|
332
|
+
|
|
333
|
+
Quick example:
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
from mdtoken.config import Config
|
|
337
|
+
from mdtoken.enforcer import LimitEnforcer
|
|
338
|
+
from mdtoken.reporter import Reporter
|
|
339
|
+
|
|
340
|
+
config = Config.from_file()
|
|
341
|
+
enforcer = LimitEnforcer(config)
|
|
342
|
+
result = enforcer.check_files()
|
|
343
|
+
|
|
344
|
+
reporter = Reporter(enforcer)
|
|
345
|
+
reporter.report(result, verbose=True)
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
See [docs/api.md](docs/api.md) for complete API reference with examples.
|
|
349
|
+
|
|
350
|
+
## Documentation
|
|
351
|
+
|
|
352
|
+
- **[API Reference](docs/api.md)** - Complete API documentation for programmatic usage
|
|
353
|
+
- **[Usage Examples](docs/examples.md)** - Project-specific configurations and workflows
|
|
354
|
+
- **[Troubleshooting](docs/troubleshooting.md)** - Common issues and solutions
|
|
355
|
+
- **[FAQ](docs/faq.md)** - Frequently asked questions
|
|
356
|
+
|
|
357
|
+
## Troubleshooting
|
|
358
|
+
|
|
359
|
+
### "Config file not found" Warning
|
|
360
|
+
|
|
361
|
+
**Problem:** mdtoken looks for `.mdtokenrc.yaml` in the current directory.
|
|
362
|
+
|
|
363
|
+
**Solution:** Either:
|
|
364
|
+
1. Create `.mdtokenrc.yaml` in your project root
|
|
365
|
+
2. Specify config path: `mdtoken check --config path/to/config.yaml`
|
|
366
|
+
3. Use defaults (no config file needed)
|
|
367
|
+
|
|
368
|
+
### "Unknown model" Error
|
|
369
|
+
|
|
370
|
+
**Problem:** Model name not recognized in MODEL_ENCODING_MAP.
|
|
371
|
+
|
|
372
|
+
**Solution:** Either:
|
|
373
|
+
- Use a supported model name (see "Supported Models" above)
|
|
374
|
+
- Use direct encoding: `encoding: "cl100k_base"`
|
|
375
|
+
|
|
376
|
+
### Pre-commit Hook Not Running
|
|
377
|
+
|
|
378
|
+
**Problem:** Hook doesn't execute on commit.
|
|
379
|
+
|
|
380
|
+
**Solution:**
|
|
381
|
+
1. Verify `.pre-commit-config.yaml` exists
|
|
382
|
+
2. Run `pre-commit install`
|
|
383
|
+
3. Test with `pre-commit run --all-files`
|
|
384
|
+
|
|
385
|
+
### Performance Issues
|
|
386
|
+
|
|
387
|
+
**Problem:** Slow execution on large repositories.
|
|
388
|
+
|
|
389
|
+
**Current Status:** mdtoken is highly optimized:
|
|
390
|
+
- 158 tests run in < 2 seconds
|
|
391
|
+
- Token counting: 6.15ms for 8K tokens (16x faster than requirement)
|
|
392
|
+
|
|
393
|
+
If you're experiencing slowness:
|
|
394
|
+
1. Use exclusion patterns to skip unnecessary directories
|
|
395
|
+
2. Limit to specific file patterns
|
|
396
|
+
3. Consider running only on changed files (pre-commit does this automatically)
|
|
397
|
+
|
|
398
|
+
## Development
|
|
399
|
+
|
|
400
|
+
### Setup Development Environment
|
|
401
|
+
|
|
402
|
+
```bash
|
|
403
|
+
# Clone repository
|
|
404
|
+
git clone https://github.com/applied-artificial-intelligence/mdtoken.git
|
|
405
|
+
cd mdtoken
|
|
406
|
+
|
|
407
|
+
# Create virtual environment
|
|
408
|
+
python -m venv venv
|
|
409
|
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
410
|
+
|
|
411
|
+
# Install development dependencies
|
|
412
|
+
pip install -e ".[dev]"
|
|
413
|
+
|
|
414
|
+
# Run tests
|
|
415
|
+
pytest
|
|
416
|
+
|
|
417
|
+
# Run linting
|
|
418
|
+
make lint
|
|
419
|
+
|
|
420
|
+
# Run type checking
|
|
421
|
+
make typecheck
|
|
422
|
+
|
|
423
|
+
# Format code
|
|
424
|
+
make format
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### Running Tests
|
|
428
|
+
|
|
429
|
+
```bash
|
|
430
|
+
# Run all tests
|
|
431
|
+
pytest
|
|
432
|
+
|
|
433
|
+
# Run with coverage
|
|
434
|
+
pytest --cov=src/mdtoken --cov-report=term
|
|
435
|
+
|
|
436
|
+
# Run specific test file
|
|
437
|
+
pytest tests/test_config.py
|
|
438
|
+
|
|
439
|
+
# Run verbose
|
|
440
|
+
pytest -xvs
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
### Project Structure
|
|
444
|
+
|
|
445
|
+
```
|
|
446
|
+
mdtoken/
|
|
447
|
+
├── src/mdtoken/
|
|
448
|
+
│ ├── __init__.py
|
|
449
|
+
│ ├── __version__.py
|
|
450
|
+
│ ├── cli.py # Command-line interface
|
|
451
|
+
│ ├── config.py # Configuration loading/validation
|
|
452
|
+
│ ├── counter.py # Token counting with tiktoken
|
|
453
|
+
│ ├── enforcer.py # Limit enforcement logic
|
|
454
|
+
│ ├── matcher.py # File pattern matching
|
|
455
|
+
│ ├── reporter.py # Output formatting
|
|
456
|
+
│ └── commands/
|
|
457
|
+
│ └── check.py # Check command implementation
|
|
458
|
+
├── tests/
|
|
459
|
+
│ ├── test_config.py # Config tests (64 tests)
|
|
460
|
+
│ ├── test_counter.py # Token counting tests
|
|
461
|
+
│ ├── test_enforcer.py # Enforcement logic tests (72 tests)
|
|
462
|
+
│ ├── test_matcher.py # File matching tests
|
|
463
|
+
│ ├── test_edge_cases.py # Edge case handling (27 tests)
|
|
464
|
+
│ └── integration/
|
|
465
|
+
│ └── test_git_workflow.py # End-to-end tests (12 tests)
|
|
466
|
+
├── .pre-commit-hooks.yaml # Pre-commit hook definition
|
|
467
|
+
├── pyproject.toml # Package configuration
|
|
468
|
+
└── README.md
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
### Test Coverage
|
|
472
|
+
|
|
473
|
+
**Current Coverage: 84%** (176 tests passing)
|
|
474
|
+
|
|
475
|
+
| Module | Coverage | Notes |
|
|
476
|
+
|--------|----------|-------|
|
|
477
|
+
| config.py | 97% | Model/encoding resolution fully tested |
|
|
478
|
+
| counter.py | 90% | Token counting core |
|
|
479
|
+
| enforcer.py | 96% | Limit enforcement logic |
|
|
480
|
+
| matcher.py | 97% | File pattern matching |
|
|
481
|
+
| reporter.py | 90% | Output formatting |
|
|
482
|
+
| cli.py | 0% | Integration tests cover CLI |
|
|
483
|
+
| commands/check.py | 0% | Integration tests cover commands |
|
|
484
|
+
|
|
485
|
+
## Roadmap
|
|
486
|
+
|
|
487
|
+
### v1.0.0 (Current - Ready for Release)
|
|
488
|
+
- ✅ Core token counting functionality
|
|
489
|
+
- ✅ Configurable tokenizers (encoding/model)
|
|
490
|
+
- ✅ YAML configuration support
|
|
491
|
+
- ✅ Pre-commit hook integration
|
|
492
|
+
- ✅ Per-file and pattern-based limits
|
|
493
|
+
- ✅ Total token budgets
|
|
494
|
+
- ✅ Clear error messages with suggestions
|
|
495
|
+
- ✅ Comprehensive test suite (176 tests, 84% coverage)
|
|
496
|
+
- ✅ CI/CD with GitHub Actions
|
|
497
|
+
- 🚧 PyPI distribution (pending)
|
|
498
|
+
- 🚧 Comprehensive documentation (in progress)
|
|
499
|
+
|
|
500
|
+
### v1.1+ (Future Enhancements)
|
|
501
|
+
- Auto-fix/splitting suggestions with AI
|
|
502
|
+
- Token count caching for performance
|
|
503
|
+
- Parallel processing for large repos
|
|
504
|
+
- GitHub Action for PR checks
|
|
505
|
+
- IDE integrations (VS Code extension)
|
|
506
|
+
- Watch mode for live feedback
|
|
507
|
+
- HTML/JSON output formats
|
|
508
|
+
- Integration with documentation generators
|
|
509
|
+
|
|
510
|
+
## FAQ
|
|
511
|
+
|
|
512
|
+
**Q: Why another markdown linter?**
|
|
513
|
+
A: mdtoken is specifically designed for AI-assisted workflows where token counts matter. Traditional linters check syntax/style; mdtoken checks token consumption.
|
|
514
|
+
|
|
515
|
+
**Q: Does it work with Claude, GPT-4o, or other models?**
|
|
516
|
+
A: Yes! Use the `model` config parameter to select the appropriate tokenizer. While exact tokenization may vary slightly between models, tiktoken provides excellent approximations.
|
|
517
|
+
|
|
518
|
+
**Q: Can I use this without pre-commit?**
|
|
519
|
+
A: Absolutely! Run `mdtoken check` manually as part of your CI/CD pipeline, as a git hook, or integrate it into your build process.
|
|
520
|
+
|
|
521
|
+
**Q: What if I want different limits for different branches?**
|
|
522
|
+
A: Create multiple config files (e.g., `.mdtokenrc.production.yaml`, `.mdtokenrc.dev.yaml`) and use `--config` flag to specify which one to use.
|
|
523
|
+
|
|
524
|
+
**Q: How accurate is the token counting?**
|
|
525
|
+
A: mdtoken uses tiktoken, the same library used by OpenAI's models. Accuracy is within 1-2% of actual model tokenization.
|
|
526
|
+
|
|
527
|
+
**Q: Does it support languages other than markdown?**
|
|
528
|
+
A: Currently markdown-only. Future versions may support additional file types.
|
|
529
|
+
|
|
530
|
+
## Contributing
|
|
531
|
+
|
|
532
|
+
Contributions are welcome! This project follows standard open-source practices:
|
|
533
|
+
|
|
534
|
+
1. Fork the repository
|
|
535
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
536
|
+
3. Make your changes with tests
|
|
537
|
+
4. Run the test suite (`pytest`)
|
|
538
|
+
5. Ensure code quality (`make lint`)
|
|
539
|
+
6. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
540
|
+
7. Push to the branch (`git push origin feature/amazing-feature`)
|
|
541
|
+
8. Open a Pull Request
|
|
542
|
+
|
|
543
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) (coming soon) for detailed guidelines.
|
|
544
|
+
|
|
545
|
+
## License
|
|
546
|
+
|
|
547
|
+
MIT License - See [LICENSE](LICENSE) file for details.
|
|
548
|
+
|
|
549
|
+
## Author
|
|
550
|
+
|
|
551
|
+
Stefan Rummer ([@stefanrmmr](https://github.com/stefanrmmr))
|
|
552
|
+
|
|
553
|
+
## Acknowledgments
|
|
554
|
+
|
|
555
|
+
- Built with [Claude Code](https://claude.com/claude-code) as part of an AI-assisted development workflow
|
|
556
|
+
- Uses OpenAI's [tiktoken](https://github.com/openai/tiktoken) library for accurate token counting
|
|
557
|
+
- Inspired by real-world challenges managing context windows in LLM-assisted development
|
|
558
|
+
- Thanks to the pre-commit framework team for excellent hook infrastructure
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
**Status**: Ready for v1.0.0 release. Star ⭐ and watch 👀 for updates!
|