TokDrift: When LLM Speaks in Subwords but Code Speaks in Grammar
Not on the map. Why?
Abstract (arXiv)
Large language models (LLMs) for code rely on subword tokenizers, such as byte-pair encoding (BPE), learned from mixed natural language text and programming language code but driven by statistics rather than grammar. As a result, semantically identical code snippets can be tokenized differently depending on superficial factors such as whitespace or identifier naming. To measure the impact of this misalignment, we introduce TokDrift, a framework that applies semantic-preserving rewrite rules to create code variants differing only in tokenization. Across nine code LLMs, including large ones with over 30B parameters, even minor formatting changes can cause substantial shifts in model behavior. Layer-wise analysis shows that the issue originates in early embeddings, where subword segmentation fails to capture grammar token boundaries. Our findings identify misaligned tokenization as a hidden obstacle to reliable code understanding and generation, highlighting the need for grammar-aware tokenization for future code LLMs.
Key facts. Cited by 2 papers in the map (data through 7 Oct 2026). Builds on 18 of its 33 references that are in the map. First posted on arXiv 16 Oct 2025.
Builds on
18 of 33 references.
References · 18
- 2018BERT: Pre-training of Deep Bidirectional Transformers for Language Understandingcited by 40,204
- 2015Neural Machine Translation of Rare Words with Subword Unitscited by 2,898
- 2024SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineeringcited by 1,628
- 2024DeepSeek-Coder: When the Large Language Model Meets Programming -- The Rise of Code Intelligencecited by 1,154
- 2021Show Your Work: Scratchpads for Intermediate Computation with Language Modelscited by 871
- 2021PICARD: Parsing Incrementally for Constrained Auto-Regressive Decoding from Language Modelscited by 402
- 2021CodeNet: A Large-Scale AI for Code Dataset for Learning a Diversity of Coding Taskscited by 199
- 2015Character-Aware Neural Language Modelscited by 129
- 2024SynCode: LLM Generation with Grammar Augmentationcited by 66
- 2021AVATAR: A Parallel Corpus for Java-Python Program Translationcited by 59
- 2019On Learning Meaningful Code Changes via Neural Machine Translationcited by 50
- 2024Tokenization Matters! Degrading Large Language Models through Challenging Their Tokenizationcited by 32
6 more
- 2025Boundless Byte Pair Encoding: Breaking the Pre-tokenization Barriercited by 31
- 2020Learning to Update Natural Language Comments Based on Code Changescited by 30
- 2021Dynamic Language Models for Continuously Evolving Contentcited by 27
- 2025Broken Tokens? Your Language Model can Secretly Handle Non-Canonical Tokenizationscited by 16
- 2023CodeBPE: Investigating Subtokenization Options for Large Language Model Pretraining on Source Codecited by 9
- 2025Is Your Benchmark Still Useful? Dynamic Benchmarking for Code Language Modelscited by 5
Built on by
Citing papers per year: 2026: 2.
2 papers.