python-token-killer 0.1.0__py3-none-any.whl

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,269 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-token-killer
3
+ Version: 0.1.0
4
+ Summary: Minimize LLM tokens from Python objects — dicts, code, logs, diffs, and more.
5
+ Project-URL: Homepage, https://github.com/amahi2001/python-token-killer
6
+ Project-URL: Repository, https://github.com/amahi2001/python-token-killer
7
+ Project-URL: Issues, https://github.com/amahi2001/python-token-killer/issues
8
+ Project-URL: Changelog, https://github.com/amahi2001/python-token-killer/blob/main/CHANGELOG.md
9
+ Author-email: amahi2001 <amahi2001@gmail.com>
10
+ License: MIT License
11
+
12
+ Copyright (c) 2026 ptk contributors
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ License-File: LICENSE
32
+ Keywords: agents,claude,compression,context-window,langchain,langgraph,llm,nlp,openai,rag,tokens
33
+ Classifier: Development Status :: 3 - Alpha
34
+ Classifier: Intended Audience :: Developers
35
+ Classifier: License :: OSI Approved :: MIT License
36
+ Classifier: Operating System :: OS Independent
37
+ Classifier: Programming Language :: Python :: 3
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Classifier: Programming Language :: Python :: 3.13
42
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
43
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
44
+ Classifier: Topic :: Text Processing
45
+ Classifier: Typing :: Typed
46
+ Requires-Python: >=3.10
47
+ Provides-Extra: tiktoken
48
+ Requires-Dist: tiktoken>=0.7; extra == 'tiktoken'
49
+ Description-Content-Type: text/markdown
50
+
51
+ <p align="center">
52
+ <img src="assets/mascot.png" alt="ptk" width="200"/>
53
+ </p>
54
+
55
+ <p align="center">
56
+ <strong>ptk — Python Token Killer</strong><br/>
57
+ <strong>Minimize LLM tokens from Python objects in one call</strong><br/>
58
+ Zero dependencies • Auto type detection • 322 tests
59
+ </p>
60
+
61
+ <table align="center">
62
+ <tr>
63
+ <td align="left" valign="middle">
64
+ <a href="https://github.com/amahi2001/python-token-killer/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/amahi2001/python-token-killer/ci.yml?branch=main&style=flat-square&label=CI" alt="CI"/></a><br/>
65
+ <img src="https://img.shields.io/badge/python-3.10+-3776AB?style=flat-square&logo=python&logoColor=white" alt="Python 3.10+"/><br/>
66
+ <img src="https://img.shields.io/badge/mypy-strict-blue?style=flat-square" alt="mypy strict"/><br/>
67
+ <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-yellow?style=flat-square" alt="License"/></a>
68
+ </td>
69
+ </tr>
70
+ </table>
71
+
72
+ ---
73
+
74
+ ## What is ptk?
75
+
76
+ ptk is a **Python library** that minimizes tokens before they reach an LLM. Pass in any Python object — dict, list, code, logs, diffs, text — and get back a compressed string representation.
77
+
78
+ Inspired by [RTK (Rust Token Killer)](https://github.com/rtk-ai/rtk), but designed as a library for programmatic use, not a CLI proxy.
79
+
80
+ ```python
81
+ import ptk
82
+
83
+ ptk.minimize({"users": [{"name": "Alice", "bio": None, "age": 30}]})
84
+ # → '{"users":[{"name":"Alice","age":30}]}'
85
+
86
+ ptk(my_dict) # callable shorthand
87
+ ptk(my_dict, aggressive=True) # max compression
88
+ ```
89
+
90
+ ```bash
91
+ pip install python-token-killer
92
+ # or
93
+ uv add python-token-killer
94
+ ```
95
+
96
+ Optional: `pip install python-token-killer[tiktoken]` or `uv add python-token-killer[tiktoken]` for exact token counting.
97
+
98
+ ## Benchmarks
99
+
100
+ Real token counts via tiktoken (`cl100k_base`, same tokenizer as GPT-4 / Claude):
101
+
102
+ ```
103
+ Benchmark Original Default Saved Aggressive Saved
104
+ API response (JSON) 1450 792 45.4% 782 46.1%
105
+ Python module (code) 2734 2113 22.7% 309 88.7%
106
+ Server log (58 lines) 1389 1388 0.1% 231 83.4%
107
+ 50 user records (list) 2774 922 66.8% 922 66.8%
108
+ Verbose paragraph (text) 101 96 5.0% 74 26.7%
109
+ ─────────────────────────────────────────────
110
+ TOTAL 11182 7424 33.6% 2627 76.5%
111
+ ```
112
+
113
+ Run yourself: `python benchmarks/bench.py`
114
+
115
+ ## What It Does
116
+
117
+ ptk auto-detects your input type and routes to the right minimizer:
118
+
119
+ | Input Type | Strategy | Typical Savings |
120
+ |---|---|---|
121
+ | `dict` | Null stripping, key shortening, flattening, compact JSON | 30–60% |
122
+ | `list` | Dedup, schema-once tabular, sampling | 40–70% |
123
+ | Code `str` | Comment stripping (pragma-preserving), docstring collapse, signature extraction | 25–80% |
124
+ | Logs `str` | Line dedup with counts, error-only filtering, stack trace preservation | 60–90% |
125
+ | Diffs `str` | Context folding, noise stripping | 50–75% |
126
+ | Text `str` | Word/phrase abbreviation, filler removal, stopword removal | 10–30% |
127
+
128
+ ## API
129
+
130
+ ### `ptk.minimize(obj, *, aggressive=False, content_type=None, **kw) → str`
131
+
132
+ Main entry point. Auto-detects type, applies the right strategy, returns a minimized string.
133
+
134
+ ```python
135
+ # auto-detect
136
+ ptk.minimize({"key": "value"})
137
+
138
+ # force content type
139
+ ptk.minimize(some_string, content_type="code")
140
+ ptk.minimize(some_string, content_type="log")
141
+
142
+ # dict output formats
143
+ ptk.minimize(data, format="kv") # key:value lines
144
+ ptk.minimize(data, format="tabular") # header-once tabular
145
+
146
+ # code: signatures only (huge savings)
147
+ ptk.minimize(code, content_type="code", mode="signatures")
148
+
149
+ # logs: errors only
150
+ ptk.minimize(logs, content_type="log", errors_only=True)
151
+ ```
152
+
153
+ ### `ptk.stats(obj, **kw) → dict`
154
+
155
+ Same compression, but returns statistics:
156
+
157
+ ```python
158
+ ptk.stats(big_api_response)
159
+ # {
160
+ # "output": "...",
161
+ # "original_len": 4200,
162
+ # "minimized_len": 1800,
163
+ # "savings_pct": 57.1,
164
+ # "content_type": "dict",
165
+ # "original_tokens": 1050,
166
+ # "minimized_tokens": 450,
167
+ # }
168
+ ```
169
+
170
+ ### `ptk(obj)` — callable module
171
+
172
+ ```python
173
+ import ptk
174
+ ptk(some_dict) # equivalent to ptk.minimize(some_dict)
175
+ ```
176
+
177
+ ## Features by Minimizer
178
+
179
+ ### DictMinimizer
180
+ - Strips `None`, `""`, `[]`, `{}` recursively (preserves `0` and `False`)
181
+ - Key shortening: `description` → `desc`, `timestamp` → `ts`, `configuration` → `cfg`, etc.
182
+ - Single-child flattening: `{"a": {"b": val}}` → `{"a.b": val}` (aggressive)
183
+ - Output formats: compact JSON (default), key-value lines, header-once tabular
184
+
185
+ ### ListMinimizer
186
+ - Uniform list-of-dicts → schema-once tabular: declare fields once, one row per item
187
+ - Primitive dedup with counts: `["a", "a", "a", "b"]` → `a (x3)\nb`
188
+ - Large array sampling with first/last preservation (aggressive, threshold: 50)
189
+
190
+ ### CodeMinimizer
191
+ - Strips comments while **preserving pragmas**: `# noqa`, `# type: ignore`, `# TODO`, `# FIXME`, `// eslint-disable`
192
+ - Collapses multi-line docstrings to first line only
193
+ - Signature extraction mode: pulls `def`, `class`, `fn`, `func` across Python, JS, Rust, Go
194
+ - Normalizes blank lines and trailing whitespace
195
+
196
+ ### LogMinimizer
197
+ - Consecutive duplicate line collapse with `(xN)` counts
198
+ - Error-only filtering preserving: ERROR, WARN, FATAL, CRITICAL, stack traces, "failed" keyword
199
+ - Timestamp stripping (aggressive)
200
+
201
+ ### DiffMinimizer
202
+ - Folds unchanged context lines to `... N lines ...`
203
+ - Strips noise: `index`, `old mode`, `new mode`, `similarity`, `Binary files` (aggressive)
204
+ - Preserves: `+`/`-` lines, `@@` hunks, `---`/`+++` headers, ``
205
+
206
+ ### TextMinimizer
207
+ - Word abbreviation: `implementation` → `impl`, `configuration` → `config`, `production` → `prod`, etc.
208
+ - Phrase abbreviation: `in order to` → `to`, `due to the fact that` → `because`, etc.
209
+ - Filler removal: strips `Furthermore,`, `Moreover,`, `In addition,`, `Additionally,`
210
+ - Stopword removal (aggressive): strips `the`, `a`, `is`, `very`, etc.
211
+
212
+ ## Use Cases
213
+
214
+ ### Agent Frameworks (LangGraph / LangChain)
215
+
216
+ ```python
217
+ import ptk
218
+
219
+ def compress_context(state):
220
+ state["context"] = ptk.minimize(state["context"], aggressive=True)
221
+ return state
222
+ ```
223
+
224
+ ### Claude Code Skills
225
+
226
+ ```python
227
+ #!/usr/bin/env python3
228
+ import ptk, json, sys
229
+ data = json.load(open(sys.argv[1]))
230
+ print(ptk(data))
231
+ ```
232
+
233
+ ### API Response Cleanup
234
+
235
+ ```python
236
+ response = requests.get("https://api.example.com/users").json()
237
+ clean = ptk.minimize(response) # strip nulls, compact JSON
238
+ ```
239
+
240
+ ## Comparison with Alternatives
241
+
242
+ | Tool | Approach | Best For |
243
+ |---|---|---|
244
+ | **ptk** | Type-detecting Python library, one-liner API | Programmatic use in scripts, agents, frameworks |
245
+ | [RTK](https://github.com/rtk-ai/rtk) | Rust CLI proxy for shell commands | Coding agents (Claude Code, OpenCode) |
246
+ | [claw-compactor](https://github.com/open-compress/claw-compactor) | 14-stage pipeline, AST-aware | Heavy-duty workspace compression |
247
+ | [toons](https://pypi.org/project/toons/) | TOON serialization format | Tabular data in LLM prompts |
248
+ | [LLMLingua](https://github.com/microsoft/LLMLingua) | Neural prompt compression | Natural language, requires GPU |
249
+
250
+ ## Design Principles
251
+
252
+ - **Zero deps** — stdlib only. tiktoken is optional for exact counts.
253
+ - **Builtins-first** — `frozenset` for O(1) lookups, precompiled regexes, `slots=True` frozen dataclasses.
254
+ - **DRY** — shared `strip_nullish()`, `dedup_lines()` reused across minimizers.
255
+ - **Type-routed** — O(1) detection for dicts/lists, first-2KB heuristic for strings.
256
+ - **Safe by default** — aggressive mode is opt-in. Default never destroys meaning.
257
+
258
+ ## Development
259
+
260
+ ```bash
261
+ git clone https://github.com/amahi2001/python-token-killer.git
262
+ cd python-token-killer
263
+ uv sync # installs all dev dependencies, creates .venv automatically
264
+ make check # lint + typecheck + 361 tests
265
+ ```
266
+
267
+ ## License
268
+
269
+ MIT
@@ -0,0 +1,15 @@
1
+ ptk/__init__.py,sha256=2cvBMJIUBNnQwGHJrYiHCpYfVOZ1XzO_Fbeh5gkx5MM,5007
2
+ ptk/_base.py,sha256=eDiRgZK-xNJ1F7we1sNok1javHo2HLpo2j2lHmRZzaI,4019
3
+ ptk/_types.py,sha256=8aXkPH5xpH6KLOr7IkIOx5lEkY81CSFVLhc4N5skXoE,3329
4
+ ptk/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ ptk/minimizers/__init__.py,sha256=SYFBJBCY9K5-nzgyL8vX7LcigC5QHxRxvQ5LPt3YCGU,479
6
+ ptk/minimizers/_code.py,sha256=Zq06CCHOmtEXb8vz0TchgDTEyLdPW0098__aM_6NivE,5256
7
+ ptk/minimizers/_dict.py,sha256=R5j9U-Vq8VDW4-elxfKrbnDzsyJ8xHdCmj_qBEQqSBI,5300
8
+ ptk/minimizers/_diff.py,sha256=TE6skH2PyjY_XB2F-nyvmCbTpifMkBvH2Y2ZTtpTQw4,2644
9
+ ptk/minimizers/_list.py,sha256=laLYCFZmrIcM_0OUVIQPsms_nGkNC6HovaTxe1LqPiA,3122
10
+ ptk/minimizers/_log.py,sha256=xdy7wMtR8_LBL8iAl84w9mGJyqMR52IWJu4pcKk-zzA,3000
11
+ ptk/minimizers/_text.py,sha256=YBephRTVVtMBp-84w2xYjEaCdO2nDV-gfDQcV7VWSXc,4931
12
+ python_token_killer-0.1.0.dist-info/METADATA,sha256=dSRZXTYymDbImcCxuf1N06g7srkh3DHnN0gF2moL1tE,10754
13
+ python_token_killer-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
14
+ python_token_killer-0.1.0.dist-info/licenses/LICENSE,sha256=lGzU7YBg4RNcgqCA6aob3n4o-_T2ppJRCo5c8Xa3ZyI,1073
15
+ python_token_killer-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.29.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ptk 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.