Skip to main content

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

Metadata

Release files for token-diet 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 token-diet 0.1.0
File Size Uploaded
token_diet-0.1.0.tar.gz 14.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for token-diet 0.1.0
File Interpreter ABI Platform
token_diet-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / token_diet-0.1.0.tar.gz

Download URL token_diet-0.1.0.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2ee26ef079401c18e4535ea8f9e563bd5b8e3048da124d39f3b574850a73d8ed
BLAKE2b-256 checksum
How to use checksums
b323576b49d25cbf695bb020f4bbfa8873e1a90eb8ae54c05ae9315edbc08718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

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

Download URL token_diet-0.1.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4522affaf7dbf50a6ef1c47791b9fd55ab2d806dfd50fcfc748cc92e1f9fb279
BLAKE2b-256 checksum
How to use checksums
fc6f05f43f78d4e8a78e001eaf51568084c8776d121d825f4688777d0e69b9f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

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