Every call to an LLM API is billed per token. The system prompt, the tool definitions,
the format rules: you pay for all of it on every single request.
Most prompts carry waste nobody notices. A JSON schema pretty-printed with deep
indentation. A rule written twice in different words. A paragraph pasted twice.
"I would like you to please…" in front of an instruction. Each costs a few tokens, on
every call, forever.
tokensaver is a free, open-source command-line
tool that finds that waste, tells you what each fix saves in tokens and dollars, and
applies only the changes that can't change what your prompt means.
pip install prompt-tokensaver tokensaver optimize prompt.txt It runs fully offline. Your prompts never leave your machine.
What it can do
1. Count tokens and cost: count
tokensaver count prompt.txt --model gpt-4o One number per prompt: tokens, cost per call and cost per 1,000 calls. On the bundled
example, a customer-support system prompt, that's 834 tokens, $2.08 per 1,000 calls on
GPT-4o.
2. Find the cheapest model: compare
tokensaver compare prompt.txt The same prompt across 20 OpenAI, Anthropic and Google models, cheapest first:
| Model | Tokens | $ per 1k calls |
|---|---|---|
| gpt-4o-mini | 834 | $0.13 |
| gemini-2.5-flash | ~876 | $0.26 |
| gpt-4o | 834 | $2.08 |
| claude-sonnet-5-5 | ~1,251 | $2.50 |
Same prompt, a 20× price difference between the cheapest and the most expensive of these
four. Counts are exact for OpenAI models and clearly marked as estimates (~)
for Claude and Gemini, because there's no offline tokenizer for those.
3. Get concrete, priced suggestions: optimize
tokensaver optimize prompt.txt This is the heart of tokensaver. Six rules look for waste:
| Rule | What it finds | Risk |
|---|---|---|
json | Pretty-printed JSON. Minified and re-parsed to prove the data didn't change. | safe |
whitespace | Trailing spaces, extra blank lines. | safe |
duplicates | Duplicate lines and (near-)duplicate paragraphs. | review |
repeated-instructions | The same rule in different words ("Always respond in JSON" … "Remember to always respond in JSON"). | review |
filler | "I would like you to please…", "As an AI language model…", "Thanks in advance!", "in order to" → "to". | review |
long-examples | Few-shot examples over ~150 tokens, or too many of them. Flagged, never edited. | flag |
Every suggestion is measured: tokensaver counts the prompt before and after the
change on your model, so you see the real saving, not a guess.
Safe changes are applied by default. Review changes are listed with their savings
and applied only when you ask. Your original file is never overwritten.
On the bundled example prompt:
| Tokens | Saved | |
|---|---|---|
| Before | 834 | |
| Safe fixes only (default) | 772 | −7.4% |
All fixes (--apply all) | 665 | −20.3% |
That's $422 per million calls on GPT-4o, from one prompt.
tokensaver optimize prompt.txt --apply all -o prompt.min.txt 4. Scan a whole project: scan
tokensaver scan prompts/ Measures every prompt file in a folder and its subfolders: tokens, cost, and what the
safe fixes and all fixes would save, biggest saving first, with a total. It's the
fastest way to find which prompt in a repo is worth fixing first. Nothing is changed.
5. See what you pay for on every call: request
New in 0.3. A request isn't just the user's message. It also carries the system prompt
and the tool schemas, and those are the same on every call.
tokensaver request --system system.md --tools tools.json --user "Where is my order?" | Part | Tokens |
|---|---|
| System prompt | 834 |
| Tool schemas (2 tools) | ~142 |
| Fixed part | ~976 |
| User message "Where is my order?" | 5 |
The fixed part is 99% of the request. Repeat --user to compare several messages,
for example the same question in different languages: the user part changes, the fixed
part doesn't.
Shrink the fixed part and every request gets cheaper. On a 431-token support-bot prompt,
optimize --apply all brought it to 310 tokens, and each request dropped by 21%.
--tools reads OpenAI or Anthropic tool JSON, or a whole request body.
6. Use it in scripts and CI: --json
Every command can print one JSON object instead of tables. For example, fail a CI job
when a prompt picks up easy savings:
saved=$(tokensaver optimize prompts/system.txt --json | jq .tokens_saved) [ "$saved" -le 50 ] || { echo "Prompt has $saved tokens of easy savings"; exit 1; } Built to be trusted
- Offline by design. No network calls, no telemetry. The tokenizer files ship inside the package and are checked against their published hashes.
- Honest numbers. Exact counts are labelled exact; estimates always carry a
~. - Never changes meaning silently. Anything that could change meaning waits for your review. Few-shot examples and code blocks are never reworded.
- Works in your language. Token counting and the JSON, whitespace and duplicate rules are tested in CI on 55 languages, including right-to-left scripts and languages without spaces. The filler rule understands English and Persian, with more on the way.
- Well tested. 283 tests, CI on Windows, macOS and Linux with Python 3.10 and 3.13. MIT licensed.
Try it in five minutes
pip install prompt-tokensaver tokensaver optimize your_prompt.txt Want a guided run? The
step-by-step test guide
downloads two sample prompts and a tools file, and shows the expected output for every
command.
Help tokensaver grow
tokensaver is young, and every bit of help makes a difference:
- ⭐ Star the repo at github.com/behrouzbk/tokensaver. It's the main way other developers find it.
- 📣 Share it with a teammate who pays an LLM bill, or post your before/after numbers.
- 🐛 Try it on your prompts and open an issue for anything confusing, wrong or missing.
- 🌍 Speak another language? Add filler phrases for it in issue #5. No code needed, though PRs are welcome.
- 🛠️ Want to contribute code? Each optimizer rule is one small file with its own tests. Issue #1 is a good first issue, and CONTRIBUTING.md gets you to green tests in about five minutes.
- 📏 Have access to Claude or Gemini token counters? Help calibrate the estimates in issue #3.
TL;DR
-
count,compare,optimize,scanandrequest: from "how many tokens?" to "what should I change, and what does it save?" - 20 models, exact counts for OpenAI, labelled estimates for Claude and Gemini.
- A typical support prompt: −20.3% tokens, about $422 per million calls on GPT-4o.
- Offline, MIT, tested on 55 languages.
pip install prompt-tokensaver and give it one of your prompts. If it saves you tokens,
a ⭐ on GitHub helps others find it.

![[Part 3] Your first steps in Computer Vision on AWS: deploying models for an ASL recognition app](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fujtow25j4x4xr644q5s9.png)