Python package that encodes and decodes text with byte pair encoding for use with OpenAI models. Available via PyPI and extensible with custom encodings.
Overview
Tiktoken is a byte pair encoding (BPE) tokenizer aimed at OpenAI model workflows. It turns arbitrary text into integer token sequences and can convert those tokens back into the original text without loss.
BPE compresses text so token counts are typically shorter than raw bytes, and common subword pieces recur across contexts. The package is positioned as faster than comparable tokenizers in published benchmarks on large text corpora.
You can install the published package from PyPI and use the documented encoding API. An educational submodule helps illustrate how BPE works, and authors can supply custom encodings either directly or through a plugin registration mechanism.
Encodings can be wired into applications that need compatible tokenization with OpenAI-style models, including examples referenced in external cookbooks.
Key features
- BPE text-to-token encoding
- Reversible lossless token decoding
- PyPI installation
- Custom encoding objects
- Plugin-based encoding registration
- Educational BPE visualization helpers
- Documented tokenizer API
Use cases
- Tokenizing text for OpenAI models
- Comparing tokenizer performance
- Learning how BPE works
- Adding custom token encodings