Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ jobs:
mkdir "$name"
cp "dist/$BIN" README.md LICENSE "$name/"
tar czf "$name.tar.gz" "$name"
# Versionless copy so releases/latest/download/<file> links never go stale.
cp "$name.tar.gz" "$BIN-${{ matrix.target }}.tar.gz"

- name: Package (Windows)
if: runner.os == 'Windows'
Expand All @@ -101,13 +103,18 @@ jobs:
New-Item -ItemType Directory $name | Out-Null
Copy-Item "dist/$env:BIN.exe", README.md, LICENSE $name
Compress-Archive -Path $name -DestinationPath "$name.zip"
# Versionless copies so releases/latest/download/<file> links never go
# stale: the bare .exe (download and double-click) and the zip.
Copy-Item "dist/$env:BIN.exe" "$env:BIN-${{ matrix.target }}.exe"
Copy-Item "$name.zip" "$env:BIN-${{ matrix.target }}.zip"

- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}
path: |
${{ env.BIN }}-*.tar.gz
${{ env.BIN }}-*.zip
${{ env.BIN }}-*.exe

release:
name: Publish GitHub Release
Expand Down Expand Up @@ -140,7 +147,7 @@ jobs:
echo
echo "### Downloads"
echo
echo "- Windows: \`$BIN-$TAG-windows-x86_64.zip\`"
echo "- Windows: \`$BIN-windows-x86_64.exe\` (just the program) or \`$BIN-$TAG-windows-x86_64.zip\`"
echo "- macOS Apple Silicon: \`$BIN-$TAG-macos-arm64.tar.gz\`"
echo "- macOS Intel: \`$BIN-$TAG-macos-x86_64.tar.gz\`"
echo "- Linux: \`$BIN-$TAG-linux-x86_64.tar.gz\`"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.3.1] - 2026-09-28

- Releases also carry versionless files, including the bare `token-visualizer-windows-x86_64.exe`, so `releases/latest/download/...` links always point at the newest build. The versioned archives the install scripts and winget use are unchanged.
- README shortened to one screen: a direct Windows download, an animated how-it-works diagram from a real run, three real examples and three steps. Options, install details, Python use, the tokenviz comparison and limitations moved to `docs/REFERENCE.md`.
- A project site at https://mattbusel.github.io/Token-Visualizer/ (in `docs/`).

## [0.3.0] - 2026-09-25

- Token boundaries are shown as colored chips, one background color per token, instead of an indexed grid (the grid is still used when colors are off).
Expand Down
151 changes: 52 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,46 @@
<p align="center"><img src="assets/banner.png" alt="Token Visualizer: a real sentence split into colored GPT-4o tokens, 17 tokens before and 13 after the suggested fix; the example prompt goes from 70 to 61 tokens." width="100%"></p>

# Token Visualizer

**Shows you exactly how an AI model chops your prompt into tokens, which lines cost the most, and which wordy phrases to cut, with the real savings measured.**

[![CI](https://github.com/Mattbusel/Token-Visualizer/actions/workflows/ci.yml/badge.svg)](https://github.com/Mattbusel/Token-Visualizer/actions/workflows/ci.yml)
**See how an AI model like ChatGPT chops your prompt into tokens, which lines cost the most, and which wordy phrases to cut, with the savings measured, not guessed.**

<p align="center"><img src="assets/demo.gif" alt="A real terminal session: token-visualizer analyzes a 5-line support prompt with the GPT-4o tokenizer, shows each token as a colored chip, and measures 70 to 61 tokens after the suggested cuts." width="900"></p>
For anyone who writes prompts and pays per token: developers, prompt engineers, and anyone curious why a sentence "costs" what it does. A free LLM token counter for the terminal, using OpenAI's real tokenizers (GPT-4, GPT-4o, GPT-3.5) offline.

### Which one do I want?
<p align="center"><a href="https://github.com/Mattbusel/Token-Visualizer/releases/latest/download/token-visualizer-windows-x86_64.exe"><b>Download for Windows (.exe)</b></a> &nbsp;&middot;&nbsp; <a href="#install">macOS and Linux</a> &nbsp;&middot;&nbsp; <a href="https://mattbusel.github.io/Token-Visualizer/">Project site</a> &nbsp;&middot;&nbsp; <a href="docs/REFERENCE.md">Docs</a></p>

This repo has a sibling, [tokenviz](https://github.com/Mattbusel/tokenviz). Both count tokens with OpenAI's `tiktoken`; they answer different questions.

| You want to... | Use |
| --- | --- |
| See the exact token boundaries, one colored chip per token | **Token-Visualizer** (this one) |
| Get suggestions for wordy phrases, plus the token savings measured by re-tokenizing | **Token-Visualizer** |
| Count with a Hugging Face tokenizer (Llama, BERT, ...) | **Token-Visualizer** (from source, with `transformers`) |
| Paste a prompt into a window without touching a terminal (Windows double-click) | **Token-Visualizer** |
| Rank a long prompt's lines by cost with `--top` / `--threshold` | [tokenviz](https://github.com/Mattbusel/tokenviz) |
| Fail a CI job when a prompt goes over a token budget, or get JSON | [tokenviz](https://github.com/Mattbusel/tokenviz) (`--budget`, `--json`) |
<p align="center"><img src="assets/demo.gif" alt="A real terminal session: token-visualizer analyzes a 5-line support prompt with the GPT-4o tokenizer, shows each token as a colored chip, and measures 70 to 61 tokens after the suggested cuts." width="900"></p>

## Install

| Platform | Command |
| Platform | How |
| --- | --- |
| macOS / Linux (Homebrew) | `brew install mattbusel/tap/token-visualizer` |
| Windows (Scoop) | `scoop bucket add mattbusel https://github.com/Mattbusel/scoop-bucket; scoop install mattbusel/token-visualizer` |
| macOS / Linux (script) | `curl -fsSL https://raw.githubusercontent.com/Mattbusel/Token-Visualizer/main/install.sh \| sh` |
| Windows (PowerShell script) | `irm https://raw.githubusercontent.com/Mattbusel/Token-Visualizer/main/install.ps1 \| iex` |
| Any OS with Python 3.8+ | `pipx install git+https://github.com/Mattbusel/Token-Visualizer` |
| Manual download | [Latest release](https://github.com/Mattbusel/Token-Visualizer/releases/latest): Windows zip, macOS (Apple Silicon or Intel) and Linux tarballs |

The downloads are single files with the GPT tokenizers built in, so they work offline. The two scripts check the SHA-256 against the release's `SHA256SUMS.txt` before installing: `install.sh` puts `token-visualizer` in `~/.local/bin`, `install.ps1` puts `token-visualizer.exe` in `%LOCALAPPDATA%\Programs\token-visualizer` and adds it to your user PATH. Hugging Face tokenizers are not in the downloads; for those use pipx or source with `transformers` (below).
| **Windows** | [**Download token-visualizer-windows-x86_64.exe**](https://github.com/Mattbusel/Token-Visualizer/releases/latest/download/token-visualizer-windows-x86_64.exe), double-click it, paste your prompt. (Unsigned, so SmartScreen may ask: *More info*, then *Run anyway*.) |
| **Windows** (Scoop) | `scoop bucket add mattbusel https://github.com/Mattbusel/scoop-bucket; scoop install mattbusel/token-visualizer` |
| **macOS / Linux** (Homebrew) | `brew install mattbusel/tap/token-visualizer` |
| **macOS / Linux** (script) | `curl -fsSL https://raw.githubusercontent.com/Mattbusel/Token-Visualizer/main/install.sh \| sh` |
| **Any OS with Python 3.8+** | `pipx install git+https://github.com/Mattbusel/Token-Visualizer` |

<details><summary>Unsigned binary warnings</summary>
The downloads have the GPT tokenizers built in and work offline. More options (PowerShell one-liner, tarballs, Hugging Face tokenizers): [docs/REFERENCE.md](docs/REFERENCE.md#install-details).

Windows SmartScreen may say "unknown publisher": click **More info**, then **Run anyway**. On macOS, if a manually downloaded binary is blocked, right-click it and choose **Open** the first time, or run `xattr -d com.apple.quarantine token-visualizer`. Homebrew, Scoop and the install scripts do not trigger this.
</details>
## How it works

## Use it in 3 steps
<img src="assets/how-it-works.svg" width="100%" alt="Animated diagram from a real run on examples/support-prompt.txt with the GPT-4o tokenizer. Step 1: line 2 is cut into 16 numbered tokens, like In, order, to, help. Step 2: line counts 12, 16, 17, 18 and 7, total 70 tokens, 357 characters, estimated $0.0021. Step 3: 'In order to' becomes 'To', 'Due to the fact that' becomes 'Because', 'In the event that' becomes 'If', and re-tokenizing takes the prompt from 70 to 61 tokens, 13 percent shorter.">

1. **Save your prompt to a file** (or skip this and paste, see step 3).
1. **Cut.** The model's own tokenizer (`tiktoken`) splits each line into tokens, the numbered pieces a model reads and bills for.
2. **Count.** Every line gets a token count, colored green, yellow or red, and the whole prompt gets a total and a rough cost.
3. **Measure.** It swaps wordy phrases for short ones, squeezes extra whitespace, runs the new text through the same tokenizer, and reports the real difference.

2. **Run it on the file**, optionally naming the model whose tokenizer you care about:
```bash
token-visualizer prompt.txt # GPT-4 tokenizer (cl100k_base)
token-visualizer prompt.txt -m gpt-4o # GPT-4o tokenizer (o200k_base)
cat prompt.txt | token-visualizer # pipes work too, and never ask questions
```
You see totals, a per-line breakdown (green under 25 tokens, yellow 25 to 50, red over 50), every token as a colored chip (for texts up to 200 tokens), wordy phrases with shorter replacements, and how many tokens those fixes actually save.
## Examples

3. **Or just paste.** Run `token-visualizer` with nothing after it (or double-click `token-visualizer.exe` on Windows), paste your prompt, press Ctrl+D (Ctrl+Z then Enter on Windows), then pick a tokenizer from the menu.
All real output from `token-visualizer` 0.3.1 today.

## Results
**1. A 5-line support prompt** ([`examples/support-prompt.txt`](examples/support-prompt.txt)), the end of the report:

Real output from today for [`examples/support-prompt.txt`](examples/support-prompt.txt), a 5-line support-bot prompt, with `-m gpt-4o`. The token chips are in the GIF above; here is the end of the report:

```text
```console
$ token-visualizer examples/support-prompt.txt -m gpt-4o
TOKEN ANALYSIS - GPT-4O tiktoken o200k_base
Total tokens: 70
Total characters: 357
...
COMPRESSION SUGGESTIONS
────────────────────────────────────────────────────────────────────────
Verbose phrases found:
'in order to' → 'to'
'due to the fact that' → 'because'
Expand All @@ -70,72 +51,44 @@ MEASURED SAVINGS
Re-tokenized with the same tokenizer; $0.0003 less per request at $0.03 per 1K.
```

The savings are not a guess: the tool applies its own phrase swaps and whitespace fixes to your text and tokenizes the result again.

<details><summary>What it checks</summary>
**2. One wordy sentence, piped in:**

- **Token counts** with `tiktoken` for GPT model names (unknown GPT names fall back to `cl100k_base`), or a Hugging Face `AutoTokenizer` for any other model ID when `transformers` is installed. The header says which tokenizer was really used.
- **Line breakdown**: each line's tokens and characters per token. Lines over 50 tokens are listed again as expensive.
- **Token boundaries**: in a terminal, each token on its own colored background with `↵` for newlines; without colors, an indexed grid like `[0:You] [1: are]`.
- **Suggestions**: repeated words, five wordy phrases (`in order to`, `due to the fact that`, `at this point in time`, `for the purpose of`, `in the event that`), low characters per token, lines over 40 tokens, and extra whitespace.
- **Cost**: a rough figure at a fixed $0.03 per 1K input tokens (an old GPT-4 list price), clearly labeled as such.
</details>

<details><summary>All options</summary>

```text
usage: token-visualizer [-h] [-m MODEL] [--no-color] [--version] [file]

positional arguments:
file text file to analyze (default: read stdin)

options:
-h, --help show this help message and exit
-m MODEL, --model MODEL
tokenizer to use: gpt-4, gpt-4o, gpt-3.5-turbo,
claude-3-sonnet, llama-2-7b, any tiktoken model name,
or a Hugging Face model ID. Default gpt-4; only pasted
text gets a menu.
--no-color disable ANSI colors
--version show program's version number and exit
```console
$ echo "In order to help you, due to the fact that you asked." | token-visualizer
Total tokens: 14
...
Applying the phrase and whitespace fixes: 14 → 8 tokens (-6, 43%)
```

Colors turn off when output is not a terminal, with `--no-color`, or when `NO_COLOR` is set; `FORCE_COLOR=1` keeps them on in a pipe. The tokenizer menu only appears for pasted text; a file argument or a pipe uses `gpt-4` unless `-m` says otherwise, so scripts never hang.
</details>
**3. Exact token boundaries.** In a terminal each token is a colored chip (the GIF above); with colors off you get an indexed grid:

<details><summary>From source, Hugging Face tokenizers, and use from Python</summary>
```console
$ echo "In order to help you, due to the fact that you asked." | token-visualizer --no-color
TOKEN BREAKDOWN:
[0:In] [1: order] [2: to] [3: help] [4: you] [5:,] [6: due] [7: to] [8: the]
[9: fact] [10: that] [11: you] [12: asked] [13:.\n]
```

Python 3.8+. `tiktoken` is required for real GPT counts; `transformers` is optional.
Most tokens carry the space in front of the word, which is why ` order` and `order` are different tokens.

```bash
git clone https://github.com/Mattbusel/Token-Visualizer
cd Token-Visualizer
pip install -e ".[hf,dev]" # or: pip install tiktoken
pytest
python token_visualizer.py examples/support-prompt.txt -m bert-base-uncased # a Hugging Face model ID
```
## Use it in 3 steps

The old `python "Token Visualizer.py"` command still works.
1. **Get it:** download the .exe above, or `brew` / `scoop` / `pipx`.
2. **Run it on your prompt:** `token-visualizer prompt.txt -m gpt-4o` (or double-click the .exe and paste, then Ctrl+Z and Enter).
3. **Cut what it flags** and run it again to see the new count.

```python
import token_visualizer as tv
`token-visualizer --help` lists every option with examples.

viz = tv.TokenVisualizer("gpt-4o")
stats = viz.tokenize("Your prompt here")
print(stats.token_count, stats.efficiency)
print(viz.compress("In order to help you, due to the fact that you asked."))
# To help you, because you asked.
viz.visualize_tokens("Your prompt here")
viz.suggest_compression("Your prompt here")
```
</details>
## Documentation

## Limitations
| Doc | What is in it |
| --- | --- |
| [Reference](docs/REFERENCE.md) | All options, what it checks, install details, Hugging Face tokenizers, use from Python, limitations |
| [Token-Visualizer or tokenviz?](docs/REFERENCE.md#token-visualizer-or-tokenviz) | Which of the two sibling tools to use for what |
| [Changelog](CHANGELOG.md) | What changed in each release |

- Anthropic does not publish a Claude tokenizer, and `claude-3-sonnet` and `llama-2-7b` in the menu are not Hugging Face model IDs, so those two fall back to whitespace splitting (the header says so). For real counts outside OpenAI models, pass a valid Hugging Face model ID with `-m`.
- With no tokenizer library at all, everything uses whitespace splitting, which undercounts real tokens.
- The cost figure uses a fixed historical price and is only a rough guide.
Anthropic publishes no Claude tokenizer, so `-m claude-3-sonnet` falls back to whitespace splitting and the header says so. For Llama and other open models, pass a Hugging Face model ID (from source, with `transformers`). [Details](docs/REFERENCE.md#limitations).

## License

MIT
MIT, see [LICENSE](LICENSE).
1 change: 1 addition & 0 deletions assets/how-it-works.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading