Skip to main content

prompt-compressor

Rule-based LLM prompt compression — 30+ algorithms, zero dependencies.

PyPI version Python 3.9+ License: MIT

Compress prompts before sending them to any LLM API. No model required, no API calls, zero overhead. Works with OpenAI, Anthropic, AWS Bedrock, Cohere, or any token-based API.

🌐 Web app: prompt-compressor.com
📦 npm: prompt-compressor


Install

pip install prompt-compressor

Python API

from prompt_compressor import compress

result = compress(system_prompt, level="low")

print(result.output)           # compressed text
print(result.tokens_before)    # e.g. 1200
print(result.tokens_after)     # e.g. 820
print(result.saved)            # 380
print(result.reduction_pct)    # 31.7

Convenience function

from prompt_compressor import compress_text

compressed = compress_text(my_prompt, level="very-low")

With AWS Bedrock

import boto3
from prompt_compressor import compress

bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")

result = compress(system_prompt, level="low")

response = bedrock.invoke_model(
    modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
    body=json.dumps({
        "anthropic_version": "bedrock-2023-05-31",
        "system": result.output,   # compressed!
        "messages": [{"role": "user", "content": user_message}],
        "max_tokens": 1024,
    }),
)

CLI

# Compress a file
prompt-compressor system_prompt.txt --level low

# With stats
prompt-compressor system_prompt.txt --level medium --stats

# Pipe
cat my_prompt.txt | prompt-compressor --level very-low

# Save to file
prompt-compressor input.txt --level low --output compressed.txt

Levels

Level Algorithms Typical Reduction
none Whitespace, unicode, ANSI, punctuation 5–15%
very-low + Filler removal, phrase shortening, JSON/SQL minify, license headers 15–30%
low + Hedging, meta-commentary, transitions, number normalization, code comments, exact dedup 25–45%
medium + Fuzzy/ngram/Levenshtein dedup, frequent patterns, TF-IDF, TextRank 30–70%

Variable Protection

Variables in {{var}}, {var}, <tag>, and ${var} format are always protected by default:

result = compress("Hello {{name}}, query: {user_query}", level="medium")
# {{name}} and {user_query} are preserved exactly

Disable with protect_vars=False.


Algorithm Reference

Structural (none / very-low)

Algorithm Description
normalize_whitespace Collapse spaces, trailing whitespace, blank lines
normalize_unicode Replace fancy quotes, dashes, NBSP with ASCII
strip_ansi Remove ANSI escape codes
normalize_punctuation Remove !!!! → !, .... → ...
strip_license_headers Remove MIT/Apache/GPL header blocks
normalize_indentation 4-space → 2-space indent

Language (very-low / low)

Algorithm Description
remove_fillers "It is important to note that", "basically", "actually"
shorten_phrases "in order to" → "to", "make use of" → "use" (50+ rules)
remove_hedging "I think that", "In my opinion"
remove_meta_commentary "Sure, I'd be happy to help!", "Let me know if..."
remove_transitions Redundant "Furthermore,", "Additionally,"
remove_parentheticals Cross-reference parentheses
normalize_numbers "twenty" → "20", "five" → "5"
normalize_dates "January 5th, 2024" → "2024-01-05"

Code (low)

Algorithm Description
strip_code_comments # and // comments in fenced blocks
strip_docstrings Python triple-quoted docstrings
minify_json JSON code blocks → minified
remove_null_json_fields Remove null fields from JSON
minify_sql SQL blocks → whitespace-collapsed
minify_css CSS blocks → minified
strip_base64 Long base64 strings → [base64_data]
strip_hex_dumps Long hex strings → [hex_data]
truncate_stack_traces Keep first 5 frames
strip_ts_types TypeScript type annotations

Deduplication (low / medium)

Algorithm Description
dedup_exact_lines Consecutive duplicate lines
dedup_exact_sentences Exact duplicate sentences
dedup_fuzzy Jaccard similarity ≥ 0.85
dedup_ngram N-gram overlap ≥ 0.7
dedup_levenshtein Edit distance < 15%
remove_frequent_patterns Apriori-style repeated phrases
trie_prefix_compress Bullet groups with shared prefix

Extractive (medium)

Algorithm Description
tf_compress Keep sentences by term frequency
tfidf_compress Keep sentences by TF-IDF score
textrank_compress PageRank-style sentence scoring

Zero Dependencies

prompt-compressor uses only the Python standard library. No numpy, nltk, spacy, tiktoken, or anything else to install.


License

MIT © Tom Ketter

Metadata

Release files for prompt-compressor 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prompt-compressor 0.1.0
File Size Uploaded
prompt_compressor-0.1.0.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prompt-compressor 0.1.0
File Interpreter ABI Platform
prompt_compressor-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.9 kB

Release files / prompt_compressor-0.1.0.tar.gz

Download URL prompt_compressor-0.1.0.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
204061ef6793641cff4958da288c435eb9e1ab41afd7c2b4a9cc6cabd2faf23d
BLAKE2b-256 checksum
How to use checksums
543b61ff494ecc3e6b63d1f89f8914dc663a2615c83622523c495e505685ca13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 4, 2026.

Transparency log

Release files / prompt_compressor-0.1.0-py3-none-any.whl

Download URL prompt_compressor-0.1.0-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a98a3ddc85b696657ccd33e32f15c8b18ddf60f968f9c3443b8c576c6a5159e
BLAKE2b-256 checksum
How to use checksums
5821a237406e2ed02935562e8e5cd08d1232ee26d20f4c33bfe46bc822158278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page