Skip to main content

Minify LLM prompts to save tokens — domain-aware, tiktoken-validated, zero regressions.

Project description

PromptMin

Minify LLM prompts to save tokens — without losing meaning.

CI License: MIT Python 3.10+

LLM APIs charge per token, and non-English prompts (especially Spanish) can use 20–30% more tokens than their English equivalents. PromptMin rewrites your prompts into a denser form using curated, domain-aware substitutions, and reports real token savings measured with tiktoken.

Core guarantee. Every substitution rule is validated against tiktoken at runtime. If a rule does not reduce token count, it is skipped. PromptMin never makes your prompt longer.

Why?

Most "token savers" are naive find-and-replace scripts that don't actually save tokens (because BPE tokenizers don't count characters). PromptMin is different:

  • Mathematically validated. Rules are only applied when the tokenizer confirms they save tokens.
  • Curated, not massive. ~400 high-signal rules beat 100,000 random ones.
  • Domain-aware. Activate dictionaries by context (web, backend, ai…) for aggressive but safe compression.
  • Multi-tokenizer. Optimize for the model you target: GPT-4o, GPT-4, Claude, Gemini.
  • Bilingual. First-class Spanish + English, extensible to any language via YAML.
  • Honest benchmarks. Run promptmin benchmark on your own corpus and see real numbers.

Real numbers

Measured on the included corpus examples/corpus_domains.txt (18 mixed EN/ES technical prompts):

Savings by configuration (tokenizer: gpt-4):

Configuration Tokens saved Avg per prompt Best
General rules only 2.2% 1.9% 23.1%
+ domain dictionaries 23.9% 23.2% 38.5%
+ domains + translate + aggressive 25.3% 24.5% 42.3%

Savings by tokenizer (config: lite + all domains):

Tokenizer Family Total saved Notes
gpt-4 / cl100k OpenAI 23.9% Exact — used by GPT-4, GPT-3.5
claude Anthropic 23.9% Approximate (cl100k_base proxy)
gemini Google 23.9% Approximate (cl100k_base proxy)
gpt-4o / o200k OpenAI 21.7% Exact — used by GPT-4o, o1, o3

The newer gpt-4o tokenizer (o200k_base) is already more efficient on raw text than cl100k_base, which is why PromptMin has slightly less margin to optimize on it. This is the honest reality — and exactly why benchmarking matters.

Zero regressions across all modes and all tokenizers. The validator guarantees it.

Install

pip install promptminify

On PyPI the package is promptminify (because promptmin was already taken), but the CLI command and Python import name are both promptmin:

promptmin --help
from promptmin import minify

From source (development)

git clone https://github.com/DelvyG/promptmin.git
cd promptmin
pip install -e ".[dev]"

Usage

CLI

# Inline
promptmin run "Please, I would like you to build a function step by step"

# From file / clipboard / stdin
promptmin run --file prompt.txt
promptmin run --clipboard --out-clipboard
cat prompt.txt | promptmin run

# With domain dictionaries
promptmin run -d web,backend "Improve the user experience and add JWT authentication"

# Target a specific model's tokenizer
promptmin run -T claude "..."
promptmin run -T gpt-4o "..."
promptmin run -T gemini "..."

# List all available tokenizers
promptmin tokenizers

# Spanish with automatic EN technical translation
promptmin run -t "Por favor, desarrolla una función paso a paso para la base de datos"

# Aggressive mode (strips more filler)
promptmin run -m aggressive "..."

# List available domain dictionaries
promptmin domains

# Benchmark on a corpus
promptmin benchmark examples/corpus_domains.txt -d web,backend,devops,data,ai

# Just count tokens
promptmin count "hello world"

As a library

from promptmin import minify
from promptmin.tokens import savings

result = minify(
    "Please improve the user experience on mobile responsive devices",
    domains=["web"],
)
print(result["minified"])
print(savings(result["original"], result["minified"]))
# {'before': 11, 'after': 7, 'saved': 4, 'pct': 36.4}

How it works

┌─────────────┐
│  Your text  │
└──────┬──────┘
       ▼
┌─────────────────────┐
│ 1. Detect language  │  es / en (cheap heuristic)
└──────┬──────────────┘
       ▼
┌─────────────────────────────────────┐
│ 2. Domain dicts (highest priority)  │  e.g. "user experience" -> "UX"
└──────┬──────────────────────────────┘
       ▼
┌─────────────────────────┐
│ 3. Language dict        │  "please" -> "", "configuration" -> "config"
└──────┬──────────────────┘
       ▼
┌───────────────────────────────────┐
│ 4. ES→EN translation (optional)   │  "desarrolla" -> "build"
└──────┬────────────────────────────┘
       ▼
┌─────────────────────┐
│ 5. Stopword strip   │
└──────┬──────────────┘
       ▼
┌─────────────────────┐
│ 6. Whitespace clean │
└──────┬──────────────┘
       ▼
┌──────────────────────────────┐
│  Minified output + stats     │
└──────────────────────────────┘

The validator. Every step applies rules one at a time. Before accepting a substitution, it calls tiktoken.encode() on before/after. If tokens didn't drop, the rule is discarded. This is why PromptMin can't make your prompt worse.

Dictionary architecture

src/promptmin/dicts/
├── en.yaml              General English rules
├── es.yaml              General Spanish rules
├── es_en.yaml           Spanish → English technical translation
└── domains/
    ├── ai.yaml          LLM / RAG / CoT / fine-tuning / embeddings
    ├── backend.yaml     API / JWT / ORM / middleware / queues
    ├── data.yaml        ETL / warehouses / KPIs / schemas
    ├── devops.yaml      CI/CD / Kubernetes / SLO / observability
    └── web.yaml         UX / UI / SPA / PWA / responsive

Each file is plain YAML: "long phrase": "short version". No code required to contribute a new domain — drop a YAML file in dicts/domains/ and it's automatically picked up by promptmin domains.

Roadmap

  • Phase 1 — MVP CLI (EN + ES dicts, tiktoken-validated)
  • Phase 2 — Benchmark on real corpus, lite / aggressive / translate modes
  • Phase 2.5 — Domain dictionaries (web, backend, devops, data, ai)
  • Phase 3 — Multi-tokenizer support: GPT-4o / GPT-4 / Claude / Gemini
  • Phase 3.5 — Official SDK integration for exact Claude/Gemini counts (optional extras)
  • Phase 3.5 — Light stemming for Spanish conjugations (no heavy NLP deps)
  • Phase 4 — VSCode extension ("Minify Selection" + keybinding)
  • Phase 4 — More domains: mobile, security, gamedev, finance
  • Phase 4 — Domain auto-detection from prompt content

Contributing

Contributions are very welcome — especially new domain dictionaries and language support. See CONTRIBUTING.md.

The lowest-friction contribution is a new domain YAML: no Python, no tests, just curated phrases.

License

MIT © DelvyG

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

promptminify-0.2.1.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

promptminify-0.2.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file promptminify-0.2.1.tar.gz.

File metadata

  • Download URL: promptminify-0.2.1.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for promptminify-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ec4b1548d3d1c835c87107cc99a9d314fac081f652e2d7df6f38aea6ccb4dce4
MD5 344c0ffa26c445671224a618b3a5046c
BLAKE2b-256 efdabb8017c4fe84218a43715d5924dad43a1faf2884df1a3a43a7b00e445409

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptminify-0.2.1.tar.gz:

Publisher: publish.yml on DelvyG/promptmin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file promptminify-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: promptminify-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for promptminify-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fcbb629af5bc0b5f26b34e97ea2f21ae99fb6ca529ddcaf4363b72b4a5ce110a
MD5 04e121fbdcdc2a39e4e014b3c99d8934
BLAKE2b-256 f0edc3c6bfd838e801b5b9ee277339c1fa123be32ee2bd84d95be28628e5fe0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for promptminify-0.2.1-py3-none-any.whl:

Publisher: publish.yml on DelvyG/promptmin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page