Count tokens in text using tiktoken encoders
Project description
Tokens CLI
A simple command-line tool to count tokens in text using OpenAI's tiktoken tokenizers.
Why?
When working with LLMs and token-based API pricing, it's useful to quickly see how many tokens your text uses. This tool makes it easy to:
- Check token counts for prompt engineering
- Calculate costs for API requests
- Verify tokenization is as expected
Installation
From PyPI:
pip install tokens-cli
# or with uv
uv pip install tokens-cli
For development:
# Clone the repository
git clone https://github.com/nikdavis/tokens_cli.git
cd tokens_cli
# Install locally
uv tool install .
Usage
The tokens command (or its shorter alias tks) supports piping input from other commands:
# Basic usage
echo "Count how many tokens are in this text" | tokens
# or with the shorter alias
echo "Count how many tokens are in this text" | tks
# Verbose output with token count details
echo "Count tokens with detailed output" | tokens -v
# Specify model for tokenization
echo "Count tokens using gpt-4 tokenizer" | tokens -m gpt-4
# Specify encoding directly
echo "Count tokens using cl100k_base encoding" | tokens -e cl100k_base
# List available models and encodings
tokens -l
Running Tests
Install development dependencies:
uv pip install -e ".[dev]"
Run tests:
pytest
# or
uv run pytest
Supported Models/Encodings
cl100k_base: Used by ChatGPT, GPT-4, text-embedding-ada-002o200k_base: Used by GPT-4o modelsp50k_base: Used by code models like text-davinci-002/003p50k_edit: Used by edit modelsr50k_base/gpt2: Used by GPT-3 and GPT-2 models
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tokens_cli-0.1.0.tar.gz.
File metadata
- Download URL: tokens_cli-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67752b3c32cf9120fc24d4d973245145954fe71e43525d4b3175e1c68d8a9ba3
|
|
| MD5 |
9c002f5e4f0f5ad29d81a5368e08d65d
|
|
| BLAKE2b-256 |
177197a49f6436a2a031f55537f6197ecaae1d9caf675df1412ddd5f4b1a84bc
|
File details
Details for the file tokens_cli-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tokens_cli-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae60b400329c0afeb47dd589f66ebd02c9da6a4a9d58dac041233a232df9cb97
|
|
| MD5 |
ecc4a5eefac438eb0bd0d6624b63b052
|
|
| BLAKE2b-256 |
9a8ad91722d7461d1474e24535eea07d7cd55b99e5c0ae1ffb5e5c4b5b4a2821
|