Skip to main content

Tokentik

A lightweight Python utility for estimating token counts in text, specifically optimized for modern LLMs using the o200k_base encoding.

Installation

pip install tokentik

Usage

from tokentik import count_tokens

text = "Hello, world!"

# Estimate tokens using the default 'o200k_base' encoding (GPT-4o)
token_count = count_tokens(text)
print(f"Token count: {token_count}")

# Specify a different encoding model (e.g., 'cl100k_base' for GPT-4/GPT-3.5)
token_count_v2 = count_tokens(text, model="cl100k_base")
print(f"Token count (cl100k_base): {token_count_v2}")

Configuration

Environment Variables

tiktoken needs to download and cache the BPE (Byte Pair Encoding) vocabulary files. By default, it uses a temporary directory. To specify a persistent location for these files, set the TIKTOKEN_CACHE_DIR environment variable:

export TIKTOKEN_CACHE_DIR="/path/to/your/models/tiktoken"

This is highly recommended for production environments or Cloud Run environments where the storage might be mounted (e.g., at /mnt/models/tiktoken).

Acknowledgments

Special thanks to OpenAI for their tiktoken library, which this utility is built upon.

License

MIT

Metadata

Release files for tokentik 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokentik 0.1.1
File Size Uploaded
tokentik-0.1.1.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokentik 0.1.1
File Interpreter ABI Platform
tokentik-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.3 kB

Release files / tokentik-0.1.1.tar.gz

Download URL tokentik-0.1.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
046d945b10dbe2aba6de128bd6cdc36b084a860f0b3db7265d74df3a9aac8703
BLAKE2b-256 checksum
How to use checksums
1e5e4038ddc8415cf9a8c636275b4e4e8f5e501763182e22c2ac02d894711780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release files / tokentik-0.1.1-py3-none-any.whl

Download URL tokentik-0.1.1-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb37c1ec51ca739f207de548cfd6b35e7ca53f85eb4b60aac24742a16b4004b3
BLAKE2b-256 checksum
How to use checksums
a52605823d3af02fda51fba744b54e3810edb7d855251fffe1dd212b7752d354
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page