AI Learning Hub
beginner

Tokens & tokenization

The "chunks" an LLM is billed by. Common words are one chunk; rare words are several.

What's actually happening

A small program called the tokenizer runs before the model sees anything. It uses BPE, an algorithm trained on a huge text corpus to find which character sequences appear together often enough to deserve a single ID.

Every prompt goes through this pipeline before the model sees a single thing.

A few non-obvious consequences of how that lookup works:

  • Spaces matter. " the" (with leading space) and "the" are two different tokens with two different IDs. This is why where punctuation goes in a prompt matters.
  • Misspellings cost more. A typo turns a common word into a rare string the tokenizer has to spell out byte by byte.
  • Non-English text is denser. Languages with less training data get split into more tokens per character.
  • The vocabulary is finite — typically ~100,000 distinct tokens. Anything not in the table is assembled from smaller pieces.
Characters: 44Tokens: 10Chars / token: 4.40Tokenizer: cl100k_base (GPT-4 / GPT-3.5)
The quick brown fox jumps over the lazy dog.
Show token IDs
[791, 4062, 14198, 39935, 35308, 927, 279, 16053, 5679, 13]

Where it shows up in real life

common word — 1 brick"the"1 tokenrare word — assembled from smaller bricks"straw""berry"·3 tokensThe tokenizer's "vocabulary" is the box of pre-built bricks (~100,000 of them).Anything not in the box gets assembled from smaller pieces.
BPE tokenization, visualised as LEGO bricks.

Under the hood (optional)

Counting tokens before sending a request is how you avoid surprise bills. ~6 lines of Python with tiktoken. Skip if you don't code — the takeaway is "count before you send."

Show the example code (Python, ~6 lines)click to expand
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o")
 
ids = enc.encode("Hello, strawberries!")
# → [9906, 11, 76050, 0]   (4 tokens, not 3 words)
 
print(len(ids))                                # 4
print([enc.decode([i]) for i in ids])
# → ['Hello', ',', ' strawberries', '!']

For Claude, Anthropic exposes a token-counting endpoint that matches what they bill you.

Try it yourself (no coding, ~2 minutes)

  1. Use the interactive demo above. Type your full name, the longest English word you know, a sentence in another language, an emoji.
  2. Watch how each splits differently — and how your bill would scale.
  3. For three tokenizers side-by-side: tiktokenizer.vercel.app.

Check your understanding

  1. 1. Roughly how many English words fit in 1,000 tokens?
  2. 2. Why does ' hello' (with a space) tokenize differently from 'hello'?
  3. 3. 800 input tokens + 200 output tokens, billed at $5/M input + $15/M output. Cost per call?

Found this useful? Share it with someone learning AI.

Further reading

Related lessons in this track