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 85.1 MB/s 0.0117 s 34.8 µs 7.4×
tiktoken 11.5 MB/s 0.0873 s 226.8 µs 1×

Python 3.14

encoder throughput CPU per MB p99 per doc
toktok 68.6 MB/s 0.0146 s 37.5 µs 5.7×
tiktoken 12.0 MB/s 0.0832 s 208.6 µs 1×

Python 3.14t · free-threaded

encoder throughput CPU per MB p99 per doc
toktok 94.5 MB/s 0.0106 s 35.1 µs 6.8×
tiktoken 14.0 MB/s 0.0716 s 188.8 µs 1×

Rust

encoder throughput CPU per MB p99 per doc
toktok 89.7 MB/s 0.0111 s 13.4 µs 12.3×
bpe-openai 30.3 MB/s 0.0330 s 38.4 µs 4.2×
tiktoken-rs 7.3 MB/s 0.1369 s 169.9 µ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 three 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)

An unknown name raises KeyError listing the valid ones.

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 three 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 2× faster than decode(encode(text)[:n]), and ~7× 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.1.3

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.1.3
File Size Uploaded
toktok_rs-0.1.3.tar.gz 2.2 MB Details

Built distributions (wheels)

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

Total release size: 27.5 MB

Release files / toktok_rs-0.1.3.tar.gz

Download URL toktok_rs-0.1.3.tar.gz
Size 2.2 MB
Tags Source
SHA-256 checksum
How to use checksums
8d9d24d641598548e0b073aee76287b7b57ba634c57e374a134a188a072a2d65
BLAKE2b-256 checksum
How to use checksums
a307dcaafca2cb017d8d0a2e24061bacfd08dcc6834e08a875683b08b536c12f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp314-cp314t-win_amd64.whl
Size 2.4 MB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
9eb7d55859b4e1aea56f19dd37c61348cb59a89a979c4581bfb2475c28335dcc
BLAKE2b-256 checksum
How to use checksums
67db69cce4d0109beac71512b71d7393275d05c559e601e859093a84061bc70b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp314-cp314t-manylinux_2_28_x86_64.whl
Size 2.6 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
66788b50607a3eccd719b38d9721de3bd90ce48aa5e8dd76030beb431665ba58
BLAKE2b-256 checksum
How to use checksums
e5193681a816e00c11c43e693b31c433210eb7240cfc35e50b9d48a2675ba07c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp314-cp314t-manylinux_2_28_aarch64.whl
Size 2.5 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
68724f3b31ddf63444469b5a5bc85fa84a31978b35385b00c5f7b5ca346eae44
BLAKE2b-256 checksum
How to use checksums
9eecdc398e069b6405cffd4957571976232208ead877151029de0300bd342972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp314-cp314t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 5.1 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
feb5c18284d272f2f206410a5e4a164fd590af622a3944330fc1439122bbe96d
BLAKE2b-256 checksum
How to use checksums
9b8549193f425d0b7471681daf2b325cd88d3dbf437ca3d1758c22e7166ae4f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp311-abi3-win_amd64.whl
Size 2.4 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
09a9eb02f2ea60b27f194d7ac54e120e06cdea683fa1faca67fb37d7e86add39
BLAKE2b-256 checksum
How to use checksums
a918326733a61b4fc81b498fef65d25364579c2b9c72ea8063c719da128494fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
0499998a9b9e680f1adc4f029cddadccd39740317f0a6133939d4032004125b6
BLAKE2b-256 checksum
How to use checksums
9ad7d833fd95fec1bc739b293da9bcca52c718e81b1a0ce613728044032664fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
826421b3d58445de7d79988186e9b4fc02029ab07af7d1e465b74a44c9375d6f
BLAKE2b-256 checksum
How to use checksums
189bb38fd8f0c2ce10f6e6e1615dff55637163085d72b7796bce9380ac294a9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

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

Download URL toktok_rs-0.1.3-cp311-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Size 5.1 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
80567ddfbc8575842f62c0b6eb8041546749cfbd2baabe1d21d80225be0f72c2
BLAKE2b-256 checksum
How to use checksums
b5708f9707302fa7a925566f371ab09fe58c60b0776a29bd5b730908b1e0b486
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 2, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.4

9 release files

This release

0.1.3 This release

9 release files

0.1.2

9 release files

0.1.1

9 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