# count-tokens
Zsh function that counts Claude tokens in files using the `Xenova/claude-tokenizer`.
```zsh
count-tokens() {
uv run --with tokenizers --with rich python -c "
import sys
from tokenizers import Tokenizer
from rich.table import Table
from rich.console import Console
t = Tokenizer.from_pretrained('Xenova/claude-tokenizer')
files = sys.argv[1:]
counts = [(f, len(t.encode(open(f, encoding='utf-8').read()).ids)) for f in files]
if len(counts) == 1:
print(counts[0][1])
else:
table = Table(title='Token Counts')
table.add_column('File', style='cyan')
table.add_column('Tokens', justify='right', style='green')
for f, c in counts:
table.add_row(f, f'{c:,}')
table.add_section()
table.add_row('Total', f'{sum(c for _, c in counts):,}', style='bold')
Console().print(table)
" "$@"
}
```
Usage:
```bash
count-tokens myfile.txt
count-tokens src/*.py
count-tokens file1.md file2.md file3.md
```
Single file prints just the count. Multiple files renders a [[Rich]] table with per-file tallies and a total.
Uses [[uv]]'s `--with` to run with ad-hoc dependencies — no install needed.