Rendered Source Note

tiktoken — Fast BPE Tokenizer for OpenAI Models

Generated HTML view. Markdown remains canonical.

tiktoken — Fast BPE Tokenizer for OpenAI Models

Type: official-doc Tier: 1 (Official Doc) Author(s): OpenAI Date: Accessed 2026-06-01 URL: https://github.com/openai/tiktoken Accessed: 2026-06-01

Why This Source Matters

tiktoken is the lab's hands-on tokenizer for Project 02. It is OpenAI's official, open-source Byte Pair Encoding (BPE) tokenizer — the exact algorithm used to turn text into the integer token IDs that GPT models consume. It grounds the project's first learning objective ("explain what a token is and why LLMs use them") in runnable, authoritative code rather than analogy. It is also the concrete tool the learner uses to predict token cost before calling an API, the bridge back to Project 01's cost tracking.

Key Claims

What it is

Why tokens at all

Properties of BPE tokenization

API (encode / decode / count)

import tiktoken

enc = tiktoken.get_encoding("o200k_base")
enc.encode("hello world")            # -> list[int] of token IDs
enc.decode(enc.encode("hello world")) # -> "hello world"  (lossless round-trip)

# token count = length of the encoded list
num_tokens = len(enc.encode("hello world"))

# get the encoding a specific model actually uses
enc = tiktoken.encoding_for_model("gpt-4o")

Encodings

Relevant To

Notes