A command-line interface for counting tokens using OpenAI's tiktoken tokenizer, powered by tiktoken-go.
- Count tokens in text using various OpenAI tokenization encodings
- Encode text to token IDs
- Decode token IDs back to text
- Support for all major OpenAI models (GPT-4o, GPT-4, GPT-3.5-turbo, etc.)
- Read from stdin for piping
- Cross-platform (Linux, macOS, Windows)
Download the latest release from the releases page.
go install github.com/alexgorbatchev/tiktoken-go-cli@latestOr clone and build:
git clone https://github.com/alexgorbatchev/tiktoken-go-cli.git
cd tiktoken-go-cli
go build -o tiktokenCount is the default action - no subcommand needed. Uses cl100k_base encoding by default (GPT-4, GPT-3.5-turbo).
# Count tokens (uses cl100k_base encoding by default)
tiktoken "Hello, world!"
# Count tokens from stdin
echo "Hello, world!" | tiktoken
# Count tokens for a specific model
tiktoken -m gpt-4o "Hello, world!"
# Count tokens using a specific encoding
tiktoken -e o200k_base "Hello, world!"
# Count tokens from a file
cat myfile.txt | tiktoken
# Using explicit count subcommand (also works)
tiktoken count "Hello, world!"# Encode text to token IDs
tiktoken encode "Hello, world!"
# Output: 9906 11 1917 0
# Encode using a specific model
tiktoken encode -m gpt-4o "Hello, world!"# Decode token IDs back to text
tiktoken decode 15339 1917 0
# Decode from stdin
echo "15339 1917 0" | tiktoken decode
# Chain encode and decode
tiktoken encode "Hello, world!" | tiktoken decode# Show available models and encodings
tiktoken modelstiktoken version| Encoding | Models |
|---|---|
o200k_base |
gpt-4o, gpt-4.1, gpt-4.5 |
cl100k_base |
gpt-4, gpt-3.5-turbo, text-embedding-ada-002, text-embedding-3-* |
p50k_base |
Codex models, text-davinci-002, text-davinci-003 |
r50k_base |
GPT-3 models (davinci, curie, babbage, ada) |
| Flag | Short | Description |
|---|---|---|
--model |
-m |
OpenAI model name (e.g., gpt-4o, gpt-4) |
--encoding |
-e |
Encoding name (default: cl100k_base) |
--help |
-h |
Show help for command |
cat README.md | tiktoken countTOKEN_COUNT=$(echo "Your text here" | tiktoken count)
echo "Token count: $TOKEN_COUNT"TEXT="Hello, world!"
echo "o200k_base: $(tiktoken count -e o200k_base "$TEXT")"
echo "cl100k_base: $(tiktoken count -e cl100k_base "$TEXT")"
echo "p50k_base: $(tiktoken count -e p50k_base "$TEXT")"Run the integration tests:
./test.shMIT License - see LICENSE file for details.
- tiktoken-go - Go implementation of tiktoken
- tiktoken - Original Python library by OpenAI