Compress LLM prompts 50%+ while preserving meaning. Lightweight API client.
Project description
preduce
Compress LLM prompts 50%+ while preserving meaning. Cut your API costs in half with one line of code.
Install
pip install preduce
Quick Start
from preduce import compress
result = compress("Your verbose text here...", api_key="your-key")
print(result.compressed_text) # compressed output
print(result.category) # GENERAL, FINANCIAL, MEDICAL, CODE, ACADEMIC, SUPPORT, or MARKETING
print(result.token_reduction_pct) # e.g. 52.3
print(result.stats) # full reduction metrics
Category Override
result = compress(
"Total revenue for fiscal year 2025 was $847,300,000, "
"representing an increase of 22.4% year-over-year.",
api_key="your-key",
category="FINANCIAL",
)
# → "Total rev FY2025 $847.3M, +22.4% YoY."
Categories
| Category | What it compresses | Example |
|---|---|---|
| GENERAL | Business docs, emails, reports | "we are pleased to announce" → removed |
| FINANCIAL | Earnings, metrics, filings | "$847,300,000" → "$847.3M" |
| MEDICAL | Clinical notes, discharge summaries | "type 2 diabetes mellitus" → "T2DM" |
| CODE | Preserves code, compresses prose | Code untouched, comments compressed |
| ACADEMIC | Research papers, dissertations | "statistically significant" → "sig." |
| SUPPORT | Support tickets, help desk | "thank you for contacting" → "thanks for contacting" |
| MARKETING | Marketing copy, promotions | "limited time offer" → "limited time" |
Response
result.compressed_text # str — the compressed text
result.category # str — detected or overridden category
result.original_tokens # int — estimated original token count
result.compressed_tokens # int — estimated compressed token count
result.confidence_pct # dict — confidence % per category (e.g. {"FINANCIAL": 85.2, "GENERAL": 14.8, ...})
result.token_reduction_pct # float — percentage reduction
result.char_reduction_pct # float — percentage reduction
result.stats # dict — full metrics
result.to_dict() # dict — serialize everything
Confidence Scores
The API returns confidence percentages for each category, useful for debugging classification:
result = compress("Revenue increased 15% this quarter.", api_key="your-key")
print(result.confidence_pct)
# {"FINANCIAL": 78.5, "GENERAL": 12.3, "MARKETING": 9.2, ...}
print(result.category) # "FINANCIAL"
Error Handling
from preduce import compress
try:
result = compress("text", api_key="invalid-key")
except PermissionError:
print("Bad API key or quota exceeded")
except ValueError:
print("Invalid input (empty text or bad category)")
except RuntimeError:
print("API error")
Get an API Key
Visit preduce.dev to get your API key.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file preduce-0.3.0.tar.gz.
File metadata
- Download URL: preduce-0.3.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6010d1df41678e7b08be6c08ba0e8db1d81bcca03579e0d5b55f77c7e8cf4500
|
|
| MD5 |
b86d89a24e23e860325f08712966898e
|
|
| BLAKE2b-256 |
b7386372acca654ee38aec248dbd4684ac86cc0d23e59b6c73864f3493f4b043
|
File details
Details for the file preduce-0.3.0-py3-none-any.whl.
File metadata
- Download URL: preduce-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4061ef1b64ae2a2f1412952608325d20d8fbf4ebbce0172034eceee306bbf595
|
|
| MD5 |
77294341ad6fa65bc9f1cae3dc2ada31
|
|
| BLAKE2b-256 |
2e9fb27572487ee735106465a15fa2130b12fe9f2d75657a0070c80b1a4258fe
|