TokenPress
Blazing-fast LLM token compression engine, built in Rust.
2–5× fewer tokens. Same or better output quality. Any model, any language.
Why TokenPress?
LLM API calls are expensive. Most prompt tokens are redundant — filler words, predictable syntax, boilerplate. TokenPress uses information theory to score every token by its surprise value, keeps only what matters, and sends a compressed prompt to the LLM.
- 18.6 M tok/s Rust core (PyO3 + rayon)
- 3.3× compression with near-zero quality loss
- Drop-in wrappers for OpenAI & Anthropic — zero code changes
- 6 selection strategies: ratio, top-k, threshold, percentile, IQR, token merging
- Language & model agnostic — works on any text, any LLM
Install
pip install tokenpress # core
pip install "tokenpress[models]" # + scoring model (distilgpt2)
pip install "tokenpress[all]" # + OpenAI & Anthropic wrappers
Quick Start
import tokenpress
result = tokenpress.compress("Your very long prompt …", ratio=0.3)
print(result.compressed_text) # compressed prompt
print(f"{result.compression_ratio:.1f}× smaller, {result.savings_percentage:.0f}% tokens saved")
Drop-in OpenAI wrapper:
import openai, tokenpress
client = tokenpress.wrap(openai.OpenAI(), ratio=0.3)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": very_long_prompt}],
)
# same API, 3× cheaper
CLI:
tokenpress compress document.txt -r 0.3 -o compressed.txt
tokenpress bench document.txt
How It Works
Each token is scored by self-information: $I(x_i) = -\log_2 P(x_i \mid x_{<i})$
High surprise → keep. Low surprise → remove. Pure math, no heuristics.
Built on research from Selective Context (EMNLP '23), TRIM (COLING '25), Token Merging (ICLR '23), H2O (NeurIPS '24), and LLMLingua-2 (ACL '24).
Links
- GitHub — full docs, benchmarks, architecture
- Benchmarks — 7 reproducible benchmark suites
- License — Apache-2.0
Metadata
Release files for tokenpress 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tokenpress-0.1.1.tar.gz | 131.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokenpress-0.1.1-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details |
Total release size: 294.7 kB
Release files / tokenpress-0.1.1.tar.gz
| Download URL | tokenpress-0.1.1.tar.gz |
|---|---|
| Size | 131.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d58a619639e9be544c47de2d72e2c44fcb42b26020e073505da3066540b140ba
|
|
BLAKE2b-256 checksum How to use checksums |
dd8a162aa2dbd041d09f98802568b908e30c5b76b6152f4cbe71bc2eadad5756
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.12.6
|
Release files / tokenpress-0.1.1-cp311-cp311-win_amd64.whl
| Download URL | tokenpress-0.1.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 163.8 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
11e31420ced7d9b1e3a5c0245c91015a4d4885a802da7691461f261ec406e240
|
|
BLAKE2b-256 checksum How to use checksums |
c4ba9cc2fa4a8fa12d3596dac17897e7561c9fe077cfe0d8ad34fe3349658743
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.12.6
|