How openai/tiktoken works
tiktoken is a Python BPE tokenizer backed by a Rust implementation. The main workflow is to select an encoding directly or by model name, load its vocabulary and special-token configuration, then encode or decode text. The repository also provides plugin-based encoding registration and an educational BPE implementation. Edges reflect the excerpts, SOURCE INDEX references, or README statements; unsampled internals are not assumed.
Main entry points: __init__.py Public exports · registry.py Encoding registry · core.py Encoding API · model.py Model mapping
Components 11
Public API
- Public exports
tiktoken/__init__.py - Model mapping
tiktoken/model.py - Encoding registry
tiktoken/registry.py
Encoding Runtime
- Encoding API
tiktoken/core.py - BPE engine
src/lib.rs - Python binding
src/py.rs
Vocabulary Sources
- OpenAI encodings
tiktoken_ext/openai_public.py - Vocabulary loader
tiktoken/load.py
Educational BPE
- Educational BPE
tiktoken/_educational.py
Other components
- Python caller
- Vocabulary files
Connections 13
- Python caller → Public exports: calls
README.md - Public exports → Encoding API: exports
tiktoken/__init__.py - Public exports → Model mapping: exports
tiktoken/__init__.py - Public exports → Encoding registry: exports
tiktoken/__init__.py - Model mapping → Encoding registry: resolves through
tiktoken/model.py - Encoding registry → Encoding API: constructs
tiktoken/registry.py - Encoding registry → OpenAI encodings: discovers
README.md - OpenAI encodings → Vocabulary loader: loads ranks
tiktoken_ext/openai_public.py - Vocabulary loader → Vocabulary files: reads
tiktoken/load.py - Encoding API → Python binding: calls
tiktoken/core.py - BPE engine → Python binding: exports
src/lib.rs - Public exports → Educational BPE: supports learning
README.md - Educational BPE → Public exports: uses encodings
tiktoken/_educational.py