Skip to main content

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, byte-token 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.byte_token_rate  # fraction of tokens that are raw bytes, not characters
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. To report byte tokens too, add tokenize_with_bytes(text) -> (tokens, unk_count, byte_token_count).

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
byte_token_rate byte tokens ÷ tokens Byte-fallback pieces (<0xE0>) or tokens holding part of a character. These tokenizers never emit UNK, so this is how you see that they barely know the script
premium tokens(text) ÷ tokens(reference) The cost multiplier your language pays for the same content

The UNK rate alone can be misleading. On the same Sinhala paragraph:

Tokenizer Fertility UNK rate Byte rate
tiktoken:gpt-4o 3.55 0.00 0.00
tiktoken:cl100k_base 11.75 0.00 0.99
gpt2-medium 17.00 0.00 0.94
bert-base-multilingual-cased 1.10 0.91 0.00

cl100k_base reports no unknown tokens, yet 99% of its tokens are fragments of Sinhala letters: it has almost no Sinhala vocabulary. mBERT looks efficient (fertility 1.10) only because it replaces nearly every word with a single [UNK].

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.

Metadata

Release files for tokscope 0.2.0

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

Source distribution (sdist)

Source distribution for tokscope 0.2.0
File Size Uploaded Provenance Actions
tokscope-0.2.0.tar.gz 14.7 kB Without provenance Details for tokscope-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 26.3 kB

Release files / tokscope-0.2.0.tar.gz

Download URL tokscope-0.2.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c2888b64a28e1d7f848d4d008b7e6d6522f392f5b04f7421263ab23d8a7fcd7c
BLAKE2b-256 checksum
How to use checksums
16244b939ee576793685662f78df14ab00e9eb89542ed93defe11ecd33fd6514
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Release files / tokscope-0.2.0-py3-none-any.whl

Download URL tokscope-0.2.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
083c7b42d03e7010b9db53d84a93fba863136ce04a118fd6b4d1a8ddb5fa8464
BLAKE2b-256 checksum
How to use checksums
44f0ecbe2755fa2bde15659293a8315483fcd597e62e050ab2ab43f902965f2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.2

Security for tokscope 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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