tokenfold 0.3.4__tar.gz → 0.4.1__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 (42) hide show
  1. {tokenfold-0.3.4 → tokenfold-0.4.1}/Cargo.lock +10 -10
  2. {tokenfold-0.3.4 → tokenfold-0.4.1}/Cargo.toml +1 -1
  3. tokenfold-0.4.1/PKG-INFO +369 -0
  4. tokenfold-0.4.1/README.md +346 -0
  5. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/benches/THRESHOLDS.toml +1 -1
  6. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/benches/compression_bench.rs +2 -2
  7. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/budget.rs +247 -17
  8. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/errors.rs +6 -1
  9. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/filters.rs +8 -8
  10. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/lib.rs +1 -1
  11. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/modes.rs +17 -15
  12. tokenfold-0.4.1/crates/tokenfold-core/src/pipeline/mod.rs +1010 -0
  13. tokenfold-0.4.1/crates/tokenfold-core/src/pipeline/tests.rs +1287 -0
  14. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/report.rs +39 -8
  15. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/retrieval_store.rs +3 -3
  16. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/safety.rs +3 -1
  17. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/stats.rs +16 -15
  18. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/diff.rs +4 -6
  19. tokenfold-0.4.1/crates/tokenfold-core/src/transforms/json_prune.rs +1237 -0
  20. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/logs.rs +4 -4
  21. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/mod.rs +1 -0
  22. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/tests/golden.rs +3 -1
  23. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/tests/integration.rs +1 -1
  24. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/tests/property_tests.rs +1 -1
  25. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-py/Cargo.toml +1 -1
  26. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-py/src/lib.rs +264 -32
  27. tokenfold-0.3.4/PKG-INFO +0 -379
  28. tokenfold-0.3.4/README.md +0 -356
  29. tokenfold-0.3.4/crates/tokenfold-core/examples/quickstart.rs +0 -83
  30. tokenfold-0.3.4/crates/tokenfold-core/src/pipeline.rs +0 -839
  31. {tokenfold-0.3.4 → tokenfold-0.4.1}/LICENSE +0 -0
  32. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/Cargo.toml +0 -0
  33. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/input.rs +0 -0
  34. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/status.rs +0 -0
  35. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/token_estimator.rs +0 -0
  36. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/json.rs +0 -0
  37. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/json_dict.rs +0 -0
  38. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/json_fold.rs +0 -0
  39. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/log_fold.rs +0 -0
  40. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/redaction.rs +0 -0
  41. {tokenfold-0.3.4 → tokenfold-0.4.1}/crates/tokenfold-core/src/transforms/schema.rs +0 -0
  42. {tokenfold-0.3.4 → tokenfold-0.4.1}/pyproject.toml +0 -0
@@ -1013,7 +1013,7 @@ dependencies = [
1013
1013
 
1014
1014
  [[package]]
1015
1015
  name = "tokenfold-adapters"
1016
- version = "0.3.4"
1016
+ version = "0.4.1"
1017
1017
  dependencies = [
1018
1018
  "serde",
1019
1019
  "serde_json",
@@ -1022,7 +1022,7 @@ dependencies = [
1022
1022
 
1023
1023
  [[package]]
1024
1024
  name = "tokenfold-admin"
1025
- version = "0.3.4"
1025
+ version = "0.4.1"
1026
1026
  dependencies = [
1027
1027
  "ed25519-dalek",
1028
1028
  "serde",
@@ -1032,7 +1032,7 @@ dependencies = [
1032
1032
 
1033
1033
  [[package]]
1034
1034
  name = "tokenfold-cli"
1035
- version = "0.3.4"
1035
+ version = "0.4.1"
1036
1036
  dependencies = [
1037
1037
  "clap",
1038
1038
  "serde",
@@ -1045,7 +1045,7 @@ dependencies = [
1045
1045
 
1046
1046
  [[package]]
1047
1047
  name = "tokenfold-core"
1048
- version = "0.3.4"
1048
+ version = "0.4.1"
1049
1049
  dependencies = [
1050
1050
  "proptest",
1051
1051
  "regex",
@@ -1059,25 +1059,25 @@ dependencies = [
1059
1059
 
1060
1060
  [[package]]
1061
1061
  name = "tokenfold-image"
1062
- version = "0.3.4"
1062
+ version = "0.4.1"
1063
1063
 
1064
1064
  [[package]]
1065
1065
  name = "tokenfold-learn"
1066
- version = "0.3.4"
1066
+ version = "0.4.1"
1067
1067
  dependencies = [
1068
1068
  "tokenfold-core",
1069
1069
  ]
1070
1070
 
1071
1071
  [[package]]
1072
1072
  name = "tokenfold-output"
1073
- version = "0.3.4"
1073
+ version = "0.4.1"
1074
1074
  dependencies = [
1075
1075
  "tokenfold-core",
1076
1076
  ]
1077
1077
 
1078
1078
  [[package]]
1079
1079
  name = "tokenfold-proxy"
1080
- version = "0.3.4"
1080
+ version = "0.4.1"
1081
1081
  dependencies = [
1082
1082
  "clap",
1083
1083
  "serde",
@@ -1089,7 +1089,7 @@ dependencies = [
1089
1089
 
1090
1090
  [[package]]
1091
1091
  name = "tokenfold-py"
1092
- version = "0.3.4"
1092
+ version = "0.4.1"
1093
1093
  dependencies = [
1094
1094
  "pyo3",
1095
1095
  "serde_json",
@@ -1098,7 +1098,7 @@ dependencies = [
1098
1098
 
1099
1099
  [[package]]
1100
1100
  name = "tokenfold-rag"
1101
- version = "0.3.4"
1101
+ version = "0.4.1"
1102
1102
 
1103
1103
  [[package]]
1104
1104
  name = "toml"
@@ -3,7 +3,7 @@ resolver = "2"
3
3
  members = ["crates/tokenfold-core", "crates/tokenfold-py"]
4
4
 
5
5
  [workspace.package]
6
- version = "0.3.4"
6
+ version = "0.4.1"
7
7
  edition = "2024"
8
8
  rust-version = "1.97"
9
9
  license = "Apache-2.0"
@@ -0,0 +1,369 @@
1
+ Metadata-Version: 2.4
2
+ Name: tokenfold
3
+ Version: 0.4.1
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: License :: OSI Approved :: Apache Software License
7
+ Classifier: Programming Language :: Rust
8
+ Classifier: Programming Language :: Python :: Implementation :: CPython
9
+ Classifier: Programming Language :: Python :: 3 :: Only
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
+ Classifier: Topic :: Text Processing
17
+ License-File: LICENSE
18
+ Summary: Token-compression toolkit for LLM payloads (Python binding over the tokenfold-core Rust crate)
19
+ Keywords: llm,tokens,compression,tokenizer,openai,json,prompt-engineering,cli,token-optimization
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
22
+
23
+ <div align="center">
24
+
25
+ # TOKENFOLD
26
+
27
+ **Send less noise. Fit more context. Pay for fewer input tokens.**
28
+
29
+ **Up to 92% fewer input tokens · lossless by default · exact receipts**
30
+
31
+ CLI · Python · TypeScript · Rust · proxy · MCP · local-first · provider-neutral
32
+
33
+ [![CI](https://img.shields.io/github/actions/workflow/status/snchimata/tokenfold/ci.yml?branch=main&label=tests&logo=github&style=flat-square)](https://github.com/snchimata/tokenfold/actions/workflows/ci.yml) [![Coverage](https://img.shields.io/github/actions/workflow/status/snchimata/tokenfold/ci.yml?branch=main&label=coverage&logo=github&style=flat-square)](https://github.com/snchimata/tokenfold/actions/workflows/ci.yml) [![GitHub Release](https://img.shields.io/github/v/release/snchimata/tokenfold?logo=github&style=flat-square)](https://github.com/snchimata/tokenfold/releases/latest) [![PyPI](https://img.shields.io/pypi/v/tokenfold?label=PyPI&style=flat-square)](https://pypi.org/project/tokenfold/) [![npm](https://img.shields.io/npm/v/tokenfold?label=npm&logo=npm&style=flat-square)](https://www.npmjs.com/package/tokenfold) [![Rust](https://img.shields.io/crates/v/tokenfold-core?label=Rust&style=flat-square)](https://docs.rs/crate/tokenfold-core/latest) [![License](https://img.shields.io/badge/license-Apache--2.0-blue?style=flat-square)](https://github.com/snchimata/tokenfold/blob/main/LICENSE)
34
+
35
+ [Measured results](#measured-results) · [Platform](#one-platform-two-engines) · [Quick start](#quick-start) · [Lossy pruning](#recoverable-lossy-pruning) · [Select model](#tokenfold-select) · [Reproduce](#reproduce-the-results)
36
+
37
+ </div>
38
+
39
+ ---
40
+
41
+ ## Measured results
42
+
43
+ ### Compression
44
+
45
+ | Search / RAG results | Repetitive JSON | API responses | Tool schemas |
46
+ | :---: | :---: | :---: | :---: |
47
+ | **91.7% fewer tokens** | **67.6% fewer tokens** | **61.3% fewer tokens** | **45.6% fewer tokens** |
48
+ | 84,032 → 6,951 | 50-record payload | 30-record payload | 1.8 MB OpenAI fixture |
49
+ | opt-in, recoverable | lossless | lossless | lossless |
50
+
51
+ All four use exact `o200k_base` counts in balanced mode. The 91.7% showcase
52
+ keeps the planted `503` result inline and makes the other 98 long rows
53
+ retrievable by hash. Run it with `python examples/lossy_pruning.py`.
54
+
55
+ ### Fine-tuned relevance ranking
56
+
57
+ [Tokenfold Select][tokenfold-select] is the optional query-aware model for
58
+ choosing what fills a tight context window after structural compression ends.
59
+
60
+ | Token budget kept | Tokenfold Select task success | BM25 reference | Lift |
61
+ | ---: | ---: | ---: | ---: |
62
+ | 50% | **86.3%** | 79.4% | **+8.7%** |
63
+ | 25% | **70.3%** | 60.7% | **+15.8%** |
64
+ | 10% | **39.9%** | 37.7% | **+5.8%** |
65
+
66
+ At a 25% budget, the fine-tuned ranker keeps the answer **70% of the time** and
67
+ beats the strongest free heuristic by **15.8%**. Critical-content survival is
68
+ **100% at every measured budget** through allocator force-keep. Results are
69
+ three-seed repeated subsampling over roughly 73,000 training and 24,000
70
+ held-out fixtures per run; the [model card][tokenfold-select] publishes the
71
+ training recipe and full baseline set. Here, task success means the literal
72
+ gold answer survived selection under the stated budget.
73
+
74
+ ## One platform, two engines
75
+
76
+ | Capability | What you get |
77
+ | --- | --- |
78
+ | **Tokenfold Core** | Fast deterministic compression for JSON, schemas, provider requests, logs, diffs, and command output |
79
+ | **Recoverable pruning** | Drop low-signal JSON rows only after storing them locally; fetch any omission with `tokenfold retrieve` |
80
+ | **Tokenfold Select** | LoRA-fine-tuned, query-aware span ranking with up to **15.8% lift** over BM25 at the same budget |
81
+ | **Budget control** | Conservative, balanced, and aggressive modes; nine task scopes; `--target-tokens` with honest best-effort reporting |
82
+ | **Exact receipts** | Before/after token counts, applied transforms, warnings, provenance, and final status on every call |
83
+ | **Realized savings** | `gain`, `stats`, and `session` report measured savings; `learn` proposes policy improvements without silently applying them |
84
+ | **Every integration** | CLI, Python, TypeScript, Rust, HTTP proxy, and MCP share the same Core engine and report shape |
85
+ | **Local-first safety** | Secret redaction, protected-content gates, reversible structural transforms, and no hosted data processor |
86
+
87
+ ### Lossless or recoverable lossy
88
+
89
+ | | Lossless — default | Recoverable lossy — opt-in |
90
+ | --- | --- | --- |
91
+ | **What it does** | Minifies, folds repeated keys into columns, and stores repeated values once | Adds deterministic ranking and retrieval markers for selected array rows |
92
+ | **Best on** | Uniform records, schemas, logs, diffs | Search results, mixed event feeds, agent traces, long arrays |
93
+ | **Measured here** | **45.6–67.6%** fewer tokens | **39.3–91.7%** fewer tokens |
94
+ | **Recovery** | All data remains in the payload | Every emitted marker resolves through the local retrieval store |
95
+
96
+ ## Quick start
97
+
98
+ Install the surface that fits your stack:
99
+
100
+ ```bash
101
+ pip install tokenfold # Python 3.9+
102
+ npm install tokenfold # Node.js 22+
103
+ cargo add tokenfold-core # Rust library
104
+ cargo install tokenfold-cli # Rust CLI
105
+ ```
106
+
107
+ Or download a signed CLI build for Linux, macOS, or Windows from
108
+ [GitHub Releases](https://github.com/snchimata/tokenfold/releases/latest) and
109
+ verify it with the adjacent `.sha256` file.
110
+
111
+ Preview first, then compress:
112
+
113
+ ```bash
114
+ tokenfold inspect payload.json --format json
115
+ tokenfold compress payload.json --format json --output payload.compact.json
116
+ ```
117
+
118
+ Python uses the same Core engine and typed receipt:
119
+
120
+ ```python
121
+ import json
122
+ from pathlib import Path
123
+
124
+ from tokenfold import CompressionMode, compress_openai_payload
125
+
126
+ result = compress_openai_payload(
127
+ Path("request.json").read_text(),
128
+ mode=CompressionMode.BALANCED,
129
+ )
130
+ compressed_request = json.loads(result.payload)
131
+ print(f"saved {result.report.saved_tokens} tokens ({result.saved_pct():.1f}%)")
132
+ ```
133
+
134
+ | Surface | Best for | Start here |
135
+ | --- | --- | --- |
136
+ | CLI | Files, stdin, diffs, and command output | `tokenfold compress`, `inspect`, `diff`, `wrap` |
137
+ | Python | Applications and evaluation pipelines | `pip install tokenfold` |
138
+ | TypeScript | Node.js applications and automation | `npm install tokenfold` |
139
+ | Rust | Native embedding | `cargo add tokenfold-core` |
140
+ | HTTP proxy | Transparent provider-shaped traffic | Build `tokenfold-proxy` |
141
+ | MCP | Agents and editors | `tokenfold mcp serve` |
142
+
143
+ `tokenfold init --agent <agent>` installs a durable host integration;
144
+ `tokenfold doctor` verifies it. Trusted filters for Git, build, and test output
145
+ are available through `tokenfold filters list`.
146
+
147
+ Runnable examples, one per surface, all under [`examples/`](examples):
148
+
149
+ ```bash
150
+ python examples/quickstart.py # Python: compress messages, request bodies, JSON data
151
+ node examples/quickstart.mjs # TypeScript/Node: compress, inspect, read the receipt
152
+ cargo run -p tokenfold-core --example quickstart # Rust: the embedded core API
153
+ python examples/lossy_pruning.py # CLI: opt-in recoverable pruning, end to end
154
+ ```
155
+
156
+ [`examples/quickstart.ipynb`](examples/quickstart.ipynb) is the notebook form of the
157
+ Python quickstart, one operation per cell.
158
+
159
+ ## Recoverable lossy pruning
160
+
161
+ Lossless folding has a ceiling on heterogeneous arrays. `--lossy` ranks rows,
162
+ keeps the strongest signals, and replaces selected rows with compact
163
+ `{"$tf_ref": {...}}` handles. A row leaves the payload only after the local
164
+ store accepts it.
165
+
166
+ ```bash
167
+ tokenfold compress examples/incident_feed.json --format json \
168
+ --lossy heuristic --lossy-ratio 0.35 --output feed.compact.json
169
+ ```
170
+
171
+ On the bundled 40-event feed:
172
+
173
+ | Mode | Exact tokens | Reduction | Events kept | Incident kept |
174
+ | --- | ---: | ---: | ---: | :---: |
175
+ | Lossless | 2,840 | 28.1% | 40 | ✅ |
176
+ | `--lossy-ratio 0.35` | 2,399 | **39.3%** | 13 | ✅ |
177
+ | `--lossy-ratio 0.05` | 2,192 | **44.5%** | 2 | ✅ |
178
+
179
+ The planted `503` with `success: false` and `retries: 7` survives every shown
180
+ setting because typed failure signals outrank position and length. Long rows
181
+ amortize marker overhead further: the 100-result showcase reaches **91.7%**.
182
+
183
+ Fetch a dropped row:
184
+
185
+ ```bash
186
+ tokenfold retrieve 978339a8898fedde3c5b0662a213f12ae4ad1b7fe6771f62b3c3d74d87389a4c
187
+ # {"seq":1,"ts":"2026-08-15T00:01:11Z","subsystem":"index-writer",...}
188
+ ```
189
+
190
+ What the flags mean:
191
+
192
+ - `--lossy-ratio` is an aggression hint over eligible array items. Lower keeps
193
+ fewer rows; it is not a whole-document guarantee.
194
+ - `--target-tokens` is the whole-document goal. Tokenfold stops when it reaches
195
+ the target losslessly and reports `best_effort` when the safe transform set
196
+ cannot reach it (`unreachable_target` when protected content alone exceeds
197
+ the target).
198
+ - `--lossy-preserve <path>` protects a named array; nested paths conservatively
199
+ protect their nearest eligible ancestor.
200
+ - Generic JSON only: lossy pruning does not run on OpenAI or Anthropic message
201
+ payloads.
202
+ - Storage is fail-closed: refused rows stay inline, and detected secret-shaped
203
+ bytes are never persisted.
204
+
205
+ Preview the projected savings with no store writes:
206
+
207
+ ```bash
208
+ tokenfold compress examples/incident_feed.json --format json \
209
+ --lossy heuristic --lossy-ratio 0.35 --dry-run
210
+ ```
211
+
212
+ The same flags, and the same fail-closed contract, from Python and TypeScript:
213
+
214
+ ```python
215
+ from tokenfold import CompressionPolicy, InputFormat, LossyPath, compress, retrieve
216
+
217
+ policy = CompressionPolicy(lossy=LossyPath.HEURISTIC, lossy_ratio=0.35)
218
+ result = compress(feed_bytes, format=InputFormat.JSON, policy=policy)
219
+ original = retrieve(marker["$tf_ref"]["hash"]) # any dropped row, verbatim
220
+ ```
221
+
222
+ ```ts
223
+ import { compress, retrieve } from "tokenfold";
224
+
225
+ const { payload, report } = await compress(feed, {
226
+ format: "json",
227
+ lossy: "heuristic",
228
+ lossyRatio: 0.35,
229
+ });
230
+ const original = await retrieve(hash); // any dropped row, verbatim
231
+ ```
232
+
233
+ <details>
234
+ <summary><strong>Current Phase 1 constraints</strong></summary>
235
+
236
+ Treat `$tf_ref` as reserved and do not enable lossy pruning on documents that
237
+ already contain retrieval markers. A filesystem failure after partial writes
238
+ may also leave unreferenced entries until their configured TTL expires. Both
239
+ require location-based transactional materialization before promotion.
240
+
241
+ Preview is a projection rather than a filesystem transaction, so a real run may
242
+ keep more rows if storage becomes unavailable.
243
+
244
+ </details>
245
+
246
+ ## Tokenfold Select
247
+
248
+ **When structure ends, rank what matters.**
249
+
250
+ Tokenfold Select is an Apache-2.0 LoRA adapter on
251
+ `ibm-granite/granite-embedding-reranker-english-r2`. It scores candidate spans
252
+ against a query; your allocator applies the token budget and force-keeps
253
+ required content. Core remains model-free and deterministic, while Select adds
254
+ relevance when lexical heuristics stop being enough.
255
+
256
+ | | Tokenfold Core | Tokenfold Select |
257
+ | --- | --- | --- |
258
+ | Best at | Structural compression | Query-conditioned span ranking |
259
+ | Runtime | Static Rust binary | Granite reranker + LoRA adapter |
260
+ | Output | Compressed payload + exact receipt | Ranking logits |
261
+
262
+ <details>
263
+ <summary><strong>Python: load the model and score spans</strong></summary>
264
+
265
+ ```python
266
+ from pathlib import Path
267
+
268
+ import torch
269
+ from huggingface_hub import snapshot_download
270
+ from peft import PeftModel
271
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
272
+
273
+ base_id = "ibm-granite/granite-embedding-reranker-english-r2"
274
+ repo_dir = Path(snapshot_download("snchimata/tokenfold-select"))
275
+ adapter_dir = repo_dir / "adapter"
276
+ tokenizer = AutoTokenizer.from_pretrained(adapter_dir)
277
+ base = AutoModelForSequenceClassification.from_pretrained(
278
+ base_id,
279
+ dtype=torch.float32,
280
+ )
281
+ model = PeftModel.from_pretrained(base, adapter_dir).eval()
282
+
283
+ def score(query: str, spans: list[str]) -> list[float]:
284
+ if not spans:
285
+ return []
286
+ encoded = tokenizer(
287
+ [query] * len(spans),
288
+ spans,
289
+ padding=True,
290
+ truncation=True,
291
+ max_length=8192,
292
+ return_tensors="pt",
293
+ )
294
+ with torch.no_grad():
295
+ output = model(
296
+ input_ids=encoded["input_ids"],
297
+ attention_mask=encoded["attention_mask"],
298
+ )
299
+ return output.logits.view(-1).float().tolist()
300
+ ```
301
+
302
+ </details>
303
+
304
+ See the [Tokenfold Select model card][tokenfold-select] for setup, evaluation,
305
+ training data, and limitations.
306
+
307
+ ## Safety and auditability
308
+
309
+ - **Never larger:** Core keeps a transform only when exact recounting shows a
310
+ reduction; a lossy branch must also beat the lossless result.
311
+ - **Reversible structure:** JSON folds must pass an exact round trip.
312
+ - **Protected content:** provider system messages and latest-user content are
313
+ held behind format-aware safety gates.
314
+ - **Clear provenance:** exact tokenizer counts, heuristics, and extrapolations
315
+ are labeled separately.
316
+ - **Actionable receipts:** every result lists savings, transforms, warnings,
317
+ retrieval state, and final status.
318
+ - **Local control:** detected secrets are redacted before reports or storage;
319
+ policy learning changes configuration only with `--apply`.
320
+
321
+ ## Reproduce the results
322
+
323
+ ```bash
324
+ # 91.7% maximum-compression showcase, mixed-feed curve, retrieval, and preserve
325
+ python examples/lossy_pruning.py
326
+
327
+ # Lossless transform benchmarks
328
+ cargo bench -p tokenfold-core
329
+
330
+ # Small exact-token CLI example
331
+ cargo run --release --locked -p tokenfold-cli -- \
332
+ inspect examples/api_response.json --format json
333
+ ```
334
+
335
+ The small bundled API response reports 382 → 206 tokens, a **46.1% lossless
336
+ reduction**. Benchmark sources and thresholds live in [CHANGELOG.md](https://github.com/snchimata/tokenfold/blob/main/CHANGELOG.md)
337
+ and [`crates/tokenfold-core/benches/THRESHOLDS.toml`](https://github.com/snchimata/tokenfold/blob/main/crates/tokenfold-core/benches/THRESHOLDS.toml).
338
+
339
+ ## Contributing
340
+
341
+ Issues and pull requests are welcome. Run the relevant checks before opening a
342
+ PR:
343
+
344
+ ```bash
345
+ cargo fmt --all --check
346
+ cargo clippy --workspace --all-targets -- -D warnings
347
+ cargo test --workspace --locked
348
+ python eval/run_fidelity.py --gate --profile smoke-first-consumer
349
+ cd packages/tokenfold && npm ci && npm test
350
+ ```
351
+
352
+ ## License
353
+
354
+ [Apache-2.0](https://github.com/snchimata/tokenfold/blob/main/LICENSE)
355
+
356
+ ---
357
+
358
+ Start with one representative payload, inspect the receipt, and see how many
359
+ tokens your application can stop sending today.
360
+
361
+ ```bash
362
+ pip install tokenfold
363
+ ```
364
+
365
+ If Tokenfold earns a place in your stack, a ⭐ on
366
+ [GitHub](https://github.com/snchimata/tokenfold) helps the next team find it.
367
+
368
+ [tokenfold-select]: https://huggingface.co/snchimata/tokenfold-select
369
+