← Nestor G Pestelos Jr · Reference

Natural Language Processing & Systems

Tokens and Tokenization

August 25, 2026

A token is the fundamental atomic unit of information processed, embedded, and generated by large language models and neural sequence architectures. Rather than processing raw characters or arbitrary words directly, language models employ deterministic tokenizers that segment strings into sequences of discrete subword units mapped to numerical indices within a fixed vocabulary \(V\). Tokens define the resolution of model comprehension, dictate the memory footprint of attention layers via Key-Value (KV) caching, and govern the operational economics of inference.

1. Tokenization Fundamentals

1.1 Vocabulary Representation

A tokenizer operates as a bijective lookup between a finite vocabulary of subwords \(V\) and integer identifiers \(\{0, 1, \dots, |V|-1\}\). Modern vocabulary sizes typically range from \(|V| = 32,000\) (Llama 2) to \(|V| = 128,256\) (Llama 3/4) and \(|V| = 256,000\) (Gemma). Each token index corresponds to an embedding vector in the model's input projection matrix \(W_e \in \mathbb{R}^{|V| imes d_{ ext{model}}}\).

In standard English text corpora, 1,000 tokens represent approximately 750 words (an average compression ratio of 1.33 tokens per word, or roughly 4 characters per token) [1].

1.2 Subword Tokenization Algorithms

Modern language models rely on three primary subword segmentation algorithms:

2. Token Lifecycle and Processing Phases

2.1 Prefill (Input) vs. Decode (Output)

Transformer execution splits into two computationally distinct phases [6]:

2.2 Thinking and Reasoning Tokens

Hybrid reasoning models (e.g., DeepSeek-R1, Gemini 3.7 Flash Thinking, OpenAI o3) introduce thinking tokens. These are internal chain-of-thought tokens generated during test-time compute prior to emitting final responses [7]. Thinking tokens allow models to explore alternative verification paths, backtrack from logic errors, and perform self-correction. While thinking tokens are generated in the decode phase (and billed at output rates), they are typically hidden or masked from final user-facing text.

2.3 Special and Control Tokens

Tokenizers include dedicated non-printing control tokens that delineate structural boundaries:

3. KV Cache Scaling and Attention Mechanics

3.1 Memory Footprint Formulation

To avoid recomputing self-attention projections for past tokens at each decode step, models store the Key (\(K\)) and Value (\(V\)) tensors in a dynamic KV Cache. For a standard Multi-Head Attention (MHA) model, the memory required for the KV cache scales linearly with sequence length \(L\) [8]:

\[M_{ ext{KV}} = 2 imes B imes L imes n_{ ext{layers}} imes d_{ ext{model}} imes ext{BytesPerElement}\]

where \(B\) is batch size, \(L\) is context length, \(n_{ ext{layers}}\) is the number of transformer layers, \(d_{ ext{model}}\) is the hidden dimension, and the factor of 2 accounts for both \(K\) and \(V\) matrices. In FP16 precision (2 bytes per element), a 70B parameter model with a 128,000-token context requires approximately 160 GB of dedicated memory solely for a single user's KV cache.

3.2 Attention Architectures: MHA, MQA, and GQA

To mitigate the memory scaling bottleneck of Multi-Head Attention, modern architectures compress the key and value heads [9]:

4. Token Economics and Prompt Caching

4.1 Cost Asymmetry Across Token Types

API pricing across commercial model providers is structured around the computational cost differential between the prefill and decode phases [10]:

4.2 Prefix Prompt Caching

Prompt caching (implemented via PagedAttention in vLLM [8] and proprietary cloud gateways) identifies exact prefix matches in token sequences. When an agent executes multiple sequential turns sharing an identical system prompt, codebase index, or context history, the inference engine skips the prefill transformer layers entirely for the cached prefix.

Prefix caching reduces Time-To-First-Token (TTFT) latency by up to 80% and enables continuous autonomous agent loops to operate with marginal token budgets under $0.01 per turn [10].

5. Tokenizer Failure Modes and Artifacts

Because tokenizers process subwords rather than raw semantic concepts, several characteristic failure modes emerge in language models [11]:

See also

References

  1. [1] OpenAI, "What are Tokens and How to Count Them?," OpenAI Help Center Documentation, 2024. https://help.openai.com
  2. [2] R. Sennrich, B. Haddow, and A. Birch, "Neural Machine Translation of Rare Words with Subword Units," in Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (ACL), 2016, pp. 1715–1725. https://arxiv.org/abs/1508.07909
  3. [3] A. Radford, J. Wu, R. Child, D. Luan, D. Amodei, and I. Sutskever, "Language Models are Unsupervised Multitask Learners," OpenAI Technical Report, 2019.
  4. [4] J. Devlin, M. Chang, K. Lee, and K. Toutanova, "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding," in NAACL-HLT, 2019. https://arxiv.org/abs/1810.04805
  5. [5] T. Kudo, "Subword Regularization: Improving Neural Network Translation Models with Subword Sampling," in ACL, 2018. https://arxiv.org/abs/1804.10959
  6. [6] R. Pope, S. Douglas, A. Chowdhery, et al., "Efficiently Scaling Transformer Inference," in MLSys, 2023. https://arxiv.org/abs/2211.05102
  7. [7] N. Brown, G. Zhang, S. Feng, et al., "Large Language Models at Test-Time: Scaling Compute via Search and Verification," OpenAI Technical Papers, 2024.
  8. [8] W. Kwon, Z. Li, S. Zhuang, Y. Sheng, L. Zheng, C. H. Yu, J. E. Gonzalez, H. Zhang, and I. Stoica, "Efficient Memory Management for Large Language Model Serving with PagedAttention," in SOSP, 2023. https://arxiv.org/abs/2309.06180
  9. [9] J. Ainslie, J. Lee, D. de Las Casas, et al., "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints," in EMNLP, 2023. https://arxiv.org/abs/2305.13245
  10. [10] DeepSeek-AI, "DeepSeek-V3 Technical Report," arXiv:2412.19437, 2024. https://arxiv.org/abs/2412.19437
  11. [11] J. Landgraf, "SolidGoldMagikarp and Other Anomalous Tokens in LLMs," LessWrong Technical Notes, 2023.