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()
out  = enc.encode_ordinary_to_buffer([data], separator=b"<|endoftext|>")
ids, offsets = out["data"], out["offsets"]           # doc i = ids[offsets[i]:offsets[i+1]]

separator= splits each input into documents inside Rust (same result as data.split(b"<|endoftext|>"), one row per piece, without building a Python list); count_tokens_batch takes it too. Pass [data] without a separator to encode one big document. dtype="uint16" returns 2-byte ids (half the memory, ready for np.memmap-style training data) when the vocabulary has at most 65,536 ids, as GPT-2's does.

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

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

Built distributions (wheels)

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

Total release size: 15.3 MB

Release files / tuetoken-0.1.7.tar.gz

Download URL tuetoken-0.1.7.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
1a2364f98358616a8f88a151a2e91c3c48b1baf431c7b8f51cf39bcc0373af4f
BLAKE2b-256 checksum
How to use checksums
323cfe8807c975a98d3ff905a57017e5431a34500e197ccbce90027050de2a5c
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.7-cp313-cp313-win_amd64.whl

Download URL tuetoken-0.1.7-cp313-cp313-win_amd64.whl
Size 1.2 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
ac1e5ef80652614d9f66667eb00c7589a9cb279c05d6675e38744617579d8c1f
BLAKE2b-256 checksum
How to use checksums
5bbac6528f16748cde619039ab626ff63fdb91427305fc5e6b144f73c6ed5073
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.7-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.7-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
b9882b036937262cfe9107b3db57d21453bd9d56fd05efa1761c5abb55e02ff2
BLAKE2b-256 checksum
How to use checksums
ac8e255daa91fe3e7c66cbf0bf4c641cc6bd480e32ac08f2bbcc469ceb090fb9
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.7-cp313-cp313-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.7-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
31c113fbdbf9e6ec796e8ae2aa17423b18faedd7888f5ab084f4db42380ad93c
BLAKE2b-256 checksum
How to use checksums
c851e09ac3623dc7400f2f9a9dab214e2209fba0489ecd97955788a742488c31
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.7-cp312-cp312-win_amd64.whl

Download URL tuetoken-0.1.7-cp312-cp312-win_amd64.whl
Size 1.2 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
a7e7d09c7497fa78887ceaa7c0db6f9db416fed5cf5a8027c3175342abfb2c7c
BLAKE2b-256 checksum
How to use checksums
f85f0fe0307fac2c19a215c311281f57346b2cb17c97e5ac8a605833eb378e8a
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.7-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.7-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
16799b22664e22dcb8b825748314e30804a8473ec6543c2d55bb24bbca48ec40
BLAKE2b-256 checksum
How to use checksums
dbcb8f6b1505ce7aff2dd6a7f43035f03fe56f9892aa952404998f1d35add14c
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.7-cp312-cp312-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.7-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
8334d8ea57e84b7701a5b0f7b3824a6ae689fd256a222166151eb9a755370680
BLAKE2b-256 checksum
How to use checksums
f5fbb8b73b56d6348298e4cbeb54acf25ec3282ecd46ff72428ad2b85653a938
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.7-cp311-cp311-win_amd64.whl

Download URL tuetoken-0.1.7-cp311-cp311-win_amd64.whl
Size 1.2 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
4bd320e1f82ff0e8876e1202257bf7c58e0393d2da50233965d51a2c11143c72
BLAKE2b-256 checksum
How to use checksums
106b9d7a4675b9ceab1ddba365b6b1f8a0e92b34dc179eac67e3220933d329a1
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.7-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.7-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
b853c7bc05c784a37ee825e7c604dcec528ba6a95f51bf255d6f1ab2c247c9c5
BLAKE2b-256 checksum
How to use checksums
0f626b156f0d6778a70454fa59475706725c929b295931e5e5933e6cad50587b
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.7-cp311-cp311-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.7-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
9835cf3e3504659e469d6c7f353b718cb4ee224039cc0bba72e555ec4f62559c
BLAKE2b-256 checksum
How to use checksums
c582da5a5bfa3b7e5bf9ef3a06dc64f6cefb043fa3d058141a6c14efaadedc66
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.7-cp310-cp310-win_amd64.whl

Download URL tuetoken-0.1.7-cp310-cp310-win_amd64.whl
Size 1.2 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
7e78020178ddd43483d651e69848ee35cea401eb6309bbb8774df066fa2b6bc0
BLAKE2b-256 checksum
How to use checksums
3bbfd8560453a78bb9cce5c42746fd2db8eae0731e9c73b0a3895c6e1dd3b1f8
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.7-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL tuetoken-0.1.7-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
3f3b53b091a84be730965b5ecccbed35387f07f3ddee6e78123046b3a7bfec81
BLAKE2b-256 checksum
How to use checksums
fd038e8be15d9bf5f205735d08d1a44d35080c11cbd2237ba439eb0354249de2
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.7-cp310-cp310-macosx_11_0_arm64.whl

Download URL tuetoken-0.1.7-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
7a696b41a21d195058fdfcb7a6be32085df8630f7e0c66011cfc0a926329e3bd
BLAKE2b-256 checksum
How to use checksums
8b71a71a7fadbf2dc5754912cf65d7a5aa127382159d001404571ac0d6978f12
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.7

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