Skip to main content

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-002
  • o200k_base: Used by GPT-4o models
  • p50k_base: Used by code models like text-davinci-002/003
  • p50k_edit: Used by edit models
  • r50k_base/gpt2: Used by GPT-3 and GPT-2 models

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokens_cli-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokens_cli-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

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

Hashes for tokens_cli-0.1.0.tar.gz
Algorithm Hash digest
SHA256 67752b3c32cf9120fc24d4d973245145954fe71e43525d4b3175e1c68d8a9ba3
MD5 9c002f5e4f0f5ad29d81a5368e08d65d
BLAKE2b-256 177197a49f6436a2a031f55537f6197ecaae1d9caf675df1412ddd5f4b1a84bc

See more details on using hashes here.

File details

Details for the file tokens_cli-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tokens_cli-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae60b400329c0afeb47dd589f66ebd02c9da6a4a9d58dac041233a232df9cb97
MD5 ecc4a5eefac438eb0bd0d6624b63b052
BLAKE2b-256 9a8ad91722d7461d1474e24535eea07d7cd55b99e5c0ae1ffb5e5c4b5b4a2821

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page