Tokenization is the process large language models use to break text into smaller units called tokens before processing it. Unlike simple word or character splitting, tokenization uses algorithms like Byte Pair Encoding (BPE) to create subword units that balance efficiency and meaning. A single token might be a whole word like "cat", a word fragment like "ing", or even a single character for rare symbols. Understanding how ChatGPT, Claude, and other LLMs tokenize your input helps you estimate API costs, write better prompts, and troubleshoot why certain phrases confuse the model.
What Is Tokenization in Large Language Models
When you type a prompt into ChatGPT or Claude, the model doesn't read your text word by word. It first converts your input into tokens, which are the fundamental units these models actually process. Think of tokens as the vocabulary building blocks the model learned during training.
Here's a concrete example. The sentence "ChatGPT is amazing!" gets tokenized by GPT-4 into roughly 5 tokens: ["Chat", "G", "PT", " is", " amazing", "!"]. Notice how "ChatGPT" splits into three tokens because it's a rare word the model didn't see often during training. Common words like "is" stay intact as single tokens.
You can test this yourself using OpenAI's tiktoken tool or Anthropic's tokenizer playground. GPT-4 uses the cl100k_base tokenizer with a vocabulary of 100,256 tokens, while Claude 3 uses a different tokenizer with approximately 100,000 tokens in its vocabulary.
Token vs Word vs Character: Why the Difference Matters
The distinction between tokens, words, and characters directly affects how much you pay and how models perform. Character-level tokenization would mean each letter is one token, making "hello" cost 5 tokens. Word-level tokenization would make "hello" cost 1 token, but then rare words like "antidisestablishmentarianism" also cost just 1 token despite being 28 characters long.
LLMs use subword tokenization to split the difference. Common words become single tokens, rare words split into multiple token fragments. This approach means GPT-4 processes English text at roughly 1.3 tokens per word on average, though this varies significantly by language and domain.
Here's why this matters for your work: if you're building prompts for AI systems that use RAG or embeddings, understanding token counts helps you fit more context into the model's window. A 4,000-word document might consume 5,200 tokens in English but 8,000+ tokens in languages like Chinese or Arabic, where tokenizers don't perform as efficiently.
How Byte Pair Encoding Works for Beginners
Byte Pair Encoding is the algorithm most modern LLMs use to learn their tokenization rules. It's simpler than it sounds: the algorithm starts with individual characters, then iteratively merges the most frequent character pairs until it reaches the desired vocabulary size.
Here's a simplified walkthrough. Imagine you're training on just three words: "low", "lower", "lowest". BPE starts with characters: ["l", "o", "w", "e", "r", "s", "t"]. It scans the training data and finds "lo" appears frequently, so it merges those into a new token "lo". Next iteration, it might merge "lo" + "w" into "low". Then "low" + "er" into "lower".
After training on billions of words, GPT-4's tokenizer learned that common sequences like " the", "ing", and "tion" should be single tokens. Less common sequences stay split. This is why "unbelievable" might tokenize as ["un", "believ", "able"] while "the" stays as one token.
The practical impact: BPE-based tokenizers handle common English text efficiently but struggle with rare proper nouns, code with unusual variable names, or languages underrepresented in training data. If you're working on AI coding assistants, this explains why models sometimes mangle unusual function names or API endpoints.
What Are Special Tokens and How ChatGPT Uses Them
Special tokens are reserved tokens that control model behavior rather than representing text content. They're invisible in your prompts but critical for how the model structures conversations and knows when to stop generating.
OpenAI's GPT models use tokens like `<|endoftext|>` to mark document boundaries and `<|im_start|>` / `<|im_end|>` to separate system messages, user inputs, and assistant responses. When you send a chat message to GPT-4, the API automatically wraps it in these special tokens to maintain conversation structure.
Claude uses a different set of special tokens but serves the same purpose. The `\n\nHuman:` and `\n\nAssistant:` prefixes you might see in Claude's API documentation are converted to special tokens that help the model distinguish between turns in the conversation.
Here's a concrete example of how special tokens affect your token count. A simple exchange like "Hello" (user) and "Hi there!" (assistant) actually consumes roughly 15 to 20 tokens once you account for the special tokens wrapping each message. This overhead adds up in long conversations, which is why ChatGPT sometimes "forgets" early parts of a chat when you hit the context limit.
How Do Tokens Work in ChatGPT and Claude APIs
Both OpenAI and Anthropic charge based on token counts, not word counts or character counts. Understanding the exact tokenization helps you estimate costs and optimize prompts.
GPT-4 Turbo costs $0.01 per 1,000 input tokens and $0.03 per 1,000 output tokens as of 2024. Claude 3 Opus costs $0.015 per 1,000 input tokens and $0.075 per 1,000 output tokens. A 1,000-word prompt typically consumes 1,300 to 1,400 tokens in English, meaning you'll pay roughly $0.013 to $0.014 per prompt on GPT-4 Turbo.
The tokenization differences between models affect these costs. The same prompt might tokenize to 1,350 tokens in GPT-4 but 1,420 tokens in Claude 3 due to different vocabulary choices. Over thousands of API calls, this 5% difference compounds.
You can check exact token counts before sending requests. Here's how to count tokens for GPT-4 using Python:
import tiktoken
encoding = tiktoken.encoding_for_model("gpt-4")
text = "Your prompt text here"
tokens = encoding.encode(text)
print(f"Token count: {len(tokens)}")
For Claude, you can use Anthropic's tokenizer library or their web-based counting tool. This pre-flight check helps you stay within context limits and budget constraints, especially when building applications that make frequent API calls.
Vocabulary Size Tradeoffs in LLMs Explained
Vocabulary size is a fundamental design choice that affects model performance, training cost, and inference speed. Larger vocabularies mean fewer tokens per text but bigger embedding matrices and more parameters to train.
GPT-2 used a vocabulary of 50,257 tokens. GPT-3 and GPT-4 expanded to roughly 100,000 tokens. This expansion reduced average tokens per word from about 1.5 to 1.3 for English text, cutting inference costs by approximately 13% for the same content. However, the larger vocabulary added millions of parameters to the model's embedding layer.
Here's the tradeoff in concrete terms: a 50,000-token vocabulary with 1,024-dimensional embeddings requires 51 million parameters just for the embedding layer. Doubling the vocabulary to 100,000 tokens doubles that to 102 million parameters. For models with billions of parameters total, this might seem small, but it affects memory bandwidth and training efficiency.
Smaller vocabularies mean more tokens per input, which increases the sequence length the model must process. Since transformer attention mechanisms scale quadratically with sequence length, longer token sequences significantly slow down inference. A vocabulary that's too small creates a bottleneck even with faster per-token processing.
Most modern LLMs settle around 100,000 tokens as a sweet spot. This size handles common English words efficiently while keeping the embedding matrix manageable. Honestly, the vocabulary size matters more for model developers than end users, but it explains why different models have different context window limits and pricing structures.
Multilingual Tokenization Challenges and Solutions
Tokenization efficiency varies dramatically across languages, creating real performance and cost disparities. English text averages 1.3 tokens per word in GPT-4, but Chinese text averages 1.7 to 2.0 tokens per character. Languages like Tamil or Thai can be even less efficient.
This happens because most LLM tokenizers train primarily on English text. BPE learns to merge common English character sequences like "th", "ing", and "tion" but doesn't see enough non-Latin scripts to learn efficient merges. The result: non-English text consumes 40 to 60% more tokens for the same semantic content.
The cost impact is direct. If you're building an application for multiple languages, the same functionality costs significantly more in Japanese than English. A 1,000-word Japanese document might consume 2,000+ tokens compared to 1,300 tokens for equivalent English content.
Some newer models address this. GPT-4's tokenizer improved multilingual efficiency compared to GPT-3 by including more diverse training data during BPE training. Models specifically designed for multilingual use, like mBERT or XLM-RoBERTa, use tokenizers trained on balanced multilingual corpora, reducing the token-per-character ratio for non-English languages.
Context Windows, Token Limits, and Practical Implications
Every LLM has a maximum context window measured in tokens, not words or characters. GPT-4 Turbo supports 128,000 tokens, Claude 3 supports 200,000 tokens. These limits include both your input prompt and the model's output combined.
Understanding tokenization helps you maximize these windows. If you're building RAG systems that retrieve documents, knowing that your 4,000-word document actually consumes 5,200 tokens helps you fit more retrieved context before hitting limits.
Token limits also explain some frustrating model behaviors. When ChatGPT suddenly "forgets" earlier conversation context, it's because the total token count exceeded the limit and the system dropped old messages. When Claude refuses to process your document, it's because the token count exceeded 200,000, even if the character count seems reasonable.
Here's a practical optimization: if you're close to token limits, you can often reduce token counts by 10 to 15% by removing unnecessary whitespace, using shorter synonyms for common phrases, or restructuring bullet points. Each space character is typically part of the next token, so "list : item" (with spaces around the colon) costs more than "list: item".
How Tokenization Affects Prompt Engineering
Knowing how tokenization works helps you write more effective prompts and troubleshoot unexpected model behavior. Some words or phrases tokenize inefficiently, causing the model to struggle with them.
For example, if you ask GPT-4 to reverse the word "strawberry", it often fails. Why? Because "strawberry" tokenizes as ["str", "awber", "ry"], and the model learned patterns at the token level, not the character level. It can't easily "see" the individual letters to reverse them.
Similarly, models struggle with counting characters in words or performing precise string manipulations because they process tokens, not characters. The word "book" is one token, so asking "how many letters are in book?" requires the model to recall memorized facts rather than count visible units.
This knowledge helps you write better prompts. Instead of asking for character-level operations directly, you can ask the model to spell out the word first: "Spell out 'strawberry' letter by letter, then reverse the order." This workaround helps the model by making the character sequence explicit in the token stream.
Tokenization also affects few-shot examples. If you're providing examples in your prompt, using words that tokenize similarly to your target task improves performance. The model learns patterns at the token level, so consistent tokenization patterns across examples help it generalize better.
Look, understanding tokenization gives you a mental model for why LLMs behave the way they do. They're not reading text the way you do. They're processing sequences of learned token units, which explains both their impressive language understanding and their occasional bizarre failures with simple tasks. When you write prompts with token-level processing in mind, you'll get more predictable and reliable results from ChatGPT, Claude, and other language models you use.
Get a free AI-powered SEO audit of your site
We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.
Run my free audit