Pythie
CategoriesTrending
An open index of Python tools.Pythie’s IndexLegal noticePrivacyData: PyPI (package metadata; download statistics under CC BY 4.0, via ClickHouse), deps.dev (CC BY 4.0), GitHub, OSV.

tiktoken

0.14.0ActivePythie’s Index: 47

tiktoken is a fast BPE tokeniser for use with OpenAI's models

Maintained. Last release 3 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity and release cadence.

LLMs & Generative AIText & NLP
$pip install tiktoken
openai/tiktokenPyPI

⏳ tiktoken

tiktoken is a fast BPE tokeniser for use with OpenAI's models.

import tiktoken
enc = tiktoken.get_encoding("o200k_base")
assert enc.decode(enc.encode("hello world")) == "hello world"

# To get the tokeniser corresponding to a specific model in the OpenAI API:
enc = tiktoken.encoding_for_model("gpt-4o")

The open source version of tiktoken can be installed from PyPI:

pip install tiktoken

The tokeniser API is documented in tiktoken/core.py.

Example code using tiktoken can be found in the OpenAI Cookbook.

Performance

tiktoken is between 3-6x faster than a comparable open source tokeniser:

image

Performance measured on 1GB of text using the GPT-2 tokeniser, using GPT2TokenizerFast from tokenizers==0.13.2, transformers==4.24.0 and tiktoken==0.2.0.

Getting help

Please post questions in the issue tracker.

If you work at OpenAI, make sure to check the internal documentation or feel free to contact @shantanu.

What is BPE anyway?

Language models don't see text like you and I, instead they see a sequence of numbers (known as tokens). Byte pair encoding (BPE) is a way of converting text into tokens. It has a couple desirable properties:

  1. It's reversible and lossless, so you can convert tokens back into the original text
  2. It works on arbitrary text, even text that is not in the tokeniser's training data
  3. It compresses the text: the token sequence is shorter than the bytes corresponding to the original text. On average, in practice, each token corresponds to about 4 bytes.
  4. It attempts to let the model see common subwords. For instance, "ing" is a common subword in English, so BPE encodings will often split "encoding" into tokens like "encod" and "ing" (instead of e.g. "enc" and "oding"). Because the model will then see the "ing" token again and again in different contexts, it helps models generalise and better understand grammar.

tiktoken contains an educational submodule that is friendlier if you want to learn more about the details of BPE, including code that helps visualise the BPE procedure:

from tiktoken._educational import *

# Train a BPE tokeniser on a small amount of text
enc = train_simple_encoding()

# Visualise how the GPT-4 encoder encodes text
enc = SimpleBytePairEncoding.from_tiktoken("cl100k_base")
enc.encode("hello world aaaaaaaaaaaa")

Extending tiktoken

You may wish to extend tiktoken to support new encodings. There are two ways to do this.

Create your Encoding object exactly the way you want and simply pass it around.

cl100k_base = tiktoken.get_encoding("cl100k_base")

# In production, load the arguments directly instead of accessing private attributes
# See openai_public.py for examples of arguments for specific encodings
enc = tiktoken.Encoding(
    # If you're changing the set of special tokens, make sure to use a different name
    # It should be clear from the name what behaviour to expect.
    name="cl100k_im",
    pat_str=cl100k_base._pat_str,
    mergeable_ranks=cl100k_base._mergeable_ranks,
    special_tokens={
        **cl100k_base._special_tokens,
        "<|im_start|>": 100264,
        "<|im_end|>": 100265,
    }
)

Use the tiktoken_ext plugin mechanism to register your Encoding objects with tiktoken.

This is only useful if you need tiktoken.get_encoding to find your encoding, otherwise prefer option 1.

To do this, you'll need to create a namespace package under tiktoken_ext.

Layout your project like this, making sure to omit the tiktoken_ext/__init__.py file:

my_tiktoken_extension
├── tiktoken_ext
│   └── my_encodings.py
└── setup.py

my_encodings.py should be a module that contains a variable named ENCODING_CONSTRUCTORS. This is a dictionary from an encoding name to a function that takes no arguments and returns arguments that can be passed to tiktoken.Encoding to construct that encoding. For an example, see tiktoken_ext/openai_public.py. For precise details, see tiktoken/registry.py.

Your setup.py should look something like this:

from setuptools import setup, find_namespace_packages

setup(
    name="my_tiktoken_extension",
    packages=find_namespace_packages(include=['tiktoken_ext*']),
    install_requires=["tiktoken"],
    ...
)

Then simply pip install ./my_tiktoken_extension and you should be able to use your custom encodings! Make sure not to use an editable install.

Popularity

Downloads / month
231.5M
GitHub stars
19.7K
Downloads, last 7 days
54.8M-1%
Stars, last 7 days
–
Direct dependents
3.5K
All dependents
13.5K

Pythie’s Index

How it works
47

Fair

Maintenance, out of 100

  • Recent release

    last release 103 days ago

  • Commit activity

    2/26 weeks with commits

  • Release cadence

    2 releases in 12 months

  • Issues & PRs handled

    43% closed in the past year

  • Contributor spread

    26 authors in last 100 commits

  • OpenSSF Scorecard

    4.8/10

Commits, last 26 weeks9

Releases

Latest
0.14.0
Released
Aug 17, 2026
Releases, 12 months
2
First release
Dec 15, 2022

Compatibility

Requires Python
>=3.9
License
MIT (Permissive)

Alternatives

Use instead of tiktoken: tools that do the same job, closest first.

Compare with toktok-rs →Compare with tokenizers →Compare with gigatoken →Compare all 4 →

  • toktok-rs0.5.0ActivePythie’s Index: 64

    Fast, exact BPE tokenizer for OpenAI encodings — Rust core, tiktoken-identical ids

    LLMs & Generative AIPerformance & Low-level14168.5K/moReleased Nov 16, 2026
  • tokenizers0.23.3ActivePythie’s Index: 84
    Text & NLPLLMs & Generative AI11.3K226.4M/moReleased Nov 20, 2026
  • gigatoken0.11.0ActivePythie’s Index: 71

    Tokenize your documents at GB/s

    Text & NLPLLMs & Generative AI4.2K304.7K/moReleased Nov 2, 2026
  • tokie0.3.0ActivePythie’s Index: 76

    Blazingly fast tokenizer — 50x faster, 10x smaller, 100% accurate

    Text & NLPLLMs & Generative AI57508.2K/moReleased Nov 12, 2026

Works well with

Use together with tiktoken: extensions, drivers, servers and companion tools.

  • openaiPythie’s Index: 90The official Python library for the openai API391.7M downloads/mo
  • semchunkPythie’s Index: 48A Python library for splitting text into smaller chunks while preserving as much local semantic context as possible.5.3M downloads/mo
  • tokencostPythie’s Index: 26To calculate token and translated USD cost of string and message calls to OpenAI, for example when used by AI agents137.3K downloads/mo
  • semantic-text-splitterPythie’s Index: 84Split text into semantic chunks, up to a desired chunk size. Supports calculating length by characters and tokens, and is callable from Rust and Python.411.4K downloads/mo
  • repomixPythie’s Index: 41A tool for analyzing and summarizing code repositories208.2K downloads/mo