AI Token Counter & LLM Tokenizer Guide (2026): BPE, Context Windows & API Cost Math
Key Takeaways
- ▸In English prose, 1,000 tokens ≈ 750 words (~4 characters per token, or 1.33 tokens per word), but source code, JSON, and non-Latin scripts require up to 2–3× more tokens for the same character length.
- ▸OpenAI's transition from
cl100k_base(100,256 vocab) too200k_base(200,019 vocab) cuts token counts by 5–10% on code and 25–45% on multilingual text. - ▸Output tokens cost 3× to 5× more than input tokens across GPT-4o, Claude 3.5 Sonnet, and Gemini 1.5 Pro because autoregressive decoding is memory-bandwidth bound on GPU HBM.
- ▸Minifying structured JSON payloads and enabling prompt caching can reduce production LLM API spend by 40–75% without changing model quality.
How Byte Pair Encoding (BPE) Tokenization Works
Large language models do not read raw characters or space-delimited words. Instead, they process sequences of integer token IDs produced by a subword tokenizer—most commonly Byte-Level Byte Pair Encoding (BPE). Byte-level BPE starts with a base vocabulary of 256 individual UTF-8 bytes and iteratively merges the most frequent adjacent byte pairs across a massive training corpus until reaching a target vocabulary size (such as 100,256 or 200,019 tokens).
Before merging bytes, modern tokenizers apply a pre-tokenization regular expression that splits input text into words, numbers, punctuation runs, and leading spaces. In tiktoken, a leading space is attached to the following word—meaning " token" (with a leading space) is a single token, while "token" at the start of a line is a different token ID.
You can inspect live token counts, character ratios, and per-model API costs directly in your browser using BytePane's AI Token Counter—all calculations run locally with zero server uploads.
cl100k_base vs o200k_base vs Llama 3 & Claude 3.5
Not all LLM tokenizers segment text identically. Vocabulary size and pre-tokenization regex rules determine how efficiently a model compresses English prose, source code, numbers, and non-English scripts:
| Tokenizer / Model Family | Vocab Size | English Ratio | Key Characteristics |
|---|---|---|---|
| o200k_base (GPT-4o, o1, o3) | 200,019 | ~1.28 tok/word | Doubled vocabulary; 25–45% fewer tokens on CJK, Arabic, and Indic scripts; better whitespace compression in code. |
| cl100k_base (GPT-4, GPT-3.5) | 100,256 | ~1.33 tok/word | Groups digits into 1–3 digit chunks; standard baseline for OpenAI embeddings and legacy GPT-4 Turbo models. |
| Claude 3.5 (Sonnet / Haiku / Opus) | ~65K–100K BPE | ~1.35 tok/word | Includes XML/HTML tag optimizations; slightly higher token density on dense code blocks than o200k_base. |
| Llama 3 / 3.1 / 3.3 (tiktoken BPE) | 128,256 | ~1.30 tok/word | Switched from SentencePiece (32K) to tiktoken-based 128K BPE, improving code compression by ~15% over Llama 2. |
Token-to-Word Ratios by Content Type
The widely cited rule of thumb—1 token ≈ 0.75 words or 1 token ≈ 4 characters—only holds for clean English prose. When estimating context usage for RAG pipelines, tool-calling schemas, or code assistants, content type shifts token density dramatically:
| Content Type | Chars per Token | Tokens per 1,000 Chars | Why Density Changes |
|---|---|---|---|
| English Prose / Documentation | 3.8 – 4.2 | ~240 – 260 | Common English words and suffixes map to single BPE tokens. |
| TypeScript / Python Source Code | 2.7 – 3.3 | ~300 – 370 | CamelCase identifiers, brackets, operators, and indentation split into multiple tokens. |
| Pretty-Printed JSON | 2.2 – 2.8 | ~360 – 450 | Repeated quotes, colons, braces, and newline indentation inflate token count. |
| Minified JSON | 3.0 – 3.5 | ~285 – 330 | Eliminating structural whitespace saves 15–30% of tokens immediately. |
| Base64 / Hex / UUID Strings | 1.4 – 2.0 | ~500 – 710 | High-entropy random strings cannot merge beyond 1–2 character byte pairs. |
Before sending large JSON payloads in system prompts or tool responses, run them through our JSON Minifier and verify the reduction in the AI Token Counter.
Context Windows, Output Caps & API Cost Math
A model's context window (e.g., 128,000 tokens on GPT-4o, 200,000 tokens on Claude 3.5 Sonnet, or 1M–2M tokens on Gemini 1.5 Pro) is a shared budget across four components:
- System Prompt & Tool Definitions: Every JSON Schema function definition injected into the request consumes input tokens on every turn.
- Conversation History & RAG Context: Retrieved document chunks and prior assistant messages accumulate linearly across multi-turn sessions.
- Reasoning / Thinking Tokens: On reasoning models (o1, o3-mini, Claude 3.7 extended thinking), internal chain-of-thought tokens are billed as output tokens even when hidden from the final text response.
- Visible Output Tokens: Capped by
max_output_tokens(typically 4,096 to 16,384 tokens on standard chat models, up to 64K–100K on reasoning models).
interface TokenUsage {
uncachedInputTokens: number
cachedInputTokens: number
outputTokens: number
}
interface ModelPricingPerMillion {
input: number // e.g. $2.50 / 1M for GPT-4o
cachedInput: number // e.g. $1.25 / 1M for GPT-4o cached prefix
output: number // e.g. $10.00 / 1M for GPT-4o output
}
export function calculateRequestCostUSD(usage: TokenUsage, pricing: ModelPricingPerMillion): number {
const inputCost = (usage.uncachedInputTokens / 1_000_000) * pricing.input
const cachedCost = (usage.cachedInputTokens / 1_000_000) * pricing.cachedInput
const outputCost = (usage.outputTokens / 1_000_000) * pricing.output
return Number((inputCost + cachedCost + outputCost).toFixed(6))
}Frequently Asked Questions
How many words is 1,000 tokens in GPT-4o and Claude 3.5?
In standard English prose, 1,000 tokens equals approximately 750 words (or roughly 4,000 characters), giving an average ratio of 1.33 tokens per word. For source code, JSON payloads, and URLs, punctuation and indentation increase density to 1.8–2.5 tokens per word-equivalent.
What is the difference between cl100k_base and o200k_base tokenizers?
cl100k_base (used by GPT-4 and GPT-3.5-Turbo) has a vocabulary of 100,256 tokens. o200k_base (used by GPT-4o, o1, and o3) expands the vocabulary to 200,019 tokens, reducing token counts by 5–10% on English code and by 25–45% on non-Latin scripts such as Hindi, Arabic, Chinese, and Japanese.
Why are output tokens more expensive than input tokens?
Input tokens are processed in parallel during the prefill phase using matrix-matrix multiplication, which saturates GPU compute efficiently. Output tokens are generated autoregressively one token at a time during the decode phase, requiring a full model weight read from GPU HBM memory for every single token generated.
How do I calculate LLM API cost from token counts?
Total request cost = ((Input Tokens / 1,000,000) × Input Price per 1M) + ((Output Tokens / 1,000,000) × Output Price per 1M). When prompt caching is active, cached input tokens are billed at a 50% to 90% discount depending on the provider.
Does minifying JSON reduce LLM token usage?
Yes. Removing indentation, newlines, and spaces after colons in pretty-printed JSON typically reduces token consumption by 15–30%, lowering both API cost and time-to-first-token latency.
Count LLM Tokens & Estimate API Cost Instantly
Paste any prompt, system instruction, JSON schema, or code file to compare token counts across GPT-4o, Claude 3.5, and Llama 3 in real time. Runs 100% client-side in your browser.
Open AI Token Counter →