AI Lesson & Submodule

What is Tokenization?

Learn how human-readable text is broken into tokens and converted into numeric IDs before entering a Large Language Model.

Beginner12 min readLLM FoundationInterview Ready
Lesson Overview

In this lesson, you will learn the first step of every LLM request: converting raw text into tokens. By the end, you should be able to explain how a sentence becomes token IDs and why tokenization affects model input, context size, and cost.

From Beginner to Engineer
Beginner Level

Tokenization breaks text into smaller parts.

Engineer Level

Tokenization maps text pieces to token IDs from a fixed vocabulary table.

Production Level

Tokenization controls prompt cost, context usage, latency margins, and retrieval quality.

Why This Matters

Tokenization is the entry gate to every language model. If text is split inefficiently, prompts become longer, costs increase, context windows fill faster, and model behavior can become harder to predict.

Mental Model: Lego blocks

Think of tokenization like cutting a sentence into Lego blocks. The model does not see the whole sentence directly. It sees reusable pieces that are converted into numbers. Just like you can build anything from a fixed set of Lego blocks, the model constructs all languages from a fixed vocabulary.

Visual Diagram: The Tokenization Pipeline

Step 01Raw Text"I love AI"User prompt string
Step 02Tokenizertiktoken / BPESegmentation engine
Step 03Tokens["I", " love", " AI"]Subword text units
Step 04Token IDs[40, 3047, 15592]Vocabulary index map
Step 05EmbeddingsDense Vector [4096]Model vector input

Tokenization in Simple Words

Humans read language as words and sentences. LLMs cannot directly process raw text. They need numbers. Tokenization is the bridge between language and mathematics. It breaks text into smaller units called tokens, then maps each token to a numeric ID from the model vocabulary.

Example: Text to Tokens to Token IDs

Step 1: Input text string"I love AI"
Step 2: Token representation["I"," love"," AI"]
Step 3: Mapped Token IDs[40,3047,15592]

Exact token IDs depend on the tokenizer used by the model. Different models may produce different token IDs and token counts.

One Word Is Not Always One Token

A token can be a full word, part of a word, punctuation, whitespace, a number, a symbol, or part of a Unicode character.

"cat"
cat
"tokenization"
tokenization
"unbelievable"
unbelievable
"₹500"
500

Tokenization Comparison Examples

Simple English2 tokens
Input: "Hello world"
Hello world

Standard English words are frequently indexed as single tokens in model vocabularies.

Long Word2 tokens
Input: "tokenization"
tokenization

Rare words are split into subword tokens to keep vocabulary size manageable.

Code Snippet7 tokens
Input: "const userName = getUser()"
const userName = getUser()

Code contains symbols, whitespace, and camelCase casing, leading to complex splits.

Non-English Text7 tokens
Input: "আমি বাংলা শিখছি"
আমি বাংলা শিখি

Non-Latin scripts consume significantly more tokens due to underrepresentation in model vocabularies.

Deep-Dive Core Concepts

Tokenizer Vocabulary

A vocabulary maps token pieces to numeric IDs. Think of it as a huge bilingual dictionary mapping string chunks to integers.

Token IDs

The model processes token IDs, not raw strings. These IDs act as coordinates inside the model parameter layers.

Embedding Lookup

Token IDs are converted into high-dimensional vectors by looking up corresponding indices in the embedding matrix.

Transformer Input

The transformer uses these dense vector embeddings to understand syntactic context and predict output tokens.

Concepts Covered

TokenToken IDTokenizer VocabularyEmbedding LookupSubword SplitContext WindowAPI Cost

Why AI Engineers Care About Tokenization

API Cost

LLM providers calculate bills based on total processed tokens (input prompts + output completions).

Context Window

Every model has a hard limit of total tokens it can process. Exceeding this limit drops older history or throws API errors.

RAG Pipelines

Information retrieved from vector databases must be chunked based on token boundaries to ensure retrieval accuracy.

AI Agents

Tool calling schemas, memory loops, and planning instructions consume valuable token slots in every execution step.

Production Failure Scenario: Unicode Emoji Bill Inflation
Root Cause: A developer parsed system log outputs containing heavy emoji streams. Emojis under cl100k_base tokenizers get broken down into 4 byte tokens each, inflating the payload count by 400%.
Fix / Strategy: Filter out emojis from raw inputs or use Llama-3's tokenizer which has expanded direct support for emoji tokens.
Try This in the Lab
  • Open the Tokenizer Visualizer Studio.
  • Input 'I love AI' and count the tokens.
  • Compare English token counts against Bengali scripts.
Mapped Foundation Project

Tokenizer Visualizer Studio

Build an interactive visualizer that shows how text becomes tokens, token IDs, token counts, and estimated API cost.

Architecture Preview

User Input String → Tokenizer Engine → Token IDs → Token Highlight UI → Cost Estimate

Tech Stack Planned
TypeScriptReactCSS VariablesTokenizer Library

Common Beginner Misconceptions

Misconception

One word equals one token.

Reality

A word can be one token, multiple subword tokens, or even split into byte-level sequences depending on spelling and language.

Misconception

Tokenization understands text meaning.

Reality

Tokenization is a purely statistical/rule-based split. Meaning is learned downstream by the model's weights and self-attention heads.

Misconception

All models count tokens the same way.

Reality

Different models utilize different tokenizers (e.g., Llama-3 uses tiktoken with a 128k vocabulary; older GPT-3 models use cl100k_base).

Technical Interview Defense Q&A

Key Takeaways

  • Tokenization is the first step of LLM input processing.
  • LLMs process token IDs, not raw text.
  • One word is not always one token.
  • Token count affects cost, context window, latency, and RAG design.
  • Understanding tokenization helps you design better AI applications.

Before You Move Next Checklist