Byte-Pair Encoding (BPE) is a subword tokenization algorithm used by LLMs like GPT-4o, Claude, and Llama to convert text and code into numeric token vectors.
Byte-Pair Encoding (BPE) is a data compression and subword tokenization algorithm widely adopted in modern Large Language Models (LLMs)—including OpenAI GPT-5 and GPT-4o, Anthropic Claude, Meta Llama 3/4, and DeepSeek. BPE bridges raw Unicode text and neural network transformer architectures by iteratively replacing the most frequent byte pairs in a dataset with newly created composite tokens. Subword BPE tokenization eliminates out-of-vocabulary (OOV) errors, compresses high-frequency words into single integers, and ensures efficient representation for both natural languages and programming codebases.
Count your prompt tokens across major BPE vocabularies with our AI Token Counter or estimate multi-turn inference budgets using the AI Cost Calculator.
| Specification | Details |
|---|---|
| Algorithm Type | Bottom-up greedy statistical subword tokenization |
| Origin | Philip Gage (1994, data compression); adapted by Sennrich et al. (2015, NLP) |
| Standard Implementations | tiktoken (Rust/WASM/Python), Hugging Face tokenizers, sentencepiece |
| Key Vocabularies | o200k_base (200k tokens), cl100k_base (100k tokens), p50k_base (50k tokens) |
| Token Density (English) | $\approx$ 3.8 to 4.2 characters per token ($\approx$ 0.75 words per token) |
| Token Density (Source Code) | $\approx$ 2.4 to 3.0 characters per token (due to syntax, whitespace, and camelCase) |
| Fallback Mechanism | Direct byte-level encoding (handles any valid UTF-8 sequence without unknown tokens) |
The BPE training and encoding lifecycle operates in three deterministic phases:
Raw Text: "low lower lowest"
│
▼ 1. Character/Byte Initialization
['l', 'o', 'w', ' ', 'l', 'o', 'w', 'e', 'r', ' ', 'l', 'o', 'w', 'e', 's', 't']
│
▼ 2. Iterative Frequency Pair Merging (e.g. 'l' + 'o' -> 'lo', 'lo' + 'w' -> 'low')
['low', ' ', 'low', 'e', 'r', ' ', 'low', 'e', 's', 't']
│
▼ 3. Vocabulary Table Lookup (String -> Integer Token ID)
[4821, 220, 4821, 359, 220, 4821, 1948]
't' and 'h' become 'th') and assign it a new token ID.When text is passed to an LLM, the tokenizer applies the learned merge rules in priority order. Common words like "function" or "developer" resolve to single tokens in one step, while novel or compound identifiers like "parseJwtClaimsPayload" split into known constituent subwords (["parse", "Jwt", "Claims", "Payload"]).
| Encoding / Model | Vocab Size | Primary Optimization | Efficiency Characteristic |
|---|---|---|---|
o200k_base (GPT-5, GPT-4o, o3) |
200,000 tokens | Code, non-Latin scripts, math | 15–20% fewer tokens on Python, JS, and Asian languages |
cl100k_base (GPT-4, Claude proxy) |
100,000 tokens | General English, structured JSON | Standard ~4 chars/token benchmark |
| Llama 3 / 4 BPE | 128,256 tokens | Reasoning traces, code, dialogue | High throughput on open-weight inference stacks |
| SentencePiece Unigram (Gemini) | 256,000 tokens | Multimodal tokens, 2M+ context | Subword probability modeling with explicit whitespace tokens |
tiktoken in TypeScriptimport { getEncoding } from 'js-tiktoken';
// Load OpenAI o200k_base BPE vocabulary
const tokenizer = getEncoding('o200k_base');
const prompt = `function calculateDiscount(tokens: number, isCached: boolean): number {
return isCached ? tokens * 0.1 : tokens;
}`;
// Encode text into array of 32-bit unsigned integers
const tokenIds = tokenizer.encode(prompt);
console.log(`Tokens: ${tokenIds.length}`);
console.log(`Token IDs:`, tokenIds);
// Decode token IDs back to human-readable string
const decoded = tokenizer.decode(tokenIds);
console.log(`Decoded string matches original: ${decoded === prompt}`);
Word-based tokenization requires massive dictionaries that still fail on typos, inflections, slang, and novel variable names. Byte-level BPE guarantees that any arbitrary sequence of bytes can be tokenized down to its individual byte representations if no multi-character merge rule exists, eliminating the need for special <unk> (unknown token) placeholders.
Source code heavily utilizes camelCase and snake_case variable names, punctuation operators (===, =>, {}), and multi-space indentation. Because BPE vocabularies prioritize common natural language collocations, code identifiers often split across multiple smaller subword tokens, yielding a lower character-to-token density (~2.5 chars/token).
Use DevFlow's AI Token Counter to calculate real-time token counts across 100+ LLMs, or plan monthly infrastructure spend with our AI Cost Calculator.
Free, browser-based utilities to test, generate, and inspect Byte-Pair Encoding (BPE Tokenization in LLMs) payloads directly.
Count tokens and estimate API costs for 100+ LLMs with exact BPE and provider tokenization.
Model and compare LLM API spend across 120+ providers for any workload with prompt caching and batch pricing.
Build structured LLM prompts with per-section token counting, variable injection, and provider-aware exports for OpenAI, Anthropic, and Google.