Tokens in LLMs: Tokenization, Vocabulary & Why Tokens Aren’t Words
-
Last Updated: October 6, 2026
-
By: javahandson
-
Series

Tokens in LLMs explained simply — what a token is, how tokenization and vocabulary work, why tokens aren’t words, plus token handling in Spring AI.
Most Spring AI tutorials jump straight to prompts, models and responses. Yet they rarely stop to explain tokens in LLMs. That small gap causes big confusion later. You see a surprise bill, a “context length exceeded” error, or a strange word split, and nothing makes sense.
So in this guide, we will slow down and look at tokens properly. We will learn what a token is, how tokenization works, and why a token is not the same as a word.
This is a foundations article in our Spring AI series. In the earlier articles, we built a ChatClient and connected multiple models. Now we go one level deeper. We open the hood and watch how an LLM actually reads your text.
By the end of this guide, you will know:
Let us start from the very basics.
Let me begin with a simple picture. An LLM cannot read letters or words the way we do. It only understands numbers. So before anything happens, the model chops your text into small pieces. Each piece is a token.
A token is the basic unit of text for a language model. It can be many things:
So a token is just a chunk. Sometimes it is a whole word. Often it is only a small piece of one.
Here is the part that trips up most beginners. We think in words. The model thinks in tokens. These two do not line up one to one.
Take the word playfulish. It is not in any dictionary. Still, the model handles it with ease. It simply breaks the word into three known pieces:
So one “word” became three tokens. This is exactly why the token count is almost always higher than the word count. Because of subword splitting, the model is never stuck on new words, typos or slang. It just builds them from smaller tokens it already knows.
Let me give one more example. The word tokenization often splits into token and ization. So one word becomes two tokens. Spaces usually join the next word too, which adds to the count.
You do not need exact math every time. A rough rule helps a lot. For English, one token is about three-quarters of a word. So 100 tokens cover roughly 75 words.
Put another way, English text runs at around 1.3 tokens per word on average. Short common words are usually one token. Longer or rarer words split into two, three or more pieces.
Let me show the rule in action. Suppose your prompt holds 400 English words. That is roughly 530 tokens. Now add the model’s 200-word reply. Together you cross 800 tokens in one short exchange. So even a small chat uses more tokens than most people expect.
Keep this rule in your head. It lets you guess prompt size and cost before you even call the model.
| 💡 Interview Insight Interviewers often ask, “Is a token the same as a word?” The safe answer is no. A token can be a full word, a part of a word, a single character or a symbol. For English, one token is about three-quarters of a word. Mention the 100 tokens ≈ 75 words rule to show you truly understand it. |
A fair question comes up here. Why bother with tokens at all? Why not just use plain words, or plain characters? Both sound simpler. Sadly, both break down at scale. Let me explain why.
Language is messy, so a word-only system fails fast. Think about these cases:
To cover every possible word, the model would need a giant list. English alone would need 500,000-plus entries, and most would appear rarely. That wastes memory and still fails on anything new.
So maybe characters are safer? They are tiny and flexible. However, they create a different problem. Characters are simply too small.
To read unbelievable, the model would process twelve separate steps, one per letter. That makes training slow. It also makes meaning harder to catch. Reading one letter at a time is like reading a book through a keyhole.
This is where tokens win. A token sits nicely between a word and a character. Common words stay as one token. Rare words split into a few known pieces. Nothing is ever truly unknown.
As a result, the model gets a short and meaningful sequence. It stays efficient, flexible and robust at the same time. That balance is the whole reason tokens exist.
Here is an analogy that makes it stick. Think of Lego blocks for a moment. Words are like fully built models, rigid and finished. Characters are like single tiny studs, far too small to be useful. Tokens are the mid-size blocks in between.
With a bag of these mid-size blocks, you can build almost anything. A common shape clicks together fast. A rare shape just needs a few more blocks. This is exactly how a model builds any word from its tokens.
Now let us trace the actual journey. The process of breaking text into tokens is called tokenization. Behind the scenes, your sentence passes through a clear pipeline.
The flow has four steps, and the arrows below show the order:
Text → Tokens → Token IDs → Vectors
Here is what happens at each stage:
So the model never sees your letters. It works with numbers from start to finish. Take the sentence “I love Spring AI”. The tokenizer may split it into about four or five tokens. Each token then gets its own ID and vector.

The clever part is subword tokenization. This is what lets a model handle words it has never seen before.
Recall playfulish from earlier. The tokenizer does not panic. It splits the word into play, ful and ish. Each piece already exists in its list of known tokens. Even made-up words become easy this way.
The same trick handles typos and slang. The model simply assembles them from smaller, familiar parts. Thanks to this flexibility, LLMs feel natural with almost any input.
Different model families use different methods. The names sound fancy, but the core idea is the same. Each one learns which pieces are worth keeping as tokens.
You rarely choose this yourself. The model you pick already decides the algorithm. Even so, it helps to know these names when you read docs or sit in an interview.
Here is a subtle point worth knowing. The same word can tokenize differently based on spacing. A leading space often joins the next token. So ” token” and “token” may count as different pieces.
This is why two very similar prompts can have slightly different token counts. You do not control this split directly. The tokenizer decides it, based on how it was trained. Still, knowing this saves you from head-scratching later.
We keep saying “known tokens”. But where does this list live? That brings us to the vocabulary. This is one of the most important ideas in the whole article.
A model’s vocabulary is the full list of tokens it can recognise. Think of it as a giant table. Each row holds one token and its unique ID.
For example:
The table stays fixed after training. Every token the model knows lives inside it. When your text arrives, the tokenizer looks up each piece in this table.
Vocabulary size changes from model to model. Bigger is not always better, as we will see soon. Here are some real numbers, checked against trusted sources.
| Model | Tokenizer | Vocabulary Size |
|---|---|---|
| GPT-2 | r50k_base | ~50,000 tokens |
| GPT-4 | cl100k_base | ~100,000 tokens |
| GPT-4o | o200k_base | ~200,000 tokens |
| BERT | WordPiece | ~30,000 tokens |
| Mixtral | BPE-based | 32,000 tokens |
| Llama 3 | BPE-based | ~128,000 tokens |
| Gemini | SentencePiece | ~256,000 tokens |
So a GPT-4 model knows about 100,000 tokens. GPT-4o nearly doubled that number. Each jump changes how your text is split, and how many tokens you pay for.
This is a great question. And the answer shows why tokens are so smart. Say the vocabulary has apple, phone and table. Now the input word is tabletop. That exact word is missing.
The model still does not fail. Instead, it splits the word into known pieces:
If top was also missing, the tokenizer would split further, maybe into tab, le, to and p. In the end, every piece exists in the table. The core idea is simple. An unknown word is broken into the smallest number of known tokens.
This is why models never hit a hard wall on new words. There is no “unknown word” error at all. There is only more splitting into smaller pieces.
So why not make the vocabulary huge? There is always a trade-off. A larger vocabulary packs common words into single tokens. That means fewer tokens per sentence, and lower cost per call.
But a big vocabulary needs more memory inside the model. A small vocabulary saves memory, yet it splits text into more pieces. More pieces mean longer sequences and higher cost. So model builders pick a balance that fits their goals.
| 💡 Interview Insight A common question is, “Why does BERT use only 30,000 tokens while GPT-4 uses 100,000?” The answer is the trade-off. A full English word vocabulary would need 500,000-plus entries, with huge embedding tables and poor handling of new words. Subword vocabularies stay small while still covering any text you throw at them. |
You do not have to take these numbers on faith. You can watch tokenization happen live. OpenAI offers a free web tokenizer for this. The open-source tiktoken library does the same job in Python.
Paste any sentence and you will see it split into coloured chunks. Try your own name, a long word, or a line of code. Then switch the language to Hindi or Chinese. You will instantly see the token count jump. This small habit builds real intuition about tokens in LLMs.
Fine, tokens are interesting. But why should a Spring developer care? Understanding tokens in LLMs is not just classroom theory. It touches your cost, your limits and your results every single day.
LLM providers charge per token, not per word. They count both your input and the model’s output. So a long system prompt costs money on every request.
This adds up quickly in production. Imagine a chat app with thousands of users. A few extra tokens per prompt become a large monthly bill. Therefore, trimming prompts is a real and easy way to save money.
Every model has a context window. This is the maximum number of tokens it can handle at once. It covers your prompt, the chat history and the reply together.
Picture a whiteboard with limited space. Once it fills up, old notes get wiped to make room. In the same way, once you cross the token limit, older messages drop off. The model then “forgets” that earlier part of the chat.
Here is a point many people miss. Tokenizers are trained mostly on English. So English is cheap in tokens. Other languages are often not.
The same sentence can cost far more tokens in another language. With older GPT-4 tokenizers, Hindi could cost several times more than English. Newer tokenizers like o200k_base reduced this gap. Still, if you serve many languages, measure token usage for each one.
There is one more effect people often forget. Tokens shape latency too. A model generates text one token at a time. So more output tokens mean a longer wait.
A chatty reply is not only costly. It is also slower for your user. Therefore, a tight output limit helps on both fronts at once. Shorter answers feel snappier and cost less.
Enough theory. Let us see tokens in real Spring AI code. This ties back to our series nicely. All examples below use Spring AI 2.0.1, the current stable release.
Sometimes you want short answers. You can cap the output length with a token limit. Spring AI exposes this through ChatOptions.
var options = OpenAiChatOptions.builder()
.model("gpt-4o-mini")
.maxCompletionTokens(300)
.temperature(0.7)
.build();
String answer = chatClient.prompt()
.options(options)
.user(message)
.call()
.content();The maxCompletionTokens value limits how many tokens the model may generate. This keeps replies tight and costs predictable. For generic options across providers, you can also use ChatOptions with maxTokens.
Guessing token usage is risky. It is far better to measure it. Spring AI returns usage data inside the ChatResponse metadata. For that, use call().chatResponse() instead of call().content().
ChatResponse response = chatClient.prompt()
.user(message)
.call()
.chatResponse();
Usage usage = response.getMetadata().getUsage();
Long promptTokens = usage.getPromptTokens();
Long outputTokens = usage.getCompletionTokens();
Long totalTokens = usage.getTotalTokens();Now you can log these numbers for each request. Later, you push them to Micrometer and Prometheus. The Spring AI course even wraps this in a reusable TokenUsageAuditAdvisor for exactly this job.
Tokens also matter a lot in RAG. When you load a big document, you cannot send it whole. You split it into chunks first. Spring AI offers a TokenTextSplitter that counts tokens, not characters.
TextSplitter splitter = TokenTextSplitter.builder()
.withChunkSize(100)
.withMaxNumChunks(400)
.build();
vectorStore.add(splitter.split(documents));Here each chunk is about 100 tokens. That keeps every chunk small enough for the model. One nice detail: Spring AI 2.0.1 added stricter validation on the TokenTextSplitter builder arguments. So bad values now fail early, instead of causing silent bugs later.
Chat memory and tokens are close friends. Memory stores past messages. But the context window limits how many of them actually fit. So you cannot keep everything forever.
By default, Spring AI keeps only the last several messages. For very long chats, that is safer than sending the whole history. For large histories, a vector-store memory picks only the relevant past messages. This way, you respect the token limit and the user still feels remembered.
| 💡 Interview Insight Expect a question like, “How do you stop a chat app from blowing past the context window?” Strong answers include capping output with maxTokens, limiting stored messages in chat memory, using a vector-store memory for long histories, and logging token usage per request so you catch growth early. |
Let me share a few simple lessons. These will save you both money and debugging time. None of them are hard, yet they are easy to miss.
A: A token is the basic unit of text an LLM reads. It can be a whole word, a part of a word, a single character, or a symbol. The model turns every token into a number before it processes your text.
A: No. Tokens and words do not line up one to one. For English, one token is about three-quarters of a word, so 100 tokens cover roughly 75 words. Long or rare words split into several tokens.
A: For English, about 1,300 tokens, since English runs at roughly 1.3 tokens per word. The exact count depends on the model’s tokenizer and the language. Non-English text usually costs more tokens for the same meaning.
A: A vocabulary is the fixed list of tokens a model can recognise, each with a unique ID. GPT-4 has about 100,000 tokens, GPT-4o about 200,000, and BERT about 30,000. Unknown words are split into smaller known tokens.
A: Pure words need a huge vocabulary and still fail on new words, while pure characters make sequences too long. Tokens sit in between, so common words stay as one piece and rare words split into a few known tokens.
A: Cap output with maxCompletionTokens on ChatOptions, and read usage from the ChatResponse metadata with getPromptTokens(), getCompletionTokens() and getTotalTokens(). For documents, chunk with TokenTextSplitter. All work in Spring AI 2.0.1.
Tokens sit at the very heart of every LLM. Once you understand tokens in LLMs, a lot of AI stops feeling like magic. You can see why bills grow, why context runs out, and why strange word splits happen.
Here is a quick recap:
Start by counting tokens in your own prompts. Then watch your usage for a week. Small changes here add up to real savings over time.