huggingface/tokenizers

💥 Fast State-of-the-Art Tokenizers optimized for Research and Production

What it solves

Before a language model can read text, the text must be chopped into small pieces called tokens (roughly word fragments). This library does that chopping — and the reverse, turning tokens back into text — as fast as possible, for any language, any model, and any hardware. It aims to be the industry-standard tokenization engine, the place everyone uses and contributes to.

How it works

You start with a Tokenizer object that holds a model's tokenizer.json configuration (you can load it from the Hugging Face Hub or a local file, and edit settings like truncation). Calling compile() builds a CompiledTokenizer, which is the fast object that actually does the work: encode() turns text into token IDs plus attention masks, tokenize() returns the token strings, and decode() turns IDs back into text.

The library is split into small, optional pieces (sub-crates) so you only ship what you need:

  • tk-encode — the inference engine: the tokenization models (BPE, Unigram, WordPiece, WordLevel) and the full pipeline (normalizer, pre-tokenizer, model, post-processor, decoder).
  • bitcannon — pre-tokenization done as a "bitstream program": characters are classified into tags using compact tables, so it avoids heavy Unicode tables and is very fast on CPU.
  • tk-serialize — reads a canonical tokenizer.json into a Tokenizer.
  • tk-convert — rewrites old, legacy tokenizer.json files into the canonical form, so every config ever published stays readable.
  • tk-train — the training half: trains new tokenizers on your own text (kept separate because training is a batch job while inference is a hot loop).
  • tokenizers — a thin umbrella that re-exports everything so existing code keeps working.

Hardware-wise, no SIMD is required: every operation has a portable fallback path that is always compiled and serves as the correctness reference. On ARM (aarch64) and x86_64, extra-fast vectorised versions are used when available (x86_64 picks them at runtime, so one binary covers CPUs since 2008). The whole library is small — about 325 KB gzipped.

Who it's for

Developers building or serving language models who need to convert text to tokens and back, quickly and reliably. It ships official bindings for Rust (the reference implementation), Python, Node.js, and WebAssembly; C/C++/Java/Go bindings are planned, and a community Ruby binding exists. It's also for anyone who wants to train a custom tokenizer on their own data.

Highlights

  • Very fast tokenization, with hardware-adapted kernels for ARM, x86_64, and WebAssembly plus a portable fallback that needs no SIMD.
  • Tiny footprint (~325 KB gzipped) thanks to a modular design where training, legacy conversion, and other optional pieces can be left out of a production binary.
  • Official bindings in Rust, Python, Node.js, and WebAssembly, with more planned.
  • Loads any model's tokenizer straight from the Hugging Face Hub (from_pretrained) or a local file.
  • Can train new tokenizers on your own text, with the training code kept separate from the inference hot path.
  • Ships byte-exact pre-tokenization grammars for common tokenizers like gpt2/ByteLevel, cl100k, o200k, tekken, deepseek, and kimi-k2.
  • Currently in a v1.0.0 release candidate, with a roadmap to restore the full Python API (training, batch encoding, offset tracking, and more).

Related

  • Dispatch
  • Project
  • Dispatch
  • Dispatch
  • Project