How LLM Tokens Work
How LLM tokens work, for beginners, shown on a real model running on a laptop: why a small model says strawberry has two r's (it reads tokens, not letters), what a token and a tokenizer are, Llama 3.2's 128,256-piece vocabulary, how byte pair encoding builds the pieces (a tiny training run), how to count tokens (a 1,108-word guide is 1,301 tokens; about four characters a token in English), why other languages cost more tokens, why each model family counts differently, how a model writes one token at a time, what tokens cost, and how Ollama's default 4,096-token context window quietly cut a long file.

Two R's in strawberry?
You ask a small AI model how many r's are in strawberry, and it says two. There are three.
ask "How many r's are in strawberry?"There are 2 R's in the word "strawberry". tokens in: 33 tokens out: 15 (41 tokens a second)
It never saw the letters. It saw tokens, numbered pieces of text. In your question, strawberry was one single number, with no letter r in it.
tokens "How many r's are in strawberry?"[How][·many][·r]['s][·are][·in][·strawberry][?] 8 tokens: 4438 1690 436 596 527 304 73700 30
A model reads, writes, and charges you in tokens. You'll count them yourself, and catch a hidden setting that made a model answer wrong, with no error.

What a token is
A program called a tokenizer cuts your text into those pieces. It uses a fixed list, and swaps each piece for its number on that list

Here's the tokenizer of Llama 3.2, an open model from Meta. It's the same one the model on this laptop uses. Let's feed it the sentence, Tokens are not words. That comes out as five tokens: four words and the full stop. In the output, each dot stands for a space. Words usually come after a space, so the list stores common words with their space in front. That's why strawberry alone is three tokens, but with a space in front, it's one.
tokens "Tokens are not words."[Tokens][·are][·not][·words][.] 5 tokens: 30400 527 539 4339 13
tokens strawberry[str][aw][berry] 3 tokens: 496 675 15717
tokens " strawberry"[·strawberry] 1 token: 73700
Think of a phrasebook with numbered entries. A common word has an entry of its own. A rarer word is built from smaller ones. And the model is only ever handed the entry numbers.
Llama 3.2's phrasebook has 128,256 entries. Nearly all of them were learned from text. The last 256 are special markers, like one that marks the start of a text. Search the list for berry, and 10 entries hold it. A capital letter, or a space in front, makes a different entry, with its own number
vocabllama3.2:3b: 128,256 pieces (128,000 learned, 256 special) 0 [!] 1 ["] 1000 [indow] 10000 [.grid] 100000 [IClient]
vocab berry10 pieces contain "berry" (the first 8): 15717 [berry] 37062 [aspberry] 44685 [·Berry] 48665 [·Raspberry] 51302 [Berry] 64528 [·BlackBerry] 73700 [·strawberry] 86188 [·berry]
Where the pieces come from
Why pieces, and not letters, or whole words? Letters would make every text several times longer: you'll measure exactly how much in a moment. Whole words would need an entry for every word, name and typo ever written. Pieces sit in between: common words whole, everything else spelled from parts

So who picked these pieces? A training run. Here's a tiny one, on sixteen words. Start from single letters. Each round, count every pair of pieces that sit side by side, and join the most common pair into one new piece. Then do it again. Here are the first four rounds. The letters e and s are the most common pair, so they become one piece. Then that piece joins t. Then l joins o, and that joins w, to make low. It's called byte pair encoding.
cat words.txtlow low low low low lower lower newest newest newest newest newest newest widest widest widest
train words.txt 616 words, 4 different: low x5, lower x2, newest x6, widest x3 merge 1: e + s -> es (seen 9 times) merge 2: es + t -> est (seen 9 times) merge 3: l + o -> lo (seen 7 times) merge 4: lo + w -> low (seen 7 times) merge 5: n + e -> ne (seen 6 times) merge 6: ne + w -> new (seen 6 times) now: low low e r new est w i d est
Real tokenizers do the same over a huge pile of text, about a hundred and twenty-eight thousand times. Each join adds one new entry, and that's how the phrasebook grew to its hundred and twenty-eight thousand entries. Common words end up as one piece. Rare words stay in parts: unbelievability comes out as four pieces, starting with un, and ending with ability.

Numbers get cut too. Llama 3.2 splits digits into groups of up to three, from the left. So one million, two hundred thirty-four thousand, five hundred sixty-seven reaches the model as three tokens: 123, 456, and 7. So, as with the r's in strawberry, the model doesn't see the digits one by one.
tokens 1234567[123][456][7] 3 tokens: 4513 10961 22
Counting tokens
Every token costs you time, money, and room in the model's memory. So how many is your text? Here's a beginner's guide to backups: 1,108 words. Guess first: how many tokens does it have? 1,301 tokens. That's more tokens than words. Each token is about four and a half characters, close to the usual rule of thumb: four characters a token, for English. Spelled out letter by letter, the guide would be four and a half times longer.
count backups.txtwords: 1,108 characters: 5,870 tokens: 1,301 (Llama 3.2) 1,299 (GPT-4o) 4.5 characters a token, 1.17 tokens a word (Llama 3.2)
To count exactly, use the tokenizer of the model you'll send it to. OpenAI publishes theirs as a library, and open models carry theirs inside the model file. Or read the count that comes back with every answer: when the guide went to the model, it reported the same thirteen hundred and one

Tokens are also what you pay for. For Claude Sonnet 4.6, Anthropic charges three dollars per million tokens in, and fifteen per million out. So sending this guide once costs about four tenths of a cent, and each token the model writes back costs five times as much

Not every text costs the same
But four characters a token is a rule for English. Here's one question, "Where is the nearest train station?", in four languages. Each row shows the characters, then Llama's token count. We'll come back to the last column. English: seven tokens. Spanish: fourteen. Hindi: twenty-one, three times as many. Japanese: nine tokens for just twelve characters, nearly one token per character. These pieces were learned mostly from English. So other languages get cut into smaller pieces, and the same question costs more.
cat languages.tsvEnglish Where is the nearest train station? Spanish ¿Dónde está la estación de tren más cercana? Hindi सबसे नज़दीकी रेलवे स्टेशन कहाँ है? Japanese 一番近い駅はどこですか?
languages languages.tsvchars Llama GPT-4o English 35 7 7 Spanish 44 14 10 Hindi 34 21 11 Japanese 12 9 11
And every model family has its own phrasebook. GPT-4o's tokenizer cuts strawberry differently: st, raw, berry. Our five-token sentence gets the same pieces, but different numbers. On the same four-language file, it counts the Hindi question in eleven tokens, not twenty-one. So a count from one model is only a guess for another.
tokens --gpt4o strawberry[st][raw][berry] 3 tokens: 302 1618 19772
tokens --gpt4o "Tokens are not words."[Tokens][·are][·not][·words][.] 5 tokens: 30325 553 625 6391 13
Tokens in, tokens out
Ever noticed an answer shows up a few words at a time? That's the model writing tokens. Ask it what a token is, and it arrives one piece at a time. The count that comes back is a little higher than the pieces you see, because of hidden special markers, like the one that ends the answer.
ask --pieces "What is a token in an AI model? Answer in one sentence."In| an| AI| model|,| a| token| is| a| small| unit| of| text|,| such| as| a| word| or| sub|word|,| that| is| used| to| represent| and| process| individual| elements| of| input| data|,| such| as| text| or| speech|,| during| tasks| like| language| translation|,| sentiment| analysis|,| or| text| classification|. (54 pieces arrived one at a time) tokens in: 39 tokens out: 55 (35 tokens a second)
Here's why it comes one at a time. Your text goes through the tokenizer, and its numbers go into the model's view, called the context window. The model picks the most likely next token, adds it to the window, and runs again. And again. On this laptop, about thirty-five times a second. That's why a long answer takes longer than a short one: every token out is one more run of the model.
The window is counted in tokens
Here's the hidden setting from the start. The model runs in a program called Ollama, on a small server next to this laptop. There, ollama show prints a model's settings: Llama 3.2 itself can take 131,072 tokens. But Ollama starts every model at 4,096, unless you ask for more.
ollama show llama3.2:3b | grep -i contextcontext length 131072
Everything shares that window: your question, any file you paste in, the chat so far, and the answer it's writing. In a chat, the whole conversation is sent again with every message, so a long chat fills the window too. And when it's full, something has to go.
So take a longer file. Its first line is a note: the label on the backup drive says BLUE-7. Then the backup guide, four times over: 5,219 tokens. Ask the model what the label says. Guess first: what will it say? It says the label reads three-two-one, a phrase it took from the guide. That's wrong. Tokens in: 2,050, not 5,219. Even less than the 4,096 window, because Ollama keeps part of it free for the answer.
head -1 long.txtBefore you start: the label on the backup drive says BLUE-7.
count long.txtwords: 4,443 characters: 23,542 tokens: 5,219 (Llama 3.2) 5,211 (GPT-4o) 4.5 characters a token, 1.17 tokens a word (Llama 3.2)
ask --file long.txt "What does the label on the backup drive say?"The label on the backup drive says "3-2-1". tokens in: 2,050 tokens out: 15 (24 tokens a second)
On the server, its log says why: truncating input prompt. It kept only the last 2,050 tokens, and dropped everything before them, the note included. No error reached you. Just a confident wrong answer
grep -o 'msg=.truncating input prompt.*' /var/log/ollama.log | tail -1msg="truncating input prompt" limit=2050 prompt=5254 keep=4 new=2050
The fix is one setting: a bigger window. Ask again with dash dash ctx, short for context, set to 8,192 tokens. Tokens in: 5,254. That's the whole file, plus the question and a few hidden markers. And the answer: BLUE-7. So count your tokens first, and give the window room for all of them, plus the answer.
ask --ctx 8192 --file long.txt "What does the label on the backup drive say?"The label on the backup drive says "BLUE-7". tokens in: 5,254 tokens out: 13 (6 tokens a second)
Wrap-up
So: the model said two r's because it never saw letters. A token is a numbered piece of text. Models read, write and bill in tokens. And if your text won't fit, the start is dropped silently.

If you'll count your tokens before you trust a long answer, subscribe, and a like helps YouTube show this to more beginners.
The lab files and every command are linked in the description, so you can run the same tokenizer on your own text. Thanks for watching Cloud Explainers.
Sources
Demos: a small AI model running on our own laptop in Docker, no account or key. Every command, count and answer was real. Watch the full video on YouTube.