Each table is one machine; the Python ones ran on separate runners, so
compare within a table. Method and full results:
docs/BENCHMARKS.md.
Encodings
Pass an encoding name — not a model name. The bundled encodings are compiled
into the binary, so there are no data files, downloads or runtime dependencies.
TypeSafe's Jev — an upper-bound estimate, not exact ids; see below
An unknown name raises KeyError listing the valid ones.
qwen3.5
Exact ids, checked token for token against Hugging Face tokenizers on 1.1M
single characters, 130k random strings and the three 25 MB benchmark corpora,
and against a bigpickle server's own counts (202 texts, 145k tokens).
It follows bigpickle's tokenizer.json: Qwen3.5's vocabulary and merges with
Qwen2's split pattern. Qwen's own tokenizer.json keeps combining marks with
their letters, so Hindi, Tamil, Thai and similar text counts a few percent
lower there. tools/gen_qwen35.py builds the data from either file.
Text is NFC-normalized first, as Qwen's tokenizer does. Python does it for
you; truncate then returns a prefix of the NFC text. From Rust, pass NFC
text: Tokenizer::unnormalized finds the few texts in a batch that aren't.
A server that renders special tokens counts <|im_start|> or <think> as
one token. Pass with_special=True to match it; the default counts them as
text, which only ever counts more.
Speed: within 10–25% of cl100k_base on English and code, about half on
multilingual web text, and 70–250× faster than tokenizers
(numbers).
hs-decide-1-flash
Hindsight's decision model. Exact ids, checked token for token against Hugging
Face tokenizers with the model's own tokenizer.json.
Text is NFC-normalized first, as the model's tokenizer does. Python does it
for you; truncate then returns a prefix of the NFC text. From Rust, pass NFC
text: Tokenizer::unnormalized finds the few texts in a batch that aren't.
A server that renders special tokens counts <|im_start|> or <think> as
one token. Pass with_special=True to match it; the default counts them as
text, which only ever counts more.
Its vocabulary is byte-identical to qwen3.5's, so the build shares that
table and adds only ~7 KB. Counts still differ: it keeps combining marks with
their letters, so Hindi, Tamil, Thai and similar text count fewer tokens.
tools/gen_qwen35.py <tokenizer.json> hsd1 rebuilds its tables and stops if
a new version's vocabulary ever stops matching.
embeddinggemma-2
Exact ids, checked token for token against Hugging Face tokenizers on every
vocab token, every Unicode character, 400k random token sequences and the three
25 MB benchmark corpora, and against the tokenizer inside Hindsight's
EmbeddingGemma 2 server (311 texts, 290k tokens).
What the model reads is more than your text. The server wraps plain text as
<bos>title: none | text: {text}<eos>, which is 8 + count(" " + text)
tokens; text that already starts with title: or task: goes in as is,
2 + count(text). The model is trained on 8,192 tokens and the server
doesn't cut longer input, so size against that.
The server's usage.prompt_tokens counts words, not tokens. Don't size
requests with it.
Special strings such as <bos> or <|turn> are always read as specials by
the model; pass with_special=True to count them that way.
▁ (U+2581) counts as a space, because that's how Gemma reads it.
truncate still returns a prefix of the text you passed.
Speed: 0.6–0.8× of cl100k_base, and 11–43× faster than tokenizers
(numbers). tools/gen_embeddinggemma2.py
builds the data from the model's tokenizer.json.
jev — an upper bound, not an exact count
TypeSafe doesn't publish Jev's tokenizer. jev gives a safe overestimate
instead, meant for splitting a payload under Jev's context limits:
batch_count, truncate and batch_truncate take it in Python, and
toktok::jev::{count, count_batch, truncate, truncate_batch} in Rust. It is a
count only: there are no ids to encode or decode.
Checked against ~1,570 texts counted by the API (English, code, 22 languages,
JSON, Markdown, logs, CSV, HTML, emoji and odd Unicode; jev-1.13 and 1.14 count
the same):
text
over Jev, on average
English and code
1.19×
other languages
1.19×
JSON, Markdown, logs, CSV, HTML
1.13×
emoji and odd Unicode
1.31×
None came out under. On text the fit hadn't seen, about 1 chunk in 500 came out
under, by less than 8%. Leave that much headroom at the limit, or re-split when
Jev answers max_tokens_exceeded.
It splits text the way Jev does (cl100k's rules with one token per digit), then
charges each piece by type: words by their cl100k tokens, with extra for rare
ones, and other scripts per character. It runs at roughly half to all of plain
cl100k's speed (numbers).
Things to know when you size a request:
The question text and some fixed wrapping count too. One short question on
an empty state already reports 268 input tokens.
A JSON object state counts like json.dumps(state, indent=1). Count that
text, not your compact JSON.
Jev NFC-normalizes text and the estimate doesn't, so decomposed text (e.g.
macOS filenames) only counts higher.
Short texts carry a fixed cost of a few tokens each, so many tiny strings
overcount far more than one payload does.
Python
uv add toktok-rs # or: pip install toktok-rs
One function. Give it your texts and an encoding, get a token count per text:
import toktok
toktok.batch_count(["hello world", "how many tokens is this?"])
# [2, 6]
toktok.batch_count(docs, "o200k_base") # GPT-4o and friends
toktok.batch_count(docs, "cl100k_base", threads=4) # 0 = every core (default)
toktok.batch_count(["a<|endoftext|>b"], with_special=True) # [3]
Returns list[int] — one count per text, in the order given.
parameter
default
what it does
texts
—
any iterable of str. An empty iterable returns []; an empty string counts as 0.
encoding
"cl100k_base"
which tokenizer to count with — one of the encodings. Model names are not accepted; look yours up in that table. An unknown name raises KeyError.
threads
0
worker threads. 0 uses every core; 1 counts on the calling thread; any other number caps the pool. Counting releases the GIL, so this scales — including on free-threaded builds. Threads are only worth it for large batches; for a handful of short strings the pool costs more than it saves.
with_special
False
how to treat special-token strings. False counts <|endoftext|> as the ordinary text it looks like (7 tokens); True counts it as the single special token it is (1). Use True when your text already contains rendered chat/control markup and you want the count the model will see.
Ids are never built: one scratch buffer per thread is reused across texts, so a
batch of any size allocates O(threads), not O(tokens).
Wheels cover CPython 3.11–3.14 plus free-threaded 3.14t, on Linux
(x86_64, aarch64), macOS and Windows.
Cut text to a token budget. Returns (text, total_tokens), where
total_tokens counts the whole input — truncated or not — so you can report how
much was dropped.
One pass, no ids built, nothing decoded — the cut is a byte offset into the
string you passed in. About 3× faster than decode(encode(text)[:n]), and
~6× with the batch form.
It also cuts on a character boundary. Byte-level BPE can split one character
across tokens ("🧠" is three), so a token-boundary cut can leave a partial
character that decodes to U+FFFD — decode(enc.encode("hello 🧠")[:2]) gives
'hello \ufffd'. truncate gives 'hello '.
Need ids, decoding or offsets? toktok._encoding(name) returns the full
tokenizer — see docs/PYTHON.md.
Rust
cargo add toktok-rs # the crate is toktok-rs, the library is toktok
let tok = toktok::Tokenizer::builtin("cl100k_base")?; // nothing to download
let ids = tok.encode(b"hello world");
let text = tok.decode(&ids); // lossless, even on invalid UTF-8
let n = tok.count(b"how many tokens is this?");
// batch APIs are parallel; 0 threads means every core
let docs: Vec<&[u8]> = vec![b"first", b"second"];
let counts = tok.count_batch(&docs, 0, false); // no ids materialized
let batches = tok.encode_batch(&docs, 0, false);
let (ids, spans) = tok.encode_with_offsets(b"per-token byte spans");
// truncation without building ids or decoding: a byte offset back
let t = tok.truncate(text, 8192);
let kept = &text[..t.bytes]; // always a character boundary
t.total_tokens; // the whole input, truncated or not
Every batch method takes the same two knobs as the Python API:
argument
meaning
threads: usize
0 uses every core; 1 runs on the calling thread; anything else caps the pool.
with_special: bool
false counts/encodes special strings as ordinary text; true maps them to their single special id.
Tokenizer is Send + Sync — load it once and share it. Full API on
docs.rs/toktok-rs.
Cargo features
feature
default
what it does
embedded-data
on
compiles the vocabularies into the binary (~13 MB of .rodata), so Tokenizer::builtin() needs no files at runtime. Turn it off (default-features = false) to drop them and load from a directory with Tokenizer::load_dir(dir, name) instead.
More
docs/BENCHMARKS.md — full results, memory and latency profile, how to reproduce
Use together with toktok-rs: extensions, drivers, servers and companion tools.
tokencostPythie’s Index: 26To calculate token and translated USD cost of string and message calls to OpenAI, for example when used by AI agents137.3K downloads/mo