Glossary

LLM tokenization explained for engineers

LLM tokenization is the first thing that happens to your prompt inside a large language model. The model cannot read words, so a tokenizer splits text into small pieces called tokens and maps each one to a number. Those tokens decide what you pay, how much fits in the context window and some surprising quirks in how models behave. Here is how it works and what it means when you build with LLMs.

Get help with your AI build

What is a token?

A token is a chunk of text the model treats as a single unit. It can be a whole word, part of a word, a single character or a piece of punctuation. A common word might be one token, while a longer or rarer word is split into several pieces.

In Inside the Black Box: The Architecture of LLMs I used the word building as an example: a tokenizer might split it into build and ing, each with its own ID in the model's vocabulary. The text string becomes an array of integers, and from that point on the model only works with numbers.

How tokenization works, step by step

  1. Split the text. The tokenizer breaks the input into pieces using rules it learned from a large body of text. Most modern tokenizers work at the subword level, so frequent fragments get their own token and rare words are assembled from smaller parts.
  2. Map to IDs. Each piece is looked up in a fixed vocabulary and replaced with its integer ID.
  3. Embed. Each ID is turned into an embedding, a vector of numbers that places the token in a space where related meanings sit close together.
  4. Process. The Transformer's attention layers look across all the tokens in the sequence to work out which ones matter for each other.
  5. Predict and decode. The model predicts the next token, appends it and repeats until it reaches a stop token. The output IDs are then decoded back into text.

Subword tokenization is a practical compromise. A vocabulary of whole words would be enormous and still miss new words, names and typos. Single characters would make every sequence very long. Subwords keep the vocabulary manageable while still covering any input.

Why LLM tokenization matters when you build AI products

Cost

Providers bill by the token, typically counting both the tokens you send and the tokens the model generates. Long system prompts, large retrieved documents and verbose output all add up on every request. So when you look at the cost of an AI feature, a good first check is how many tokens each call actually carries and whether all of them earn their place.

Context window

The context window is the maximum number of tokens, input plus output, the model can hold in its working memory at once. When a conversation or document exceeds it, the earliest content drops out of view and the model effectively forgets it. This is why long chats drift and why assistants lose track of earlier instructions.

Latency

Output is generated one token at a time, so longer answers take longer. Streaming, where tokens are sent to the user as they are produced, makes that wait feel much shorter. My Assistants API to Responses API migration guide shows how to stream output text deltas to a UI.

Behaviour

Because models see tokens rather than letters, some tasks that look trivial to people can trip them up, such as counting characters in a word or handling unusual spellings. Different models use different tokenizers, so the same text can produce a different number of tokens depending on the model, and languages or formats that were less common in training data often split into more tokens.

Tokenization, embeddings and RAG

LLM tokenization is closely tied to retrieval augmented generation. In a RAG system you split documents into chunks, turn them into embeddings and retrieve the most relevant ones at query time. Token budgets shape every step: how large each chunk can be, how many chunks fit into the prompt alongside the question and instructions, and how much room is left for the answer. Getting this balance right is a large part of RAG chatbot development, and it applies to any retrieval system that answers questions over company data.

Common misunderstandings

  • "A token is a word." Often it is not. Long, rare or made-up words split into several tokens.
  • "The context window is only for my prompt." It holds the input and the output together. A long prompt leaves less room for the answer.
  • "The model learns from what I send." Not during inference. The weights are frozen while the model answers.
  • "Token counts are the same everywhere." Each model family has its own tokenizer, so the same text can count differently.

Practical tips for working with tokens

  • Measure, do not guess. Use the tokenizer for your chosen model to count tokens on real prompts. Interactive tools such as the OpenAI Tokenizer show exactly how a piece of text is split.
  • Trim the system prompt. Instructions sent on every call are a fixed cost, so keep them tight.
  • Retrieve less, retrieve better. A few well-chosen chunks beat a large dump of loosely related text.
  • Cap output length where long answers are not useful.
  • Summarise long histories instead of resending every past message.
  • Set temperature deliberately. Temperature controls how the model picks among likely next tokens; low values suit code and maths, higher values add variety.

Understanding the path from tokenization to embeddings, attention and prediction helps you see why prompts fail and how to structure data for better answers. If you are building an AI feature and want a second opinion on cost or context design, contact me.

Frequently asked questions

Is a token the same as a word?

Not always. A token can be a whole word, part of a word, a character or punctuation. Common words are often a single token, while long or rare words are split into several.

Why do AI providers charge per token?

Tokens are the unit the model actually processes. Every token in the input and every token generated costs compute, so billing by token reflects the work done on each request.

What happens when I exceed the context window?

The model cannot hold more tokens than its window allows. Depending on the tool, the request is rejected or the oldest content is dropped, and anything outside the window is effectively forgotten by the model.

Do LLMs learn from the tokens I send them?

Not during inference. The model's weights are frozen when it answers your request, so it does not update its knowledge from that interaction. Whether a provider stores data for future training depends on its terms and your agreement with it.

Let's talk about your project

Software Architect, AI Native. Google Certified Generative AI Leader with 14+ years of experience.

Get help with your AI build