Skip to main content

tuetoken

The fastest tokenizer for modern LLMs, up to 30x faster.

tuetoken is a BPE tokenizer with a fast, safe Rust core. It is a drop-in replacement for 🤗 transformers.AutoTokenizer: it loads any model's own tokenizer.json and reproduces tokenization exactly (special tokens, chat templates, padding/truncation), up to 30x faster. It also loads OpenAI/tiktoken encodings natively, and its O(n) merger stays fast even on adversarial inputs (hashes, base64, minified code) where other tokenizers degrade to O(n²).

from tuetoken import AutoTokenizer

tok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
out = tok.apply_chat_template(messages, add_generation_prompt=True)   # {"input_ids", "attention_mask"}

Detection is 100% config-driven (the model's tokenizer.json, never its name), so the same code works across families: Llama, Qwen, Mistral/Mixtral, DeepSeek, Gemma, Phi, GPT-OSS, GLM, Kimi, and more.

Install

pip install tuetoken

Build from source (development, or a platform with no prebuilt wheel):

git clone https://github.com/tuetoken-org/tuetoken && cd tuetoken
pip install maturin
maturin develop --release

Performances

Performances

Drop-in AutoTokenizer

The full 🤗 API, byte-exact with transformers.AutoTokenizer:

from tuetoken import AutoTokenizer
tok = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")

tok.encode("Hello <|eot_id|> world")                 # special-token aware -> list[int]
tok.decode(ids, skip_special_tokens=True)            # -> str
tok(texts, padding=True, truncation=True,            # batch dict: input_ids + attention_mask
    max_length=512, return_tensors="np")             #   (also "pt" for torch)
tok.apply_chat_template(messages, add_generation_prompt=True)  # -> {input_ids, attention_mask}
tok.apply_chat_template(messages, add_generation_prompt=True, return_dict=False)  # -> list[int]
tok.batch_decode(...) ; tok.convert_ids_to_tokens(...) ; tok.tokenize(...)
tok.bos_token_id ; tok.eos_token ; tok.pad_token_id ; tok.vocab_size

This matches transformers.AutoTokenizer token-for-token across byte-level models (Llama, Qwen, DeepSeek, …) and SentencePiece models (Mistral, Phi-3, CodeLlama, …).

OpenAI / tiktoken encodings

tuetoken loads OpenAI's encodings natively, with no tiktoken dependency, and is faster than tiktoken itself, by up to an order of magnitude on long inputs:

from tuetoken import Tokenizer
enc = Tokenizer.from_tiktoken("cl100k_base")   # also "o200k_base", "gpt2", ...
enc.encode_ordinary("Hello world")             # list[int]

Lower-level core

Tokenizer is the raw BPE engine (no special tokens or chat templates; that is what AutoTokenizer is for). Reach for it when you only need fast token ids or counts:

from tuetoken import Tokenizer
enc = Tokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")   # or Tokenizer("tokenizer.json")

enc.encode_ordinary("Hello world")                # list[int]
enc.encode_ordinary_batch(texts, num_threads=0)   # parallel (0 = all cores), GIL released
enc.decode(ids) ; enc.count_tokens("Hi") ; len(enc)

For ML pipelines there are zero-copy numpy paths, a padded training-batch helper, and byte-span offsets:

import numpy as np
arr   = np.frombuffer(enc.encode_to_bytes(text), dtype=np.uint32)   # skip per-token boxing
text  = enc.decode_array(arr)
batch = enc.encode_batch(texts, max_length=512, pad_id=0)           # input_ids + attention_mask
ids, offsets = enc.encode_with_offsets("Hello café")                # byte spans, ByteLevel only

Tokenizing a whole corpus: pass bytes straight from disk (no UTF-8 conversion in Python) and get one flat array back, built in parallel and handed to numpy without a copy:

data = open("corpus.txt", "rb").read()
docs = data.split(b"<|endoftext|>")
out  = enc.encode_ordinary_to_buffer(docs)          # or [data] for one big document
ids, offsets = out["data"], out["offsets"]           # doc i = ids[offsets[i]:offsets[i+1]]

Every chunk is resolved with one probe into a table shared by all threads: it starts with the vocabulary's own chunks, and anything else is merged once and added (grows on demand, 128 MB cap by default; longer results go to a side map of up to a quarter of that): enc.set_cache_limit_mb(512), enc.cache_limit_mb, enc.learned_entries.

Coverage

tuetoken works with essentially every modern LLM tokenizer: ByteLevel BPE (Llama, Qwen, DeepSeek, Mistral/Mixtral, GPT-OSS, GLM, Phi, OLMo, Yi, …), SentencePiece (Llama-2, Mistral, Phi-3, CodeLlama, Gemma, …), and OpenAI/tiktoken encodings. We extend coverage constantly; if a tokenizer you need isn't supported yet, please open an issue.

Anything tuetoken can't reproduce exactly fails closed (raises at load) rather than mistokenizing, so you never get silently wrong tokens.

Linear-time on any input

Classic BPE is O(n²) per chunk and collapses on long, poorly-merging content (random identifiers, hashes, base64, minified code), tiktoken included. tuetoken's merger is O(n), so adversarial inputs that hang other tokenizers for minutes stay in the millisecond range, byte-identical.

Correctness

Every claim above is validated byte-exact against the reference tokenizers (transformers, tokenizers, tiktoken) on a large, adversarial corpus. That is the only reason those libraries appear in the test dependencies; they are not runtime dependencies of tuetoken.

Metadata

Release files for tuetoken 0.1.6

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

Source distribution (sdist)

Source distribution for tuetoken 0.1.6
File Size Uploaded Provenance Actions
tuetoken-0.1.6.tar.gz 1.1 MB With provenance Details for tuetoken-0.1.6.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for tuetoken 0.1.6
File Actions
tuetoken-0.1.6-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for tuetoken-0.1.6-cp313-cp313-win_amd64.whl
tuetoken-0.1.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details for tuetoken-0.1.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.6-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for tuetoken-0.1.6-cp313-cp313-macosx_11_0_arm64.whl
tuetoken-0.1.6-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for tuetoken-0.1.6-cp312-cp312-win_amd64.whl
tuetoken-0.1.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details for tuetoken-0.1.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.6-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for tuetoken-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
tuetoken-0.1.6-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for tuetoken-0.1.6-cp311-cp311-win_amd64.whl
tuetoken-0.1.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details for tuetoken-0.1.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.6-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for tuetoken-0.1.6-cp311-cp311-macosx_11_0_arm64.whl
tuetoken-0.1.6-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for tuetoken-0.1.6-cp310-cp310-win_amd64.whl
tuetoken-0.1.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details for tuetoken-0.1.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.6-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for tuetoken-0.1.6-cp310-cp310-macosx_11_0_arm64.whl

Total release size: 14.9 MB

Release files / tuetoken-0.1.6.tar.gz

Download URL tuetoken-0.1.6.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
0a7eb929be56333527b3e5d196cf8880778dfed1daa3ab6d61988226adf3fe4a
BLAKE2b-256 checksum
How to use checksums
b313b68323df17db5ff5cdfa428eb815db5b4401f1cf7476a3419d7211a4b70a
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp313-cp313-win_amd64.whl

Download URL tuetoken-0.1.6-cp313-cp313-win_amd64.whl
Size 1.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
8ce4f95af4b6bdfd1734a88d4b24aa98774c27e0beb5bdc6826ed58e936fad7c
BLAKE2b-256 checksum
How to use checksums
7526a6ebefcc90fb27c25bd7f50be1855735718d40d889b0e353e6f644fe8a41
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.6-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.2 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7d313547b7992df3aeaa4a41682b2f930694d6077a6e03f724e2641a41daafb0
BLAKE2b-256 checksum
How to use checksums
7c86edc5788ac07dee94c144793aadd76d6b107670443fb9bdadae01030ff689
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp313-cp313-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.6-cp313-cp313-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d931b5e956d12c1b542a3a34db56ded4a1a6c73b837d3e47059843c0dc70060d
BLAKE2b-256 checksum
How to use checksums
95b164612559b2007bb1b531eed0427259e2b62be951094fb38ccdf884a567e8
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp312-cp312-win_amd64.whl

Download URL tuetoken-0.1.6-cp312-cp312-win_amd64.whl
Size 1.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
684d63cda1a400f0b51c006e05e9fae316d475e71638a001154f70ee45c03551
BLAKE2b-256 checksum
How to use checksums
4568a88af1a7590e94f7de94e9223367849545404fcf49b37472e78a7dbfe827
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.6-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.2 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c94d23121661261548f6f09a3ffcccfb419fc7ce4dd40a9c81db999d6663d5c3
BLAKE2b-256 checksum
How to use checksums
17679b5b7b561ce0ae960ce4cc3a2e62371d495c84dd04703625e847a906090f
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp312-cp312-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
63e8ea92ae576d875cc7a78234d8efab74ee0f055039e25a250d7b248bb61319
BLAKE2b-256 checksum
How to use checksums
e528a3a824e377791be91a553088e8eee9aa9632eb7a5cd878e112fa08162e97
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp311-cp311-win_amd64.whl

Download URL tuetoken-0.1.6-cp311-cp311-win_amd64.whl
Size 1.1 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
ea893fdfdc482ead5f7d315d5054d9cc55878edfe9bad807335a669a0d6ec572
BLAKE2b-256 checksum
How to use checksums
ba2b9d4dec423ea2943922bd75245c98053493ddb4080eef5a3e3aff66aa3e42
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.6-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.2 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
cf5e8f0571767f4f21f05e3f7987c29984581305c9749eefef69e5c557754546
BLAKE2b-256 checksum
How to use checksums
b03c6cb7fe33daf7946e4b1b34157dbeddecda6e9e15af43ff8f35c428709712
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp311-cp311-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.6-cp311-cp311-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f9f2fb4f7134fa1c00b190f70cd76640fac0cc941992d7b28948bbc97178a1fd
BLAKE2b-256 checksum
How to use checksums
5c47efeb7dbf32413c51c175d80c8154cdc0215369e4dac509f19c6d6f708202
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp310-cp310-win_amd64.whl

Download URL tuetoken-0.1.6-cp310-cp310-win_amd64.whl
Size 1.2 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
d5316a713f7fe31a6a00ab324ba74d16d6c6b468dc7f82811de3dff645c008b5
BLAKE2b-256 checksum
How to use checksums
111068c15582c17fb1ab2e6a0dd1d15cbb7ce8563c1c50ec62eb24ac8153a4b9
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.6-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.2 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7d9741f1bb0fe0174ceea75d103cc256bc0c59570e3bab67e69cc7c1e46f307f
BLAKE2b-256 checksum
How to use checksums
873111c7e4d6f73c8a8e0a9c558e0650e9e26d890da9afc2ce570c964a17422a
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 Nov 17, 2026.

Transparency log

Release files / tuetoken-0.1.6-cp310-cp310-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.6-cp310-cp310-macosx_11_0_arm64.whl
Size 1.1 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
df415b9275cfca2fdace3396bce7d3b82aa02d73faf383013ac041983d86aa01
BLAKE2b-256 checksum
How to use checksums
9439e3a04b52ac6bcb8c99919a4b7d971905d86f77e8de5a0e621a4281c700eb
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 Nov 17, 2026.

Transparency log

Security for tuetoken 0.1.6

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 13 files in this release have been uploaded with provenance metadata.

Select from 13 files

Attestation for tuetoken-0.1.6.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 Nov 17, 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