GENERATIVE AI • NLP • LARGE LANGUAGE MODELS

Tokenization Explained: How LLMs Process Text

Understand how Large Language Models convert human language into tokens, token IDs, embeddings, and numerical representations that neural networks can process. Explore BPE, WordPiece, Unigram tokenization, special tokens, context windows, multilingual text, and practical tokenization examples.

🔤 Tokenization 🧠 LLMs ⚙️ NLP 🔢 Token IDs
By Affordable AI

What Is Tokenization?

Tokenization is one of the first important steps in the processing pipeline of a Large Language Model (LLM). Before a model can perform tasks such as text generation, question answering, summarization, translation, or code generation, human-readable text must be converted into a numerical form that a neural network can process.

Instead of directly processing complete words or raw characters, modern language models generally divide text into smaller units called tokens. These tokens are then mapped to numerical token IDs and subsequently transformed into vectors through an embedding layer.

From Human Text to an LLM

A simplified LLM input pipeline can be represented as:

Raw Text   →   Normalization   →   Tokenization   →   Token IDs   →   Embeddings   →   Transformer

Simple Tokenization Example

Consider the sentence:

"AI is changing technology."

A tokenizer might divide the sentence into units similar to:

AI is changing technology .

However, modern subword tokenizers may split words differently. For example, a less frequent word may be represented using multiple subword tokens rather than requiring the entire word to exist in the vocabulary.

Why Don't LLMs Simply Process Complete Words?

Using complete words as the only vocabulary units creates several problems. Natural language contains an enormous number of words, names, technical terms, spelling variations, prefixes, suffixes, compound words, and newly created terms.

Huge Vocabulary

A word-level vocabulary can become extremely large, increasing memory and computational requirements.

Rare Words

Uncommon names, technical terms, and newly created words may not appear as individual vocabulary entries.

Out-of-Vocabulary Problem

Subword tokenization helps models represent words that were not explicitly stored as complete vocabulary entries.

Why Subword Tokenization Is Important

Most modern LLM tokenization approaches operate around subword units. A frequently occurring word may be represented by one token, while a rare or complex word can be decomposed into multiple smaller pieces.

Conceptual Example

unbelievable

un + believe + able

The exact tokenization depends on the tokenizer and vocabulary being used. The important idea is that reusable subword pieces can represent many different words efficiently.

Byte Pair Encoding (BPE)

Byte Pair Encoding, commonly called BPE, is a widely used subword tokenization strategy. At a high level, BPE starts with small units and repeatedly merges frequently occurring adjacent units to create larger tokens.

Initial units:

t h e

Frequent pair:

t + h → th

Then:

th + e → the

The vocabulary gradually contains useful
frequently occurring subword units.

The actual implementation used by a production tokenizer can operate over bytes or characters and applies a learned vocabulary and merge rules.

WordPiece Tokenization

WordPiece is another subword tokenization approach associated with Transformer-based language models. Instead of simply selecting merges based on raw frequency, WordPiece vocabulary construction is designed around the likelihood and usefulness of subword units for language modeling.

Example Concept

A rare word can potentially be represented using a combination of known subword pieces rather than being treated as completely unknown.

Unigram Language Model Tokenization

Unigram tokenization takes a different approach. Instead of constructing the vocabulary primarily through iterative merges, it starts with a larger candidate vocabulary and selects a smaller vocabulary that provides an effective probabilistic representation of the training data.

Unigram-based approaches are particularly associated with tokenization frameworks such as SentencePiece.

BPE vs WordPiece vs Unigram

Method Core Idea Typical Strength
BPE Builds larger units through learned merges. Efficient and widely applicable.
WordPiece Selects useful subword units based on a language-modeling objective. Effective representation of subwords.
Unigram Uses a probabilistic vocabulary-selection approach. Flexible segmentation.

From Tokens to Token IDs

After tokenization, each token is mapped to an integer from the model's vocabulary. These integers are called token IDs.

Text:

"AI is powerful"

Tokens:

["AI", " is", " powerful"]

Conceptual token IDs:

[1532, 318, 7421]

The exact IDs depend on the tokenizer vocabulary.

Token IDs themselves do not contain the semantic meaning of a word. They act as indices into the model's vocabulary and embedding matrix.

Tokenization vs Embeddings

Tokenization

Converts text into discrete token units and token IDs.

Text → Tokens → IDs

Embeddings

Maps token IDs to dense numerical vectors that can be processed by neural network layers.

Token ID → Vector

Tokenization and Context Windows

An LLM does not process an unlimited amount of text in a single context. The maximum number of tokens that can be considered depends on the model's context window.

Input Tokens

Tokens representing the user's prompt and supplied context.

Output Tokens

Tokens generated by the model as its response.

Context Limit

The model has a maximum context capacity defined by its architecture and configuration.

Computer code and artificial intelligence technology

Special Tokens

Tokenizers can also use special tokens that provide structural information to the model. The exact tokens differ across model architectures and tokenizer implementations.

Beginning / Start

Can indicate the beginning of a sequence in tokenization schemes that use such markers.

End of Sequence

Can indicate that a generated or processed sequence has ended.

Padding

Can be used to make sequences compatible with batch processing requirements.

How Tokenization Handles Different Languages

Tokenization becomes particularly interesting when processing multilingual text. Different languages have different writing systems, word boundaries, morphological structures, and frequency distributions.

Example

English, Hindi, Marathi, Japanese, Chinese, Arabic, and other languages may require different token segmentation patterns.

A tokenizer's vocabulary and training data influence how efficiently different languages are represented in tokens.

Why Token Efficiency Matters

Token count affects multiple aspects of an LLM application, including context usage, processing cost, latency, and how much information can fit inside a model's context window.

💰 Cost

Many LLM APIs calculate usage based partly on the number of input and output tokens.

⚡ Latency

Larger inputs and outputs can require more computation.

📦 Context

Efficient tokenization allows more useful information to fit into a fixed context window.

Practical Tokenization with Python

Developers can inspect tokenization using tokenizer libraries provided for specific model families. A conceptual Python workflow looks like this:

text = "Artificial intelligence is transforming software."

tokens = tokenizer.encode(text)

print(tokens)

decoded_text = tokenizer.decode(tokens)

print(decoded_text)

The exact tokenizer API depends on the model and tokenizer library being used. The important workflow is to encode text into token IDs and decode those IDs back into text when required.

Tokenization in RAG Systems

Tokenization also plays an important role in Retrieval-Augmented Generation (RAG). Documents may first be divided into chunks before being converted into embeddings and stored in a vector database. Retrieved chunks are then combined with the user query and sent to an LLM.

Documents

Chunking

Embedding

Vector Database

Relevant Chunks

Tokenization

LLM Context

Generated Answer

Common Misconceptions About Tokens

❌ One Token = One Word

Not necessarily. A token can represent a complete word, part of a word, punctuation, whitespace-related information, or another learned unit.

❌ Token IDs Are Meaning

A token ID is an index. Semantic representations are learned through the model's embeddings and subsequent neural network computations.

✓ Tokenization Is Fundamental

Tokenization determines how text is represented before it enters the Transformer architecture.

Complete LLM Text Processing Pipeline

User Text

Text Normalization / Preprocessing

Tokenizer

Token Sequence

Token IDs

Embedding Layer

Transformer Layers

Output Probability Distribution

Next Token Generation

Generated Text

Key Takeaways

  • LLMs do not directly process raw human-readable text.
  • Tokenization converts text into smaller units called tokens.
  • Tokens are mapped to numerical token IDs.
  • Token IDs are converted into vector representations through embeddings.
  • Subword tokenization helps handle rare words and vocabulary limitations.
  • BPE, WordPiece, and Unigram are important tokenization approaches.
  • Token count affects context usage, cost, latency, and model capacity.
  • Tokenization is a foundational stage in modern Generative AI systems.

Understanding Tokens Means Understanding LLMs

Tokenization may look like a simple preprocessing step, but it directly influences how language is represented, how much information fits inside the context window, and how efficiently an LLM processes text. For anyone working with Generative AI, NLP, RAG, or AI application development, understanding tokenization is an essential technical foundation.