Getting Started
Installation
Using pip
Using uv
From source
CLI Usage
The count-tokens CLI provides a simple way to count tokens in files.
Basic usage
Available options
Specifying encoding
Available encodings:
- cl100k_base - Used by GPT-4 and GPT-3.5-turbo
- p50k_base - Used by Codex models
- r50k_base - Used by GPT-3 models
Python API Usage
Count tokens in a file
from count_tokens import count_tokens_in_file
num_tokens = count_tokens_in_file("myfile.txt")
print(f"File contains {num_tokens} tokens")
Count tokens in a string
from count_tokens import count_tokens_in_string
text = "Hello, world! This is a test."
num_tokens = count_tokens_in_string(text)
print(f"String contains {num_tokens} tokens")