diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3284b5f..2ce4acc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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/ links never go stale. + cp "$name.tar.gz" "$BIN-${{ matrix.target }}.tar.gz" - name: Package (Windows) if: runner.os == 'Windows' @@ -101,6 +103,10 @@ 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/ 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: @@ -108,6 +114,7 @@ jobs: path: | ${{ env.BIN }}-*.tar.gz ${{ env.BIN }}-*.zip + ${{ env.BIN }}-*.exe release: name: Publish GitHub Release @@ -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\`" diff --git a/CHANGELOG.md b/CHANGELOG.md index 01e7342..048f0ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/README.md b/README.md index c8b7f4c..33294f0 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,46 @@ -

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.

- # 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.** -

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.

+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? +

Download for Windows (.exe)  ·  macOS and Linux  ·  Project site  ·  Docs

-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`) | +

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.

## 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` | -
Unsigned binary warnings +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. -
+## How it works -## Use it in 3 steps +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' @@ -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. - -
What it checks +**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. -
- -
All options - -```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. -
+**3. Exact token boundaries.** In a terminal each token is a colored chip (the GIF above); with colors off you get an indexed grid: -
From source, Hugging Face tokenizers, and use from Python +```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") -``` -
+## 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). diff --git a/assets/how-it-works.svg b/assets/how-it-works.svg new file mode 100644 index 0000000..d0f4e17 --- /dev/null +++ b/assets/how-it-works.svg @@ -0,0 +1 @@ +How Token Visualizer splits a prompt into tokens, counts them and measures savingsA real run on examples/support-prompt.txt with the GPT-4o tokenizer. Line 2 is cut into 16 tokens with their IDs. Line counts are 12, 16, 17, 18, 7, total 70 tokens, 357 characters, estimated $0.0021. Swapping wordy phrases and re-tokenizing takes it from 70 to 61 tokens.How Token Visualizer counts and costs a promptReal run: token-visualizer examples/support-prompt.txt -m gpt-4o (tiktoken o200k_base)1The model's tokenizer cuts each line into tokens (numbered pieces)Line 2: In order to help the customer, read their whole message carefully before you answer.In637·order2569·to316·help1652·the290·customer5989,11·read1729·their1043·whole6062·message3176·carefully18455·before2254·you481·answer6052.1316 tokens. · is a space: most tokens carry the space before the word. The number is the token ID.2Count every line, then the whole promptLine 112Line 216Line 317Line 418Line 57bar colour: green under 25 tokens, yellow 25 to 50, red over 50Total tokens70Characters357Chars per token5.10Est. cost$0.0021at the old GPT-4 price, $0.03 per 1K3Swap wordy phrases, re-tokenize, and measure what you saved"In order to"→"To"3 → 1 tokens"Due to the fact that"→"Because"5 → 1 tokens"In the event that"→"If"4 → 1 tokensMeasured, not guessed70 → 61-9 tokens, 13% shorterThe five phrases it knows: in order to, due to the fact that, at this point in time, for the purpose of, in the event that.Extra spaces and blank lines are squeezed too. Then the new text goes through the same tokenizer again.Colours match the terminal output. Numbers from a real run of token_visualizer.py. \ No newline at end of file diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md new file mode 100644 index 0000000..14178e4 --- /dev/null +++ b/docs/REFERENCE.md @@ -0,0 +1,95 @@ +# Token Visualizer reference + +[README](../README.md) · [Reference](REFERENCE.md) · [Project site](https://mattbusel.github.io/Token-Visualizer/) + +## All options + +```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 +``` + +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. + +## What it checks + +- **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. + +The measured savings are not a guess: the tool applies its own phrase swaps and whitespace fixes to your text and tokenizes the result again with the same tokenizer. + +## Install details + +| Platform | Command | +| --- | --- | +| Windows (download) | [token-visualizer-windows-x86_64.exe](https://github.com/Mattbusel/Token-Visualizer/releases/latest/download/token-visualizer-windows-x86_64.exe) | +| 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 .exe and 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). + +**Unsigned binary warnings.** 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. + +## From source, Hugging Face tokenizers, and use from Python + +Python 3.8+. `tiktoken` is required for real GPT counts; `transformers` is optional. + +```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 +``` + +The old `python "Token Visualizer.py"` command still works. + +```python +import token_visualizer as tv + +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") +``` + +## Token-Visualizer or tokenviz? + +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`) | + +## Limitations + +- 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. diff --git a/docs/assets/demo.gif b/docs/assets/demo.gif new file mode 100644 index 0000000..9f4c179 Binary files /dev/null and b/docs/assets/demo.gif differ diff --git a/docs/assets/how-it-works.svg b/docs/assets/how-it-works.svg new file mode 100644 index 0000000..d0f4e17 --- /dev/null +++ b/docs/assets/how-it-works.svg @@ -0,0 +1 @@ +How Token Visualizer splits a prompt into tokens, counts them and measures savingsA real run on examples/support-prompt.txt with the GPT-4o tokenizer. Line 2 is cut into 16 tokens with their IDs. Line counts are 12, 16, 17, 18, 7, total 70 tokens, 357 characters, estimated $0.0021. Swapping wordy phrases and re-tokenizing takes it from 70 to 61 tokens.How Token Visualizer counts and costs a promptReal run: token-visualizer examples/support-prompt.txt -m gpt-4o (tiktoken o200k_base)1The model's tokenizer cuts each line into tokens (numbered pieces)Line 2: In order to help the customer, read their whole message carefully before you answer.In637·order2569·to316·help1652·the290·customer5989,11·read1729·their1043·whole6062·message3176·carefully18455·before2254·you481·answer6052.1316 tokens. · is a space: most tokens carry the space before the word. The number is the token ID.2Count every line, then the whole promptLine 112Line 216Line 317Line 418Line 57bar colour: green under 25 tokens, yellow 25 to 50, red over 50Total tokens70Characters357Chars per token5.10Est. cost$0.0021at the old GPT-4 price, $0.03 per 1K3Swap wordy phrases, re-tokenize, and measure what you saved"In order to"→"To"3 → 1 tokens"Due to the fact that"→"Because"5 → 1 tokens"In the event that"→"If"4 → 1 tokensMeasured, not guessed70 → 61-9 tokens, 13% shorterThe five phrases it knows: in order to, due to the fact that, at this point in time, for the purpose of, in the event that.Extra spaces and blank lines are squeezed too. Then the new text goes through the same tokenizer again.Colours match the terminal output. Numbers from a real run of token_visualizer.py. \ No newline at end of file diff --git a/docs/assets/og-image.png b/docs/assets/og-image.png new file mode 100644 index 0000000..5459547 Binary files /dev/null and b/docs/assets/og-image.png differ diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..0accb3e --- /dev/null +++ b/docs/index.html @@ -0,0 +1,169 @@ + + + + + + +Token Visualizer: free LLM token counter for prompts + + + + + + + + + + + + + + + + + + + + + + +
+
+

Token Visualizer

+
In order to help you, due to the fact
+

A free LLM token counter. See how GPT-4 or GPT-4o chops your prompt into tokens, which lines cost the most, and which wordy phrases to cut. The savings are measured by running the shorter text through the same tokenizer, not guessed.

+ +

Free, MIT licensed, works offline. Unsigned: if Windows SmartScreen asks, click More info, then Run anyway.

+
+ 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. +
A real run on a 5-line support-bot prompt with the GPT-4o tokenizer: 70 tokens, 61 after the suggested cuts.
+
+
+ +
+
+

How it works

+
+ Diagram from a real run with the GPT-4o tokenizer. Step 1: a line of the prompt is cut into 16 numbered tokens. Step 2: each line is counted, 12, 16, 17, 18 and 7 tokens, 70 in total, about $0.0021. Step 3: wordy phrases are swapped for short ones and the text is tokenized again: 70 to 61 tokens, 13 percent shorter. +
+
+

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, green under 25, yellow 25 to 50, red over 50, and the prompt gets a total and a rough cost.

+

3. Measure

It swaps wordy phrases for short ones, squeezes extra whitespace, tokenizes the new text again and reports the real difference.

+
+
+
+ +
+
+

Examples

+

Real output from version 0.3.1.

+

One wordy sentence

+
$ 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%)
+

Exact token boundaries

+
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]
+

Most tokens carry the space in front of the word, so order and order are different tokens.

+

A 5-line support prompt

+
$ token-visualizer examples/support-prompt.txt -m gpt-4o
+  Verbose phrases found:
+     'in order to' → 'to'
+     'due to the fact that' → 'because'
+     'in the event that' → 'if'
+MEASURED SAVINGS
+  Applying the phrase and whitespace fixes: 70 → 61 tokens (-9, 13%)
+
+
+ +
+
+

Install

+
+ + + + + + +
PlatformHow
WindowsDownload the .exe, double-click it, paste your prompt
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
+

Use it in 3 steps

+
    +
  1. Get it with any line above.
  2. +
  3. Run it on your prompt: token-visualizer prompt.txt -m gpt-4o, or double-click the .exe and paste (end with Ctrl+Z, then Enter).
  4. +
  5. Cut what it flags and run it again to see the new count.
  6. +
+

Claude has no public tokenizer, so claude-3-sonnet falls back to whitespace splitting and says so. For Llama and other open models, pass a Hugging Face model ID (from source, with transformers). Full options: reference.

+
+
+
+ + + + diff --git a/pyproject.toml b/pyproject.toml index 30e682b..4cba80c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "token-visualizer" -version = "0.3.0" +version = "0.3.1" description = "See how an LLM tokenizer splits your prompt, which lines cost the most, and what to cut" readme = "README.md" requires-python = ">=3.8" @@ -20,7 +20,8 @@ dev = ["pytest>=7"] token-visualizer = "token_visualizer:main" [project.urls] -Homepage = "https://github.com/Mattbusel/Token-Visualizer" +Homepage = "https://mattbusel.github.io/Token-Visualizer/" +Source = "https://github.com/Mattbusel/Token-Visualizer" [tool.setuptools] py-modules = ["token_visualizer"] diff --git a/token_visualizer.py b/token_visualizer.py index 1f870f0..fed1dff 100644 --- a/token_visualizer.py +++ b/token_visualizer.py @@ -13,7 +13,7 @@ from dataclasses import dataclass from typing import List, Tuple, Dict, Optional -__version__ = "0.3.0" +__version__ = "0.3.1" MODEL_OPTIONS = ["gpt-4", "gpt-4o", "gpt-3.5-turbo", "claude-3-sonnet", "llama-2-7b"]