tokenizers
Fast NLP tokenizer library by Hugging Face for text processing.
3. Used by agents' projects
Dependency in 25 public GitHub projects set up for AI coding agents that we scanned (0.4%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
huggingface.co/docs/tokenizers/llms.txtFrom the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What tokenizers does
Hugging Face's tokenizer library is partway through a Rust-first rewrite to v1.0. Inference is split from training so a serving build carries only what it needs, and other projects treat its output as the correct answer for token ids.
tokenizers popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How tokenizers is used
- tokenizers is a dependency in 25 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (1.5% of the 1,623 Python ones), #3 of 78 in prompts, structured output and evals.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 756; those are left out of every share on this page.
- The most used alternative in our scan is tiktoken (Python) (58 projects).
- Projects that use it most often also use NumPy (100.0%), PyYAML (92.0%), Hugging Face Hub (88.0%).
By coding agent
Often used with
tokenizers in practice
Strengths
Inference lives in <code>tk-encode</code> and training in <code>tk-train</code>. The README says the two have "opposite constraints", so a production binary does not link a trainer, a corpus reader or legacy converters.
<code>tk-convert</code> rewrites tokenizer files from any older version into the canonical form. It is a pure JSON-to-JSON pass, and you can disable it to get smaller on-device builds.
Every vectorised kernel has a portable path that is always compiled. That path also serves as the test oracle for the vectorised versions, so missing CPU features only cost speed. On x86_64 the dispatch happens at runtime.
The <code>bitcannon</code> crate pre-tokenizes using a bitstream approach and has no runtime dependency on Unicode tables. It ships grammars for several current model families. The maintainers say it is, as far as they know, the fastest implementation for every pretokenizer regex on CPU.
Watch out for
The docs say rc0 only loads a <code>tokenizer.json</code>, encodes and decodes. Building a tokenizer from components, editing it, saving it and training are not exposed yet.
The maintainers say they introduced breaking changes in the switch from 0.23. They plan to describe them in a blog post that is not yet published. Installing the candidate needs <code>pip install --pre tokenizers</code>.
The Rust core roadmap lists an open fix for ModernBERT and OLMo tokenizers, which fail to load with a "64-bit hash collision". Offset tracking and windowing are also still on the list.
Load a pretrained tokenizer, encode and decode
>>> from tokenizers import Tokenizer
>>> tokenizer = Tokenizer.from_pretrained("meta-llama/Llama-3.1-8B") # or .from_file(path)
>>> tokenizer.encode("Hello, y'all! How are you ๐ ?")
Encoding(ids=[128000, 9906, 11, 379, 65948, 0, 2650, 527, 499, 27623, 223, 949], type_ids=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], attention_mask=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
>>> tokenizer.tokenize("Hello")
['<|begin_of_text|>', 'Hello']
>>> tokenizer.decode([15339, 1917])
'hello world'
>>> tokenizer.decode_tokens(tokenizer.encode("Hello"))
['<|begin_of_text|>', 'Hello']More about tokenizers
Packages around it
- numpy: Arrays
- pyyaml: YAML parsing
- huggingface-hub: Hub downloads
- pydantic: Data validation
- tqdm: Progress bars
- charset-normalizer: Encoding detection
More in Prompts, structured output and evals
Alternatives
tokenizers This page
Fast BPE tokenizer for OpenAI models enabling accurate token counting and text splitting.
Unsupervised text tokenizer and detokenizer from Google for natural language processing.
Get started
Install it.
pip install tokenizersRules for your agent
## tokenizers
- Check whether the project pins the 0.23 line (latest release v0.23.3) or the v1 candidate. The candidate installs with `pip install --pre tokenizers`.
- Load tokenizers with `Tokenizer.from_pretrained("<repo>")` or `Tokenizer.from_file(path)`. Read ids from the returned `Encoding`.
- On v1 rc0, do not write code that trains, builds from components, edits or saves a tokenizer. The docs say these are not exposed yet.
- If `huggingface_hub` v2 is in the environment, use tokenizers v0.23.3 or later. That release loosened the requirement.
- In Rust, depend on the umbrella `tokenizers` crate unless you know you want less.
- For wasm32 builds, enable the `simd128` target feature. Without it, the scalar path is used.