Skip to main content

tiktoken Cheatsheet

tiktoken is OpenAI's fast BPE tokenizer for encoding text into LLM-understandable token IDs. Supports GPT-4o, GPT-4, GPT-3.5, DeepSeek, and other major models. Accurate token counting is essential for cost control and context window management.

Updated: 2026-07-20·8 commands

Quick Start

``bash pip install tiktoken

python -c " import tiktoken enc = tiktoken.get_encoding('cl100k_base') tokens = enc.encode('Hello world') print(f'Tokens: {len(tokens)}') print(f'IDs: {tokens}') print(f'Decoded: {enc.decode(tokens)}') " ``

Startup & Modes(8)

CommandLevel
pip install tiktoken
Install tiktoken
Basic
tiktoken encode
Encode text into token IDs
Basic
tiktoken decode
Decode token IDs into text
Basic
tiktoken count
Count tokens in text
Basic
tiktoken encoding for model
Get encoding for a specific model
Basic
tiktoken batch count
Batch count tokens for multiple texts
Intermediate
tiktoken cost estimate
Estimate token count and cost for chat messages
Intermediate
tiktoken special tokens
Encode with special tokens
Expert

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-20.