Skip to main content
AI / MLLLM FundamentalsInteractiveBeginner

Tokenization

KO: 토큰화·EN: Tokenization·JA: トークン化

The preprocessing step that splits raw text into subword units (tokens) mapped to integer IDs for an LLM

Table of Contents

Interactive Playground

Tap or drag to interact with the demo
Quick Answer

What is Tokenization?

The preprocessing step that splits raw text into subword units (tokens) mapped to integer IDs for an LLM. It is a core concept in LLM Fundamentals within AI / ML.

Core Principles

Tokenization is the first step in every Large Language Model (LLM) pipeline, converting raw text into a sequence of integer IDs representing tokens. Splitting purely by whole words fails on unseen vocabulary, while splitting by individual characters makes sequences impractically long. Modern LLMs therefore rely on subword tokenizers such as Byte Pair Encoding (BPE) or SentencePiece.

Common English words map to a single token ("apple"), rarer words split into familiar fragments ("token" + "ization"), and non-Latin scripts like Korean or Japanese can consume 1.5–3× more tokens for the same semantic meaning depending on the tokenizer's vocabulary balance.

Practical Takeaways

  • Tokens govern cost, latency, and context limits: API billing, generation throughput (tokens/sec), and context-window caps are all measured in tokens—not characters. Measure budgets programmatically via tiktoken or provider token-counting endpoints.
  • Optimize multilingual prompts: Writing concise English system prompts and JSON schemas while requesting localized user output can reduce token overhead and latency in Korean/Japanese applications.
  • Understand character-level blind spots: LLMs struggle with counting letters or exact string reversal because they see opaque token IDs rather than raw characters. Offload exact string math to code execution or tool calls.

Notable Examples

  • OpenAI BPE (o200k_base): Expanded vocabulary to 200k tokens, significantly improving compression for Korean, Japanese, and source code.
  • SentencePiece: Treats text as a raw Unicode stream without assuming whitespace word boundaries, widely used in Gemini and multilingual transformers.
  • RAG Chunk Sizing: Production retrieval pipelines slice documents by token count (e.g., 512 tokens with 10% overlap) rather than character count to avoid silent embedding truncation.

Practical Code Snippet

Copy and drop straight into your project
token-budget.tsTypeScript
// Rule of thumb: ~4 chars/token in English, ~1.5-2 chars/token in KO/JAexport function estimateTokens(text: string): number {  const cjkChars = (text.match(/[぀-ヿ가-힯一-鿿]/g) ?? []).length;  const otherChars = text.length - cjkChars;  return Math.ceil(cjkChars / 1.6 + otherChars / 4);} const tokens = estimateTokens("RAG retrieves relevant chunks before generation.");