Skip to content
GenAI Learn/Tokens, Embeddings & Memory
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

How LLMs See Text: Tokenization

7 min read

You'll learn to

  • -Understand how text is broken into tokens before an LLM ever sees it
  • -Trace Byte-Pair Encoding merging characters into subword tokens
  • -Explain why token count, not character or word count, drives both cost and context limits

An LLM never sees words, sentences, or characters the way you do. Before a single number reaches the model, every input is converted into a sequence of tokens, integer IDs pointing into a fixed vocabulary of text pieces the model was trained on. Getting this right matters immediately and practically: token count is what a context window actually limits, and token count is what you are billed for, not characters, not words.

Why Not Just Use Characters, or Whole Words?

Both extremes have a real problem. Tokenizing by individual character keeps the vocabulary tiny, but it makes sequences enormous: a 500-word paragraph becomes thousands of character tokens, and the model has to work far harder to reconstruct meaning from such a fine-grained stream. Tokenizing by whole word keeps sequences short, but the vocabulary explodes, every inflection, typo, and made-up word needs its own slot, and any word never seen during training becomes an unrecoverable gap. Byte-Pair Encoding (BPE), the algorithm behind GPT's tokenizer and close relatives behind most other major LLMs, is the practical middle ground.

Byte-Pair Encoding, Traced by Hand

BPE starts every word as a sequence of individual characters, then repeatedly finds the most frequent adjacent pair of pieces across the whole training corpus and merges it into a single new piece. Repeat that thousands of times and you end up with a vocabulary where common whole words are single tokens, common sub-word chunks (like "-ing" or "-tion") are single tokens, and anything genuinely novel falls back to smaller, still-recognizable pieces instead of an unrecoverable gap. Run it yourself on a tiny five-word corpus and watch the exact same merging behavior a real tokenizer's training run uses, just at a scale you can trace by eye.

A minimal BPE trainer, run on five words. Watch "low" and "lower" and "newer" converge on a shared "er" subword.

Running this prints exactly five merges: ('l','o') because "low" appears in three of the five words, then ('lo','w') to form the whole piece "low", then ('e','r') because "er" shows up in "lower", "newer", and "wider", then ('er','_') to close it off as a full suffix, and finally ('low','_') closing off the standalone word. After just five merges, "lower" is already down to two pieces, ['low', 'er_'], sharing its "low" piece with "lowest" and its "er_" piece with "newer" and "wider". A real tokenizer runs this exact process tens of thousands of times over billions of words, which is exactly why common words like "the" or "running" usually end up as one or two tokens, while a rare, made-up, or foreign word gets split into more, smaller pieces, never an unrecoverable gap.

Type Something, Watch It Get Tokenized

A simplified illustration of the idea: short, common words usually stay whole. Longer or rarer words split into pieces.

Theweathertodayisnice.

characters

26

tokens

7

est. cost

$0.00004

Cost shown at an illustrative $0.005 / 1K tokens. Real per-token pricing varies by model and provider, and input/output tokens are usually priced differently.

Context Windows: The Hard Ceiling

A model's context window is the maximum number of tokens, input and output combined, it can process in a single call. Exceed it and the call fails outright, or (worse, and far more common as a production bug) the application silently truncates the oldest part of the conversation to fit, which is exactly the "chatbot randomly cuts off mid-sentence" bug that shows up constantly in real production systems, and is the exact scenario the first GenAI Lab level puts in front of you.

Context Window Sizes, Roughly (Late-Model Generation)
~128K tokens
Now closer to a budget/legacy tier than a flagship limit
~200K-500K tokens
Where current-generation flagship models typically sit
1M+ tokens
The newest long-context models, growing fast
~750 words
Roughly what 1,000 tokens holds in plain English prose

Why This Drives Cost, Not Just Limits

Every major provider bills per token, and almost universally, output tokens cost noticeably more than input tokens, often three to five times as much, because generating a token requires a full forward pass through the model while the input can be processed in a more parallelizable batch. This asymmetry matters for design decisions you will make constantly in this phase: a system that asks a model to return a long, verbose explanation costs meaningfully more than one engineered to return a short, structured answer, even if the input prompt is identical in both cases.

A subtle, very real bug: conversation history is not free just because it "already happened." Every previous turn you resend as context gets re-tokenized and re-billed on every single subsequent call. A ten-turn conversation with no memory management doesn't cost ten times one turn, it costs roughly one plus two plus three all the way up to ten, since each new call resends everything before it. That compounding cost is exactly why the next chapter exists.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

ScaleDojo Logo
Initializing ScaleDojo