# openai/tiktoken architecture

> Architecture diagram of the GitHub repository openai/tiktoken, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openai/tiktoken
- Repository: https://github.com/openai/tiktoken

## Overview

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: [`tiktoken/__init__.py`](https://github.com/openai/tiktoken/blob/main/tiktoken/__init__.py) (Public exports), [`tiktoken/registry.py`](https://github.com/openai/tiktoken/blob/main/tiktoken/registry.py) (Encoding registry), [`tiktoken/core.py`](https://github.com/openai/tiktoken/blob/main/tiktoken/core.py) (Encoding API), [`tiktoken/model.py`](https://github.com/openai/tiktoken/blob/main/tiktoken/model.py) (Model mapping)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_public["Public exports<br/>[__init__.py]"]
  node_model["Model mapping<br/>[model.py]"]
  node_registry["Encoding registry<br/>[registry.py]"]
end

subgraph group_encoding["Encoding Runtime"]
  node_encoding["Encoding API<br/>[core.py]"]
  node_native["BPE engine<br/>[lib.rs]"]
  node_binding["Python binding<br/>[py.rs]"]
end

subgraph group_vocab["Vocabulary Sources"]
  node_plugin["OpenAI encodings<br/>[openai_public.py]"]
  node_loader["Vocabulary loader<br/>[load.py]"]
end

subgraph group_learning["Educational BPE"]
  node_educational["Educational BPE<br/>[_educational.py]"]
end

node_user(("Python caller"))
node_assets[("Vocabulary files")]

node_user -->|"calls"| node_public
node_public -->|"exports"| node_encoding
node_public -->|"exports"| node_model
node_public -->|"exports"| node_registry
node_model -->|"resolves through"| node_registry
node_registry -->|"constructs"| node_encoding
node_registry -->|"discovers"| node_plugin
node_plugin -->|"loads ranks"| node_loader
node_loader -->|"reads"| node_assets
node_encoding -->|"calls"| node_binding
node_native -->|"exports"| node_binding
node_public -->|"supports learning"| node_educational
node_educational -->|"uses encodings"| node_public

click node_public "https://github.com/openai/tiktoken/blob/main/tiktoken/__init__.py"
click node_model "https://github.com/openai/tiktoken/blob/main/tiktoken/model.py"
click node_registry "https://github.com/openai/tiktoken/blob/main/tiktoken/registry.py"
click node_encoding "https://github.com/openai/tiktoken/blob/main/tiktoken/core.py"
click node_native "https://github.com/openai/tiktoken/blob/main/src/lib.rs"
click node_binding "https://github.com/openai/tiktoken/blob/main/src/py.rs"
click node_plugin "https://github.com/openai/tiktoken/blob/main/tiktoken_ext/openai_public.py"
click node_loader "https://github.com/openai/tiktoken/blob/main/tiktoken/load.py"
click node_educational "https://github.com/openai/tiktoken/blob/main/tiktoken/_educational.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public,node_model,node_registry toneBlue
class node_encoding,node_native,node_binding,node_assets toneAmber
class node_plugin,node_loader toneMint
class node_educational toneRose
class node_user toneIndigo
```

## Components

### Public API

- [Public exports](https://github.com/openai/tiktoken/blob/main/tiktoken/__init__.py): `tiktoken/__init__.py`
- [Model mapping](https://github.com/openai/tiktoken/blob/main/tiktoken/model.py): `tiktoken/model.py`
- [Encoding registry](https://github.com/openai/tiktoken/blob/main/tiktoken/registry.py): `tiktoken/registry.py`

### Encoding Runtime

- [Encoding API](https://github.com/openai/tiktoken/blob/main/tiktoken/core.py): `tiktoken/core.py`
- [BPE engine](https://github.com/openai/tiktoken/blob/main/src/lib.rs): `src/lib.rs`
- [Python binding](https://github.com/openai/tiktoken/blob/main/src/py.rs): `src/py.rs`

### Vocabulary Sources

- [OpenAI encodings](https://github.com/openai/tiktoken/blob/main/tiktoken_ext/openai_public.py): `tiktoken_ext/openai_public.py`
- [Vocabulary loader](https://github.com/openai/tiktoken/blob/main/tiktoken/load.py): `tiktoken/load.py`

### Educational BPE

- [Educational BPE](https://github.com/openai/tiktoken/blob/main/tiktoken/_educational.py): `tiktoken/_educational.py`

### Other components

- **Python caller**
- **Vocabulary files**

## Connections

- Python caller → Public exports: calls (evidence: `README.md`)
- Public exports → Encoding API: exports (evidence: `tiktoken/__init__.py`)
- Public exports → Model mapping: exports (evidence: `tiktoken/__init__.py`)
- Public exports → Encoding registry: exports (evidence: `tiktoken/__init__.py`)
- Model mapping → Encoding registry: resolves through (evidence: `tiktoken/model.py`)
- Encoding registry → Encoding API: constructs (evidence: `tiktoken/registry.py`)
- Encoding registry → OpenAI encodings: discovers (evidence: `README.md`)
- OpenAI encodings → Vocabulary loader: loads ranks (evidence: `tiktoken_ext/openai_public.py`)
- Vocabulary loader → Vocabulary files: reads (evidence: `tiktoken/load.py`)
- Encoding API → Python binding: calls (evidence: `tiktoken/core.py`)
- BPE engine → Python binding: exports (evidence: `src/lib.rs`)
- Public exports → Educational BPE: supports learning (evidence: `README.md`)
- Educational BPE → Public exports: uses encodings (evidence: `tiktoken/_educational.py`)
