Skip to main content

Compress prompts to use fewer tokens without losing meaning

Project description

token-diet

Compress prompts to use fewer tokens without losing meaning.

token-diet is a CLI tool that takes any prompt — from a file, stdin, or a pipe — and rewrites it to be as compact as possible while preserving the original intent. It works offline using battle-tested rewrite rules, or via the Claude API (claude-haiku-4-5-20251001) for intelligent AI-powered compression.

Quick install

pip install token-diet

Or from source:

git clone https://github.com/your-handle/token-diet
cd token-diet
pip install -e .

Usage

# From a file
token-diet prompt.txt

# From stdin
echo "Please summarize the following text for me..." | token-diet

# With Claude API key (intelligent compression)
token-diet prompt.txt --api-key sk-ant-...

# Or set the env var once
export ANTHROPIC_API_KEY=sk-ant-...
token-diet prompt.txt

# Compression levels
token-diet prompt.txt --level aggressive   # max compression, may lose nuance
token-diet prompt.txt --level balanced     # default, preserves intent
token-diet prompt.txt --level gentle       # minimal, safest changes only

# Show a diff between original and compressed
token-diet prompt.txt --diff

# Output ONLY the compressed prompt (great for piping)
token-diet prompt.txt --quiet | pbcopy
token-diet prompt.txt --quiet > compressed.txt

Example output

╭──────────────────────────────────────────╮
│            TOKEN DIET REPORT             │
╰──────────────────────────────────────────╯

Mode: balanced compression (rule-based / offline)

────────────────────── BEFORE ──────────────────────
│ Please take the following document and create a   │
│ comprehensive summary that covers all of the main │
│ points and key details. I would like you to make  │
│ sure to include every important fact...           │

────────────────────── AFTER ───────────────────────
│ Take the following document and create a thorough │
│ summary covering all main points and key details. │
│ Include every important fact...                   │

────────────────────── Results ─────────────────────
  Original:   847 tokens  (~3,388 chars)
  Compressed: 234 tokens  (~936 chars)
  Saved:      613 tokens  (72.4% reduction)
  Est. savings: $0.0031/call  (at GPT-4o pricing $5/1M tokens)

────────────────────── Techniques applied ──────────
  • Removed filler phrases (8)
  • Shortened verbose constructions (12)
  • Collapsed redundant instructions (3)

How it works

Offline (no API key)

The rule engine applies linguistically safe substitutions in three tiers:

Level What it removes
gentle Politeness openers ("Please", "Could you please", "I would like you to"), obvious verbose forms ("in order to", "due to the fact that")
balanced Everything in gentle, plus hedging adverbs ("very", "quite", "basically"), discourse markers ("of course", "clearly", "in fact"), and more verbose constructions
aggressive Everything above, plus restructuring question openings ("Can you help me…"), discourse section headers, and more

With API key

Sends your prompt to claude-haiku-4-5-20251001 with a meta-prompt asking it to rewrite for minimum tokens. Falls back gracefully to rule-based compression on any API error.

Token counting

Uses a simple 4 chars ≈ 1 token heuristic by default. Install tiktoken for accurate OpenAI tokeniser counts:

pip install "token-diet[tiktoken]"

Options

Flag Default Description
--api-key KEY $ANTHROPIC_API_KEY Anthropic API key for Claude-powered compression
--level balanced Compression level: gentle, balanced, aggressive
--diff off Show unified diff between original and compressed
--quiet / -q off Output only the compressed text (no formatting)
--version Show version and exit
--help / -h Show help and exit

Cost estimate

The report shows an estimated cost saving per API call based on GPT-4o input pricing ($5 / 1M tokens). This is illustrative — actual savings depend on which model you use and your actual call volume.

Development

# Clone and install in dev mode
git clone https://github.com/your-handle/token-diet
cd token-diet
python -m venv venv && source venv/bin/activate
pip install -e ".[dev]"

# Run tests
pytest -v

# Run tests with coverage
pytest --cov=src --cov-report=term-missing

# Lint and format
black src/ tests/
isort src/ tests/
flake8 src/ tests/
mypy src/

License

MIT

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

token_diet-0.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

token_diet-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file token_diet-0.1.0.tar.gz.

File metadata

  • Download URL: token_diet-0.1.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for token_diet-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ee26ef079401c18e4535ea8f9e563bd5b8e3048da124d39f3b574850a73d8ed
MD5 3b49de46b60782b6b14fdf5d1d5ce92f
BLAKE2b-256 b323576b49d25cbf695bb020f4bbfa8873e1a90eb8ae54c05ae9315edbc08718

See more details on using hashes here.

File details

Details for the file token_diet-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: token_diet-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for token_diet-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4522affaf7dbf50a6ef1c47791b9fd55ab2d806dfd50fcfc748cc92e1f9fb279
MD5 27bbaf35dda16879be85d1f0bed0c4b1
BLAKE2b-256 fc6f05f43f78d4e8a78e001eaf51568084c8776d121d825f4688777d0e69b9f9

See more details on using hashes here.

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