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]]

Chunks the vocabulary can't answer directly are merged once and remembered in a cache shared by all threads (grows on demand, 128 MB cap by default): 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.5

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.5
File Size Uploaded Provenance Actions
tuetoken-0.1.5.tar.gz 1.1 MB With provenance Details for tuetoken-0.1.5.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for tuetoken 0.1.5
File Actions
tuetoken-0.1.5-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for tuetoken-0.1.5-cp313-cp313-win_amd64.whl
tuetoken-0.1.5-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.5-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.5-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for tuetoken-0.1.5-cp313-cp313-macosx_11_0_arm64.whl
tuetoken-0.1.5-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for tuetoken-0.1.5-cp312-cp312-win_amd64.whl
tuetoken-0.1.5-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.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.5-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for tuetoken-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
tuetoken-0.1.5-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for tuetoken-0.1.5-cp311-cp311-win_amd64.whl
tuetoken-0.1.5-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.5-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.5-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for tuetoken-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
tuetoken-0.1.5-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for tuetoken-0.1.5-cp310-cp310-win_amd64.whl
tuetoken-0.1.5-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.5-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
tuetoken-0.1.5-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for tuetoken-0.1.5-cp310-cp310-macosx_11_0_arm64.whl

Total release size: 14.8 MB

Release files / tuetoken-0.1.5.tar.gz

Download URL tuetoken-0.1.5.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
6803b29817a6fd9f3fc32625597b254dd4b077c5c65112aebeb79b20eef903c2
BLAKE2b-256 checksum
How to use checksums
a50bc94dd99f84637d18c74c552af4bd9103c1135090cd43dd5b6c7385a7008a
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.5-cp313-cp313-win_amd64.whl

Download URL tuetoken-0.1.5-cp313-cp313-win_amd64.whl
Size 1.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
e779f2a46d53a3be2939d9c6136da2bb44a6e2bd8cf858f777fa46783a84954f
BLAKE2b-256 checksum
How to use checksums
8032e93c30b3954c6e70f67eae2fc8691164a044beeae044c1be7a2aff38abac
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.5-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.5-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
1f2ac6dbb471f2e692600c7feca1419b78d8e68ad3deabbfd532166727322118
BLAKE2b-256 checksum
How to use checksums
467bbeeb182ee6c3e79bb3e42bec2fcc7080c178312af6797ec09950800d6c2b
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.5-cp313-cp313-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.5-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
af4c16a4742d7ef70fc9331e0ed6c188307bd9cc337b96550aac19a82ca99b8f
BLAKE2b-256 checksum
How to use checksums
b2009fb304e1bce044442bd1b1ccf55293eb392662e067bb62cc07a9680146c6
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.5-cp312-cp312-win_amd64.whl

Download URL tuetoken-0.1.5-cp312-cp312-win_amd64.whl
Size 1.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
2f13a01f57af6269040bd4d3c63b0383393a0212c2c012d848f10943a2f4673a
BLAKE2b-256 checksum
How to use checksums
a0f1d64f73c977081a1b83753d1a7f23b9af2efca3bc06ea071f59890df530ff
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.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.5-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
5a92516badb9ddc1bcae121643efa1eaac0ed7635459b08708b06839975c7e67
BLAKE2b-256 checksum
How to use checksums
cabafb048df2ee552ea65231f4dcfbe0096912b2006416dfeeb9c37e4d864567
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.5-cp312-cp312-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.5-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
68341c7bb14cef30aaa65ae72ecda963836feb3e7ab1245c743b13f3f243a43c
BLAKE2b-256 checksum
How to use checksums
36e55292223a1371aa360287fab9dec09efa535f279d4ff39e55cbbc650bef5f
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.5-cp311-cp311-win_amd64.whl

Download URL tuetoken-0.1.5-cp311-cp311-win_amd64.whl
Size 1.1 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
cbc745b811ad6db248a03b6d93d3d858a5fa819deb4a307a326eeeaf3012e704
BLAKE2b-256 checksum
How to use checksums
7f37eda5f716f1f0665b46548bd2551782158a6745edb601cd179034708955a7
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.5-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.5-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
044768e056f61c5595659b685184af57a3872e6db4bfdb821278f4148040e68b
BLAKE2b-256 checksum
How to use checksums
a8111ece7946293f2df05e8241213b73a52505144225ee39dd8639df1f342bcb
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.5-cp311-cp311-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.5-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
b5c5792f7ee9e783b67e71d131a6b26de73c4d72b022e04878943b114c06e0e4
BLAKE2b-256 checksum
How to use checksums
68eb5526777c55d764bfbf47caf480b2d5f9c86c28ce59ee3f66607a1dfe4821
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.5-cp310-cp310-win_amd64.whl

Download URL tuetoken-0.1.5-cp310-cp310-win_amd64.whl
Size 1.1 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
3a323644decd19d9ffe126d57dbc164bd2ef52d866a1eb3c3347738434b13550
BLAKE2b-256 checksum
How to use checksums
9d6a6e9565a09f2850359d7541198618f6a6e9f4f6b32fd917f008a5952b66a7
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.5-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.5-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
ab23fcb2cd24ee437e08a2845bc54dc3f0882c4a9c0becd4e083e6f3e08f651d
BLAKE2b-256 checksum
How to use checksums
35a6d339ba9d04ae1df151f9661c4849ef81c02ef32297f622078efab22c3554
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.5-cp310-cp310-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.5-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
a905f443a857b19dcaebacccb0c6b707bbe9ad85a1bad89520c6aa7936bfbe3f
BLAKE2b-256 checksum
How to use checksums
808b5892ef78fd9959cdb75b1104b058a4dfc89ca856f5ec0c45ae9a5c5b19b6
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.5

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.5.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