LLMsLoading progress…

Token budgets and limitations

Trace a toy tokenizer and explain byte-pair merges, vocabulary IDs and token-budget limits.

Budgets are in tokens, not words

Limits and prices are counted in tokens. A common word may be a single token while a rare word is several, so the same number of words can cost very different amounts. Tokenization also shapes what the model can directly see: it reads pieces, not letters.

token budget5
wordpieces the model seestokensfits?
thethe1yes
strawberrystrawberry3yes
tokenizationtokenization2cut
isis1cut
funfun1cut
5 words = 8 tokens. With a budget of 5 tokens, 2 whole words fit.

"strawberry" has 3 letter r's, but the model reads str | aw | berry; two pieces contain an r and none says how many. Tokenization is one reason letter counting is hard for models. It is not the only one.

A toy tokenizer table. Move the budget and see which words fit.

Predict first. Does a token budget guarantee a fixed number of visible words?

Chapter context

A language model receives token IDs, not visible words. The tokenizer chooses how text becomes those IDs. Different tokenizers can represent the same string with different lengths, so character count is not a universal token count.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

In a toy merge, replacing m nonoverlapping adjacent pairs by one symbol reduces sequence length by m. A request budget must include formatted input plus requested output within the model's supported context; exact accounting is model-specific.

Worked example

Start with toy symbols ['a','b','a','b'] and a vocabulary containing a and b. The pair (a,b) occurs twice. A merge creates symbol ab and encodes the sequence as ['ab','ab'], reducing length from 4 to 2. This deliberately omits byte encoding and pre-tokenization so the merge operation is visible.

Reference code

The following example uses the Python standard library and does not call a remote service.

tokens = list('abab')
merged=[]
i=0
while i < len(tokens):
    if tokens[i:i+2] == ['a','b']:
        merged.append('ab'); i += 2
    else:
        merged.append(tokens[i]); i += 1
assert merged == ['ab','ab']
assert len(tokens)-len(merged) == 2

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A token is not necessarily a word. A vocabulary ID is a label, not a magnitude. A hand-built toy merge sequence is not a drop-in replacement for a production tokenizer.