Skip to main content

Measure how efficiently LLM tokenizers handle your language: fertility, UNK rate, and token premium across HuggingFace, tiktoken, and SentencePiece tokenizers

Project description

tokscope

Measure how efficiently LLM tokenizers handle your language.

PyPI version License: MIT

Tokenizers are the invisible tax on every LLM: a language that fragments into 3× more tokens costs 3× more money, fills the context window 3× faster, and is usually understood worse. For low-resource languages the situation can be catastrophic — multilingual BERT tokenizes Sinhala text into 100% [UNK] tokens (it literally cannot see the language).

tokscope makes this measurable in one command: fertility (tokens per word), UNK rate, chars/bytes per token, and the token premium your language pays relative to a parallel English text.

Installation

pip install tokscope            # zero-dependency core
pip install 'tokscope[all]'     # + transformers, tiktoken, sentencepiece backends

CLI

tokscope corpus_si.txt \
    -t xlm-roberta-base \
    -t google/mt5-small \
    -t tiktoken:gpt-4o \
    -t sp:sinhala_bpe.model \
    --reference corpus_en.txt \
    --csv results.csv
Tokenizer                           Vocab     Tokens     Fertility  Chars/tok  UNK rate
---------------------------------------------------------------------------------------
sp:sinhala_bpe_v2.model             32000     41126      1.5801     4.05       0.0007
xlm-roberta-base                    250002    52982      2.0357     3.1437     0.0
tiktoken:gpt-4o                     200019    84752      3.2563     1.9653     0.0
tiktoken:cl100k_base                100277    254974     9.7965     0.6532     0.0

(Real measurements on OSCAR Sinhala — full results in the study.)

Tokenizer specs: hf:NAME (default), tiktoken:MODEL, sp:PATH, and ws: (whitespace baseline — fertility exactly 1.0 by construction).

Python API

import tokscope

report = tokscope.analyze(text, "tiktoken:gpt-4o", reference_text=english_text)
report.fertility        # tokens per word
report.unk_rate         # fraction of tokens the tokenizer can't represent
report.premium          # token cost multiplier vs the reference text

reports = tokscope.compare(text, ["xlm-roberta-base", "tiktoken:gpt-4o", "ws:"])

Custom tokenizers: pass any object with a name, a vocab_size, and a tokenize(text) -> (tokens, unk_count) method.

Metrics

Metric Definition Why it matters
fertility tokens ÷ whitespace words The standard measure of tokenizer fit; higher = more fragmentation
chars_per_token characters ÷ tokens How much text each token carries
bytes_per_token UTF-8 bytes ÷ tokens Script-independent capacity measure
unk_rate UNK tokens ÷ tokens Any nonzero value = the tokenizer cannot represent the text
premium tokens(text) ÷ tokens(reference) The cost multiplier your language pays for the same content

Why this exists

Built while studying how tokenizers treat Sinhala (~20M speakers). The headline findings from the full study: Sinhala pays a 2.26× token premium vs English for the same content on GPT-4o (7.37× on the original GPT-4 tokenizer); a 32k-vocab SentencePiece BPE trained on OSCAR Sinhala by one student beats 200k–250k multilingual vocabularies by 1.3–2×; and mBERT silently replaces 62% of Sinhala tokens with [UNK]. If you work on a low-resource language, measuring this is step one of fixing it.

License

MIT — see LICENSE.

Author

Ravindu Pabasara Karunarathna — also the author of sinhaladate and slnic.

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

tokscope-0.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

tokscope-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file tokscope-0.1.0.tar.gz.

File metadata

  • Download URL: tokscope-0.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for tokscope-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dd141ccd1d31d96dd661c5d9b997630f2192da56311dbce2fdb47084237396db
MD5 0f83f3a1c5487994d79847d4e4c27307
BLAKE2b-256 22f72676adaac144985a55a5c5e913b29726f080f0cd78b182d70b2c9453354b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokscope-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for tokscope-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a36f42d57e3e28fa254b6ffdc3154822677d0f912c7800bbbd555af205359b3d
MD5 9c2491658ad3479b1d99d037a17fb978
BLAKE2b-256 73b35d2d809d87169af3fba8cd0d947bba705ed612c107ac676fe058af766219

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