Skip to main content

toktok

A fast, exact BPE tokenizer for OpenAI encodings — Rust core, Python bindings. Token ids are byte-identical to tiktoken.

The Pile, 25 MB, cl100k_base, single thread, GitHub runners. Ids verified identical before timing.

Python 3.11

encoder throughput CPU per MB p99 per doc
toktok 211.8 MB/s 0.0054 s 17.4 µs 15.4×
tiktoken 13.8 MB/s 0.0752 s 187.3 µs 1×

Python 3.14

encoder throughput CPU per MB p99 per doc
toktok 187.0 MB/s 0.0058 s 21.4 µs 16.6×
tiktoken 11.3 MB/s 0.0931 s 235.6 µs 1×

Python 3.14t · free-threaded

encoder throughput CPU per MB p99 per doc
toktok 174.7 MB/s 0.0064 s 21.0 µs 15.2×
tiktoken 11.5 MB/s 0.0901 s 227.8 µs 1×

Rust

encoder throughput CPU per MB p99 per doc
toktok 249.8 MB/s 0.0040 s 4.1 µs 26.6×
bpe-openai 36.4 MB/s 0.0274 s 32.4 µs 3.9×
tiktoken-rs 9.4 MB/s 0.1066 s 132.4 µs 1×

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.

encoding used by
cl100k_base GPT-3.5, GPT-4, GPT-4 Turbo, text-embedding-3-*, text-embedding-ada-002
o200k_base GPT-4o, GPT-4o mini, GPT-4.1, GPT-5, o1 / o3 / o4-mini
o200k_harmony GPT-OSS (o200k_base's merge ranks plus the harmony special tokens)
qwen3.5 Qwen3.5's vocabulary, split the way bigpickle does — exact ids; see below
jev 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).

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]

batch_count(texts, encoding="cl100k_base", threads=0, with_special=False)

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.

truncate(text, max_tokens, encoding="cl100k_base")

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.

toktok.truncate("hello world", 1)          # ('hello', 2)
toktok.truncate("hello world", 50)         # ('hello world', 2) — same object back

toktok.batch_truncate(docs, 8192, "o200k_base", threads=8)   # [(text, total), ...]

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 (~4 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

License

MIT — see LICENSE. Ported from quicktok (MIT); vocabulary data derives from tiktoken (MIT). See NOTICE.

Metadata

Release files for toktok-rs 0.3.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 toktok-rs 0.3.0
File Size Uploaded Provenance Actions
toktok_rs-0.3.0.tar.gz 4.2 MB With provenance Details for toktok_rs-0.3.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for toktok-rs 0.3.0
File Actions
toktok_rs-0.3.0-cp314-cp314t-win_amd64.whl CPython 3.14 CPython 3.14 free-threading Windows x86-64 Details for toktok_rs-0.3.0-cp314-cp314t-win_amd64.whl
toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64 Details for toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_x86_64.whl
toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ ARM64 Details for toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_aarch64.whl
toktok_rs-0.3.0-cp314-cp314t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl CPython 3.14 CPython 3.14 free-threading macOS 10.12+ x86-64, macOS 11.0+ ARM64, macOS 10.12+ universal2 (ARM64, x86-64) Details for toktok_rs-0.3.0-cp314-cp314t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
toktok_rs-0.3.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for toktok_rs-0.3.0-cp311-abi3-win_amd64.whl
toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
toktok_rs-0.3.0-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl CPython 3.11 abi3 macOS 10.12+ x86-64, macOS 11.0+ ARM64, macOS 10.12+ universal2 (ARM64, x86-64) Details for toktok_rs-0.3.0-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Total release size: 49.0 MB

Release files / toktok_rs-0.3.0.tar.gz

Download URL toktok_rs-0.3.0.tar.gz
Size 4.2 MB
Tags Source
SHA-256 checksum
How to use checksums
a90106c50802f94b885416b667bde2db23f0778f7ea602a040bd6eb015a89b9c
BLAKE2b-256 checksum
How to use checksums
b917e8c5745ebc307b2c2e65ccd9661cbdefab122a32cb0eed63a6aee6eba830
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp314-cp314t-win_amd64.whl

Download URL toktok_rs-0.3.0-cp314-cp314t-win_amd64.whl
Size 4.4 MB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
52b5d25968cfff9dddffcf3e83b9e82c6f18469dc594cb969cc622b14a18c71c
BLAKE2b-256 checksum
How to use checksums
691a6cec393a26eab542259cd1440f0f4ae4d9ca5ba373260ee36df40ca0030c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_x86_64.whl

Download URL toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_x86_64.whl
Size 4.5 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
1692b47c42fe4bc9a75f233d29a4c910efad964330f0e3847f1b9d645e261005
BLAKE2b-256 checksum
How to use checksums
e5ac3960522030d5cc169d6e5760b5e7bd2cdb3494b775cba96d9268ebd2a3a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_aarch64.whl

Download URL toktok_rs-0.3.0-cp314-cp314t-manylinux_2_28_aarch64.whl
Size 4.5 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
c447e6088bba129dcaa1b05ad07b2038ead282ea0c73f9f61099ca76e33c661d
BLAKE2b-256 checksum
How to use checksums
d0166c61618847c8d72f52f345d7df3233d444c6b29b24b5612469c1764c1482
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp314-cp314t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL toktok_rs-0.3.0-cp314-cp314t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 9.0 MB
Tags CPython 3.14 CPython 3.14 free-threading macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b7cb192eb1004de75d4be8a5a12795eca76b77ab98b589b7815e7f2360bd94a4
BLAKE2b-256 checksum
How to use checksums
524a41ccacbf7d820482a3593c0d639c81187c0b63159f66246dd21a8c2793e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp311-abi3-win_amd64.whl

Download URL toktok_rs-0.3.0-cp311-abi3-win_amd64.whl
Size 4.4 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
edaf13a23cba0e7841fdc2eb8fa1b1b6870c19862c8f95f2370cd82c87aa4a15
BLAKE2b-256 checksum
How to use checksums
555f3c458029467d8f2f6c2a89d05dac1d725989390f0c20ebf2e2a71eb8c239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 4.5 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
dc23f9129dfaf91ccfe3f7db3920a1ed0a1c9ce2259f233cf5bb473d1822de98
BLAKE2b-256 checksum
How to use checksums
d79319d9928e709d10f540c8182677eda18a972d7b247d7e5cc483dcd41649ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL toktok_rs-0.3.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 4.5 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
5d9076a3d2169ae314dfef8d75661a0335f29d4f4fb2269ab28e52e9ba3c2c6f
BLAKE2b-256 checksum
How to use checksums
81d01cbd18bcd7f446a8e6d5bcf7745298074b485b4d6f73c0463a6077acf305
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / toktok_rs-0.3.0-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl

Download URL toktok_rs-0.3.0-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 9.0 MB
Tags CPython 3.11 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f351bd245076df724ea62c8e5c2454cfe6dbb1ec258a1a0800a22ef6a4b5e6da
BLAKE2b-256 checksum
How to use checksums
4bc22d983ddb0b41471842f1c7ea060cd9febfbf7a368259395e15585d12f30a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for toktok-rs 0.3.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.

Full provenance

All 9 files in this release have been uploaded with provenance metadata.

Select from 9 files

Attestation for toktok_rs-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

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.

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