arXiv:2610.19139cs.DSOct 2026

Manuscript Offprint

arXiv:2610.19139

Online Tokenization with Optimal Delay in Linear Time

Mina Dalirrooyfard, Christopher Ye

Abstract

Tokenization is an essential preprocessing step in almost all large language models (LLMs). One of the most popular frameworks is BPE tokenization, which consists of special token segmentation, pretokenization, and byte-pair encoding (BPE). However, existing algorithms are sequential, with each stage only processing complete output from the previous stage. In order to reduce latency and memory footprints, recent works (Jiang and Gong, ICML 2026) (Li, Yang and Mamouras, ASPLOS 2026) (Mamouras, Li and Yang, PLDI 2026) have initiated the study of online algorithms for subroutines such as pretokenization and BPE with a goal of streaming input to output as the input arrives online. However, the search for efficient online tokenization remains open.

In this work, we formalize and resolve online tokenization. We introduce the notion of delay for online tokenization which characterizes the extent to which the algorithm output lags behind the (possibly computationally intractable) optimum. We give a near-linear time algorithm with optimal delay (up to constant factors). We complement this algorithm with a conditional lower bound: unless P=NP\mathbf{P} = \mathbf{NP}, there is no polynomial time algorithm that beats the delay of our algorithm by more than a constant factor.

We implement a practical variant for tokenizers from OpenAI's tiktoken and HuggingFace's tokenizers. We obtain a 7.9×−34.8×7.9 \times -34.8 \times latency advantage (measured as time to first token) over tiktoken (as well as the recent gigatoken tokenizer) and our implementation maintains approximately constant memory usage independent of input size. Our implementation also achieves a 2.4×−7.0×2.4 \times -7.0 \times improvement in throughput over tiktoken and 15.8×−38.1×15.8 \times -38.1 \times over tokenizers.

*Supported by NSF HDR TRIPODS Phase II grant 2217058 (EnCORE Institute). This work was completed as part of an internship at Morgan Stanley Machine Learning Research.

1 Introduction

In many natural language processing (NLP) applications, including powerful and popular large language models (LLMs), raw text undergoes tokenization before being provided as model input. Tokenization defines a vocabulary containing as many common words and sequences as possible, which help compress raw text into tokens, balancing expressivity and computational efficiency.

Byte-Pair Encoding (BPE) is a popular method for tokenization proposed by Gage [Gag94] that has been widely adopted in LLMs [SHB16]. Indeed, BPE Tokenization is both the subject of intense recent theoretical study [Bv23; JG26; MLY26] and the basis for popular practical tokenizers, including OpenAI's tiktoken and HuggingFace's tokenizers [Ope26a; Hug26].

Concretely, tokenization consists of a three step process: Special Token, Pretokenization, and BPE. As observed in Jiang and Gong [JG26], existing implementations of tokenization are inherently offline and sequential: we take in the full input, apply segmentation to the full input, then apply pretokenization on each segment, and then apply BPE to each pretoken. While completed segments and pretokens can be passed on to the next stage, one should in principle be able to apply pretokenization to a segment as it forms and similarly apply BPE to a pretoken as it forms. Following this observation, Jiang and Gong [JG26] raise the question of online tokenization: can we process each stage as the input arrives in an online setting?

While offline tokenization is now well understood both theoretically and empirically, the question of efficient online tokenization remains wide open. Online algorithms are desirable for many reasons. First, we can drastically reduce the memory footprint of our algorithm by avoiding holding the entire input or output in memory, a shortcoming of all existing end-to-end tokenization algorithms. Second, streaming can reduce latency of tokenization, drastically reducing the time to first token. Indeed, streaming and online algorithms are a fundamental and well-studied paradigm both in theory and in practice, with efficient algorithms being developed in many applications including counting [Mor78], selection and sorting [MP80], heavy hitters [MG82], frequency moments [AMS96], and cardinality estimation [FM85; DF03; Fla+07].

Motivated by latency and memory constraints, online algorithms have been developed for individual steps of tokenization: Li, Yang, and Mamouras [LYM26] studies the problem of online pretokenization and Berglund and van der Merwe [Bv23], Jiang and Gong [JG26], and Mamouras, Li, and Yang [MLY26] study online BPE algorithms. However, none of these works provide an online algorithm for the full tokenization pipeline. Indeed, Jiang and Gong [JG26] raise the design of end-to-end online tokenization as a crucial direction for future research. Our work is thus guided by the following question:

Is there an efficient algorithm for online tokenization?