A command-line utility for counting tokens in text files using various tokenization models. This tool helps estimate token usage for large language models (LLMs) like GPT-3.5 and GPT-4.
- Count tokens in individual files
- Count tokens in entire directories
- Support for different tokenization models
- Respect .gitignore rules to skip ignored files
- Detailed reports with token counts by directory and file
- Skip binary files and common non-text formats
- Filter files by minimum token count
- Download the latest release for your platform from the Releases page
- Extract the binary (if compressed)
- Make it executable (on Unix-like systems):
chmod +x token-counter-*
- Optionally move it to a directory in your PATH and rename it to just
token-counter
- If you're on MacOS and you try running this, you'll get an error saying the app isn't sign. You can quarantine the app with
xattr -d com.apple.quarantine /usr/local/bin/token-counter
and then run it again. Or you can follow the instructions below to build the binary yourself.
- Ensure Go is installed on your system (version 1.16 or later recommended)
- Clone this repository
- Build the binary:
go build -o token-counter
Basic usage:
./token-counter [options] [path]
If no path is provided, the current directory will be analyzed.
➜ token-counter git:(main) token-counter
Processing directory: /code/token-counter
Respecting .gitignore rules if present
Token Count Summary for: /code/token-counter
Total tokens in repository: 5510
Directories (sorted by token count):
----------------------------------
/code/token-counter: 4561 tokens
|- main.go: 2596 tokens
|- README.md: 1028 tokens
|- go.sum: 858 tokens
|- go.mod: 79 tokens
/code/token-counter/tests: 949 tokens
|- tests/lorem-ipsum.txt: 949 tokens
Flag | Default | Description |
---|---|---|
-path |
current directory | Path to the directory or file to analyze |
-model |
cl100k_base | Token counting model to use (e.g., cl100k_base for GPT-4) |
-gitignore |
true | Whether to respect .gitignore rules |
-files |
true | Whether to show individual file details |
-min |
0 | Minimum token count for a file to be included |
-no-hidden |
true | Whether to ignore hidden files and directories (starting with .) |
-file |
false | Explicitly treat the path as a single file rather than a directory |
Count tokens in the current directory:
./token-counter
Count tokens in a specific directory:
./token-counter /path/to/project
Count tokens in a single file:
./token-counter /path/to/file.txt
Explicitly specify that the path is a file:
./token-counter -file -path /path/to/file.txt
Count tokens using a different model:
./token-counter -model r50k_base
Only show files with at least 100 tokens:
./token-counter -min 100
Show summary without file details:
./token-counter -files=false
Include hidden files and directories:
./token-counter -no-hidden=false
cl100k_base
- Used by GPT-4 and GPT-3.5-Turbop50k_base
- Used by GPT-3 models like text-davinci-003r50k_base
- Used by older GPT-3 models
The tool provides a summary of token usage:
For directories:
- Total token count for the repository
- Token count by directory (sorted by token count)
- Token count by file within each directory (if -files=true)
For single files:
- Total token count for the file
MIT License
Copyright (c) 2023
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.