Python tokenizer libraries

Updated Nov 2026

Python tokenizer libraries split text into the tokens a language model reads and map each token to an integer ID. A subword tokenizer trains its vocabulary from text rather than splitting only on spaces and punctuation. These libraries let apps and AI agents count tokens before a model call.

Popularity

tiktoken has the most monthly downloads in this comparison, at 223.6M.

LibraryMonthly downloadsStarsLast releaseLicense
tiktoken
223.6M
19.7KAug 2026MIT
Tokenizers
216.8M
11.3KOct 2026Apache-2.0
Transformers
116.7M
169.2KNov 2026Apache-2.0
NLTK
48.7M
14.8KOct 2026Apache-2.0
SentencePiece
29.8M
12.2KNov 2026Apache-2.0
spaCy
25.2M
34.1KOct 2026MIT
Sentence Transformers
22.8M
19.3KNov 2026Apache-2.0
Jieba
6.2M
35.2KJan 2020MIT
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Encodes and decodes text using byte-pair tokenization for OpenAI models.

Downloads / mo
223.6M
Stars
19.7K
Last release
0.14.0
License
MIT

Strengths

  • Selects encodings by model name. Use encoding_for_model to select the model’s encoding.
  • Preserves token bytes for inspection. Use decode_single_token_bytes to inspect an individual token.

Limitations

  • Special-token text raises by default. Choose allowed_special or disallowed_special behavior for text containing special-token strings.
  • Partial decoding can replace bytes. Use byte decoding or strict errors when inspecting incomplete UTF-8 tokens.

Quickstart

From README.md
$ python -m pip install tiktoken
import tiktoken
encoding = tiktoken.get_encoding('o200k_base')
print(encoding.encode('Hello world'))

tiktoken examples

From open-source projects on GitHub

Provides configurable tokenization pipelines and vocabulary training through Rust-backed Python bindings.

Downloads / mo
216.8M
Stars
11.3K
Last release
0.23.3
License
Apache-2.0

Strengths

  • Trains a custom vocabulary. Train from files or an iterator of text.
  • Tracks tokens to original text. Use offsets to recover the input span behind each token.

Limitations

  • Decoding needs a matching decoder. Configure the decoder to handle the vocabulary’s subword markers.

Quickstart

From README.md
$ python -m pip install tokenizers
from tokenizers import Tokenizer
tokenizer = Tokenizer.from_pretrained('bert-base-cased')
print(tokenizer.encode('Hello world').ids)

Tokenizers examples

From open-source projects on GitHub
def encode(self, txt: str):
    """
    clean_text > (append `lang_id`) > replace SPACE > encode text using Tokenizer
    """
    txt = txt.replace(' ', SPACE)
    code = self.tokenizer.encode(txt)
    ids = code.ids
    return ids

Loads model-specific tokenizers and prepares encoded model inputs with padding and special tokens.

Downloads / mo
116.7M
Stars
169.2K
Last release
5.23.0
License
Apache-2.0

Strengths

  • Loads the matching tokenizer class. AutoTokenizer resolves a tokenizer using the model configuration.
  • Prepares padded model input batches. Create input IDs and attention masks with padding and truncation.

Limitations

  • New tokens need matching embeddings. Resize the model’s token embeddings after extending its vocabulary.
  • Offsets depend on the backend. Use a fast tokenizer when requesting character offset mappings.

Quickstart

From custom_tokenizers.md
$ python -m pip install transformers
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
print(tokenizer('Hello world')['input_ids'])

Transformers examples

From open-source projects on GitHub

Provides a natural language toolkit.

Downloads / mo
48.7M
Stars
14.8K
Last release
3.10.4
License
Apache-2.0

Quickstart

From nltk.tokenize.html
$ python -m pip install nltk
from nltk.tokenize import wordpunct_tokenize
print(wordpunct_tokenize('Hello, world!'))

NLTK examples

From open-source projects on GitHub

Trains and applies subword tokenization directly to raw text.

Downloads / mo
29.8M
Stars
12.2K
Last release
0.2.3
License
Apache-2.0

Strengths

  • Trains directly from raw text. Train a vocabulary without a language-specific word splitter.
  • Supports sampled segmentations. Sample different subword splits while preparing training text.

Limitations

  • Normalization can change whitespace. Check the model’s whitespace rules before assuming exact text reconstruction.

Quickstart

From README.md
$ python -m pip install sentencepiece
import sentencepiece as spm
processor = spm.SentencePieceProcessor(model_file='test/botchan_en_unigram_1000.model')
print(processor.encode('This is a test'))

SentencePiece examples

From open-source projects on GitHub

Processes natural language in Python.

Downloads / mo
25.2M
Stars
34.1K
Last release
3.8.18
License
MIT

Quickstart

From README.md
$ python -m pip install spacy
import spacy
nlp = spacy.blank('en')
print([token.text for token in nlp.tokenizer('Hello, world!')])

Sentence Transformers

Computes embeddings for retrieval and reranking.

Downloads / mo
22.8M
Stars
19.3K
Last release
6.3.0
License
Apache-2.0
$ python -m pip install sentence-transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
features = model.preprocess(['Hello world'])
print(features)

Sentence Transformers examples

From open-source projects on GitHub

Segments Chinese text into words.

Downloads / mo
6.2M
Stars
35.2K
Last release
0.42.1
License
MIT

Quickstart

From README.md
$ python -m pip install jieba
import jieba
print(jieba.lcut('我来到北京清华大学'))

Jieba examples

From open-source projects on GitHub

FAQ

tiktoken suits counting and encoding tokens for OpenAI models. Tokenizers and SentencePiece suit training a custom vocabulary. Transformers suits loading the tokenizer that matches a specific model.

tiktoken encodes and decodes text with byte-pair tokenization for OpenAI models. It can select the encoding for a given model name.

SentencePiece trains and applies subword tokenization directly on raw sentences.

Jieba segments Chinese text into words.

Encoding raises by default for text that matches a special token. The allowed_special and disallowed_special parameters control that behavior.

For coding agents

Found your tokenizer library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official