The same word costs different numbers of tokens because BPE (byte pair encoding) doesn't tokenize words — it tokenizes the entire string as one sequence, merging adjacent character pairs according to a fixed, learned priority ranking. Surrounding characters compete for those merges: a space, a punctuation mark, or a common neighboring word can win a merge that the word would otherwise have won alone, shifting its internal boundaries. The word is the same, but the sequence it sits in is not.
What BPE Tokenization Actually Decides
Byte pair encoding is a compression algorithm adapted for language models. Starting from a vocabulary of individual bytes (the raw 0–255 values that make up any UTF-8 text), a trainer repeatedly finds the most frequent adjacent pair of symbols in a large corpus and adds that merged pair as a new token. After thousands of rounds, the result is a ranked list of merge rules: the first rule is applied first, the last rule last. At inference time, the tokenizer takes your exact input string, converts it to bytes, and applies those merge rules in order until no rule applies.
The key point is that BPE never receives a "word" as input. It receives a flat sequence, usually with spaces encoded as visible marker characters (many tokenizers represent a leading space as a special character such as Ġ so that "word" and " word" are distinguishable). Every token boundary you see in the output is the residue of merge decisions made over that flat sequence. A token, then, is just a substring that happened to survive the merge process intact.
Two consequences follow. First, tokenization is deterministic: the same string always yields the same tokens for a given tokenizer. Second, tokenization is not compositional: the tokens for "a" plus the tokens for "b" do not generally equal the tokens for "ab", because the concatenated string offers merge opportunities that neither part had alone. There is no separate "word segmentation" stage that BPE respects. It is a greedy-ish, rule-ordered process over characters, and words emerge as a side effect.
Why Context Changes Merge Order and Boundaries
Merge rules are applied in a fixed global order, and each application can consume characters that a neighboring merge would otherwise have used. Consider a simplified set of learned rules where the pair th merges before he, and he merges before er. On its own, "the" might resolve as th + e. But prepend a space and follow it with a common continuation, and the sequence becomes ␣the... — now the pairs ␣t, th, he, and possibly e␣ are all in play, and whichever rules rank highest fire first, potentially yielding a single ␣the token or splitting the word differently than it would stand alone.
Three forces drive the differences:
- Left context (usually a space or punctuation). A leading space creates new candidate pairs like
␣t. If the tokenizer learned a frequent token beginning with a space, the word may attach to that space and consume its first letters, leaving fewer characters for the word's own internal merges. - Right context (punctuation or the next word). A following comma or period introduces pairs like
e,ord.. If such a pair outranks the word's internal pairs, the final letter may be pulled into a punctuation token, e.g.word+,becoming something likeword,as one token orwor+d,depending on the ranking. - Case and character composition. An unusual character (a digit, an accent, a rare letter) breaks the chain of high-frequency merges, so the rest of the word falls back to shorter tokens. Context that supplies common neighbors can partially "rescue" merges or, conversely, steal them.
Because merge order is global and greedy per rule application, a merge that fires early in one context can block a merge that would have fired later — the classic reason a word fragments differently inside a sentence than in isolation. This is not a bug; it is the mechanism working as designed.
Note on numbers: the exact token counts below are illustrative and depend on the specific tokenizer; treat them as a reasoned example, not a benchmark. In most modern BPE tokenizers, a short common English word in isolation is roughly 1–3 tokens, and the same word embedded in a sentence can plausibly shift by ±1 token depending on spacing and punctuation — an estimate based on the merge mechanics described, not a measured statistic.
Worked Example: One Word Alone vs. Inside a Sentence
Take the word playing. Assume a BPE vocabulary (the fixed list of tokens a model can emit) that contains these relevant pieces: play, ing, play-ing is not assumed — instead assume the trained merge rules favor play + ing when play sits at the start of a string, but favor pla + ying when the preceding character is a space and the following context pulls y toward a common ying merge. These are stated assumptions, not measured facts; real vocabularies differ.
Isolated string: playing
- No leading space, so the tokenizer starts fresh at
p. - Greedy longest-match picks
play(4 chars), thening(3 chars). - Result:
play+ing= 2 tokens.
Inside a sentence: She is playing now
- The substring is
playing(leading space included — most tokenizers attach the space to the following token). - Assume the space-plus-context shifts the merge:
pla+ying. - Result:
pla+ying= 2 tokens here, but note the boundaries moved:play|ingbecamepla|ying.
Now a case where the count changes. Assume the vocabulary lacks pla, so the space forces a split: + pla + ying = 3 tokens, versus 2 in isolation. Arithmetic check: 1 (space) + 1 (pla) + 1 (ying) = 3. The word is identical; only its neighbors changed, yet the count rose by one.
Reading the Token-Counter Output Like a Pro
A token counter shows the segmented string and the token count. Read it in this order:
- Locate the target word's boundaries. Find where the word starts and ends in the token list; don't just count total tokens.
- Check the leading character. Is the space its own token, or glued to the first piece? This alone explains many count differences.
- Compare boundary positions, not just totals. Equal counts can hide different splits (
play|ingvspla|ying). - Watch punctuation and casing. A trailing period or capital letter often creates an extra token.
Quick decision formula:
delta = tokens_in_context - tokens_isolated
if delta == 0 and boundaries differ -> merge shift, same cost
if delta > 0 -> context forced a split (space, punctuation, rare neighbor)
if delta < 0 -> context enabled a longer merge
Reproduce it yourself: paste the bare word, note the count and boundaries; paste the sentence, find the same word, compare. Change one neighbor at a time (add a space, add a comma, lowercase it) and re-check. Each change isolates one cause, turning the counter into a controlled experiment rather than a black box.
Frequently Asked Questions
Why does the same word produce different tokens in different sentences?
Because tokenization is not a word-by-word dictionary lookup; it runs once over the entire string. Byte Pair Encoding (BPE) starts from raw characters and repeatedly merges the most frequent adjacent pair according to a fixed merge list learned during training. A word like "playing" may keep a clean split such as play + ing in one sentence, but when the characters around it form a more strongly learned pair — say, a letter plus the following space, or a hyphen joining two fragments — those merges get claimed first, and the leftover pieces of the word are cut differently. The word itself is unchanged; its segmentation is a side effect of what the surrounding text lets the merge process do first.
Does BPE tokenize words or the whole string at once?
The whole string at once. There is no separate "word mode": the tokenizer treats the input as one continuous sequence of bytes or characters, including spaces, punctuation, and newlines, and applies merges across the entire sequence in priority order. Word boundaries only matter indirectly, because spaces are themselves characters that participate in merges, which is why the space before a word often ends up attached to its first token. Because the process is global, a merge decision in one part of the text can change the merge order available elsewhere, and therefore the final boundaries of an individual word.
How does surrounding text change which merges win?
BPE applies merges by rank: pairs that were merged earlier (more frequently) during training are applied before lower-ranked pairs, and each application rewrites the sequence the next merge sees. If the text around a word creates a high-ranked pair that overlaps the word's own characters, that pair consumes the characters before the word can be split along its usual lines. Conversely, unusual neighbors can block a familiar merge, forcing the tokenizer to fall back on shorter pieces. The result is that the same substring can be swallowed into a longer token, split into smaller ones, or shifted by one character depending on what sits immediately before and after it.
Why do leading spaces and punctuation affect token counts?
Spaces, punctuation, and capitalization are ordinary characters in the input, not invisible delimiters, so they compete for merges like anything else. A leading space is frequently merged with the word's first letters into a single token, which changes the raw pieces even when the visible word is identical; punctuation can either attach to the preceding token or break a merge that would otherwise have formed. A token-count estimate assumes a rough ratio of characters to tokens, but that ratio shifts with every such boundary effect, so the same word in different positions can honestly cost a different number of tokens. This is why token counts for short strings are especially sensitive to spacing and punctuation, and why exact counts should be checked rather than assumed.
Can I reproduce these differences with the token-counter tool?
Yes, and the direct comparison is the fastest way to see the mechanism. Tokenize a single word alone, then paste the same word inside a full sentence and compare the highlighted token boundaries for that word; the pieces will often differ in count and in where the cuts fall. Try variants with a leading space, trailing punctuation, and a capitalized first letter to isolate which neighbor caused the change, and keep the sentence otherwise identical so only one variable moves at a time. Because merge behavior depends on the tokenizer's training data rather than on any universal rule, results can vary between tokenizer versions, so compare outputs from the same tool and model when drawing conclusions.