betaFeedback

WorksComputer Science

Tokenisation is not in APX

Abstract

In this paper, we prove that, unless P=NP, no polynomial-time algorithm can find a tokeniser whose objective is within any constant factor of the optimum, meaning that tokenisation is not in the APX complexity class. We prove this for direct and bottom-up tokenisation, under both a compression or unigram log-likelihood objectives, and for binary or larger alphabets. We further show that, even over unary alphabets, direct tokenisation is APX-hard, and thus admits no polynomial-time approximation scheme.

Introduction

Tokenisers are the “glasses” through which language models (LMs) see the world, converting raw text—made of bytes or characters—into sequences of tokens. They define an LM’s API costs, impact the models’ throughput, affect their performance, and directly shape output distributions [28, 19, 1]. It is thus natural to pose the question:

how should one choose a tokeniser?

A recent line of research frames this question as an optimisation problem: define an objective function, and find a tokeniser that performs well on it. In fact, famous tokenisation algorithms can be seen as instantiations of this principle. BPE is a greedy algorithm for compression, iteratively merging the most frequent pair of adjacent tokens [11, 24], while UnigramLM is an expectation-maximisation algorithm for log-likelihood [18, 21]. Notably, these methods are purely heuristic, having no guarantees regarding their solutions’ quality.1

A few recent works have explored whether it is even possible to propose tokenisers with optimality guarantees—all focusing on compression as their

analysed objective. Studying unboundedly large alphabets, a set of initial papers proved that tokenisation’s optimisation problem is NP-hard [26, 17, 20].2 Follow up work then showed it is still NP-hard over unary alphabets, and APX-hard over binary ones [16].3 These results, however, leave open three main questions:

(i) is tokenisation hard for other objectives beyond compression? (ii) does any constant-factor approximation algorithm exist for tokenisation? (iii) is unary tokenisation also hard to approximate?

In this work, we tackle these three questions. First, we answer (i) and (ii) showing that binary tokenisation is not in APX; we prove this for direct and bottom-up tokenisation, covering both the compression and unigram log-likelihood objectives. This means that, unless P=NPP = NP, no polynomialtime algorithm can approximate binary tokenisation within any constant factor, under either objective.4 Second, we answer (iii) by proving that unary tokenisation is APX-hard—so even in such simple alphabets, tokenisation cannot be approximated arbitrarily well (shown in §C due to space). Figure 1 summarises prior results and ours.

Approximation ratios ruled out for tokenisation

Figure 1. Approximation ratios ruled out for tokenisation, assuming P≠NPP \ne NP. A ratio of 1 means exact solutions (NP-hardness), cc some constant (APX-hardness), and “any cc” every constant (not in APX). Under compression, NN is achieved by character-level tokenisation.

Overall, our results rule out the existence of (unconditional) approximation algorithms for tokenisation.5 Guarantees for practical tokenisers must therefore be dataset-specific, rather than hold for all possible inputs. In fact, in practice, tokenisers perform fairly well when applied to natural languages, coming within about 1% of the optimal compression ([25]). Future work should explore what makes natural language so much easier to tokenise than our worst-case constructions, and explore data-specific algorithmic guarantees.

Tokenisation

Let c∈Σ∗\mathbf{c} \in\Sigma^{*} be a character-string over an alphabet Σ\Sigma, and let D={cn}m=1M\mathcal{D} = \{c_n\}_{m=1}^{M} be a dataset (a multiset of such strings) with total length N≔∑c∈D∣c∣N \coloneqq\sum_{\mathbf{c}\in\mathcal{D}} |\mathbf{c}|.6 We say the alphabet is binary if ∣Σ∣=2|\Sigma|=2 and unary if ∣Σ∣=1|\Sigma|=1. A tokeniser’s job is then to segment c\mathbf{c} as a sequence of tokens s=⟨s1,…,sm⟩\mathbf{s}=\langle s_1,\ldots,s_m\rangle from its vocabulary S⊂Σ+\mathcal{S}\subset\Sigma^{+}, which we write as tok⁡(c)=s\operatorname{tok}(\mathbf{c})=\mathbf{s} s.t. concat⁡(s)≔s1∘⋯∘sm=c\operatorname{concat}(\mathbf{s})\coloneqq s_1\circ\cdots\circ s_m=\mathbf{c}. To guarantee any string can be encoded, we enforce Σ⊆S\Sigma\subseteq\mathcal{S} and define the vocabulary’s budget as K≔∣S∣−∣Σ∣K\coloneqq|\mathcal{S}|-|\Sigma|.

Given these definitions, how do we now choose a tokeniser? As described above, we first choose an objective function G\mathcal{G}; given a class of realisable tokenisers T\mathcal{T}, we then select the one optimising it. The optimal objective value achievable is then:

G⋆(D)≔min⁡tok⁡∈TG(tok⁡,D).(1)\mathcal{G}^{\star}(\mathcal{D}) \coloneqq\min_{\operatorname{tok}\in\mathcal{T}} \mathcal{G}(\operatorname{tok},\mathcal{D}). \tag*{(1)}

We say a tokenisation algorithm is a ρ\rho-approximation if, given any dataset D\mathcal{D}, it returns a tokeniser tok⁡\operatorname{tok} with G(tok⁡,D)≤ρG⋆\mathcal{G}(\operatorname{tok},\mathcal{D})\leq\rho\mathcal{G}^{\star}. A problem with no ρ=1\rho=1 approximation is then NP-hard, with no ρ=1+ε\rho=1+\varepsilon approximation (for some ε>0\varepsilon>0) is APX-hard, and with no ρ=c\rho=c (for all constants c>1c>1) is not in the APX class (all unless P = NP).

Tokenisation Objectives

Although no perfect objective seems to exist for tokenisation, several functions are said to correlate with a tokeniser’s performance: e.g., unigram log-likelihood ([18]), 95%-ile token frequency ([13]), Rényi efficiency ([28]), and compression ([12]). It is thus natural to use them as an objective for tokenisation. We analyse two of them here.

A tokeniser’s compression Gcomp\mathcal{G}_{\mathrm{comp}}, defined as the number of tokens it uses to represent a dataset, and its unigram negative log-likelihood Gll\mathcal{G}_{\mathrm{ll}}, defined as the log-likelihood of the tokenised dataset under a unigram distribution. Notably, compression is the objective behind BPE, while log-likelihood is used by UnigramLM.7 Writing fsf_s for the frequency of token ss in dataset D\mathcal{D} and F=∑sfsF=\sum_s f_s as the total token count, we formalise these objectives as

Gcomp(tok⁡,D)≔∑c∈D∣tok⁡(c)∣,(2)\mathcal{G}_{\mathrm{comp}}(\operatorname{tok},\mathcal{D}) \coloneqq\sum_{\mathbf{c}\in\mathcal{D}}|\operatorname{tok}(\mathbf{c})|, \tag*{(2)}
Gll(tok⁡,D)≔∑s∈Sfs>0fslog⁡Ffs.(3)\mathcal{G}_{\mathrm{ll}}(\operatorname{tok},\mathcal{D}) \coloneqq\sum_{\substack{s\in\mathcal{S}\\ f_s>0}} f_s\log\frac{F}{f_s}. \tag*{(3)}

Classes of Tokenisers

Several classes of tokenisers exist. We consider two of them here. First, given a vocabulary S\mathcal{S} and a training dataset D\mathcal{D}, direct tokenisers segment each character string c∈D\mathbf{c}\in\mathcal{D} optimally under the given objective, being defined as:

tok⁡S,D(c)≔arg⁡min⁡s∈S∗: concat⁡(s)=c GS[S,D](s,{c}).(4)\operatorname{tok}_{\mathcal{S},\mathcal{D}}(\mathbf{c}) \coloneqq\underset{\mathbf{s}\in\mathcal{S}^{*}:\,\operatorname{concat}(\mathbf{s})=\mathbf{c}}{\arg\min}\ \mathcal{G}_{\mathcal{S}}[\mathcal{S},\mathcal{D](\mathbf{s},\{\mathbf{c}\})}. \tag*{(4)}

where GS[S,D]\mathcal{G}_{\mathcal{S}}[\mathcal{S},\mathcal{D}] returns a function which outputs the objective value (compression or negative log-likelihood) of a specific segmentation s\mathbf{s} considering the entire training dataset.8 Second, bottom-up tokenisers are specified by a sequence of merges m=⟨m1,…,m∣m∣⟩∈M∗\mathbf{m}=\langle m_1,\ldots,m_{|\mathbf{m}|}\rangle\in\mathcal{M}^{*}, where M=Σ+×Σ+\mathcal{M}=\Sigma^{+}\times\Sigma^{+} and each mk=sk∘sk′m_k=s_k\circ s'_k, which they apply in order to iteratively merge pairs of tokens in a string. Writing merge⁡mk\operatorname{merge}_{m_k} as a function that replaces, from left to right, every non-overlapping occurrence of adjacent tokens ⟨sk,sk′⟩\langle s_k,s'_k\rangle by the single token sknew=sk∘sk′s_k^{\mathrm{new}}=s_k\circ s'_k, we formalise these tokenisers as:

tok⁡[m](c)≔(∏k=1∣m∣merge⁡mk)(c).(5)\operatorname{tok}_{[\mathbf{m}]}(\mathbf{c})\coloneqq\left(\prod_{k=1}^{|\mathbf{m}|}\operatorname{merge}_{m_k}\right)(\mathbf{c}). \tag*{(5)}

where ∘\circ denotes function composition. UnigramLM is an example of a direct tokeniser, while BPE is a bottom-up tokeniser. We write TK\mathcal{T}_K for the set of tokenisers with budget KK, noting bottom-up tokenisers have K=∣S∣−∣Σ∣=∣m∣K=|S|-|\Sigma|=|\mathbf{m}|.

Direct Tokenisation is not in APX

We now prove that direct tokenisation is not in the APX class. We do so using a gap reduction from a known NP-hard problem, finite-word rank, to binary direct tokenisation. From an instance of this NP-hard decision problem—whose desired output is a YES or NO answer—we build an instance of the tokenisation optimisation problem whose optimal objective will be small if the answer to the finite-word rank instance is YES, and large if it is NO. By making this gap arbitrarily large, we then show that any constant-factor approximation to tokenisation would allow us to distinguish YES from NO answers in finite-word rank.9 Unless P = NP, this is thus infeasible in polynomial time. Notably, proving the hardness of tokenisation with ∣Σ∣=2|\Sigma|=2 also proves it for all ∣Σ∣>2|\Sigma|>2.

Finite-Word Rank

As the starting point of our reductions, we will use the finite-word rank decision problem, which was proven to be NP-complete by Néraud [22]. Let X={xm}m=1M\mathcal{X}=\{x_m\}_{m=1}^{M} be a finite set of words, composed of strings in an alphabet Σwr\Sigma_{\mathrm{wr}}, i.e., xm∈Σwr+x_m\in\Sigma_{\mathrm{wr}}^{+}. This problem then asks if there is any set of strings Y⊆Σwr+\mathcal{Y}\subseteq\Sigma_{\mathrm{wr}}^{+} with size at most KwrK_{\mathrm{wr}} which covers X\mathcal{X}.

Definition 1. Let X⊂Σwr+\mathcal{X}\subset\Sigma_{\mathrm{wr}}^{+} be a finite set of words and Kwr∈NK_{\mathrm{wr}}\in\mathbb{N} a target size. The finite-word rank decision problem requires deciding whether there exists a set Y⊆Σwr+\mathcal{Y}\subseteq\Sigma_{\mathrm{wr}}^{+} that covers X\mathcal{X}, i.e., X⊆Y∗\mathcal{X}\subseteq\mathcal{Y}^{*}, with size at most ∣Y∣≤Kwr|\mathcal{Y}|\leq K_{\mathrm{wr}}. Defining rank⁡(X)\operatorname{rank}(\mathcal{X}) as X\mathcal{X}’s smallest possible cover size, we write:

Kwr≥rank⁡(X)=defmin⁡Y⊆Σwr+ s.t. X⊆Y∗∣Y∣(6)K_{\mathrm{wr}}\geq\operatorname{rank}(\mathcal{X})\mathrel{\mathop=^{\mathrm{def}}}\min_{\mathcal{Y}\subseteq\Sigma_{\mathrm{wr}}^{+}\ \mathrm{s.t.}\ \mathcal{X}\subseteq\mathcal{Y}^{*}}|\mathcal{Y}| \tag*{(6)}

The finite-word rank problem is closely related to tokenisation, but differs in two main ways. First, Y\mathcal{Y} need not contain Σwr\Sigma_{\mathrm{wr}}, allowing for solutions that cannot represent all strings in Σwr∗\Sigma_{\mathrm{wr}}^{*}. Second, it asks about the size of Y\mathcal{Y}, instead of how compactly X\mathcal{X} can be represented using tokens in Y\mathcal{Y}. Leveraging these differences will be the key to our results.

Reducing from Finite-Word Rank

We now provide the reduction from finite-word rank instances to binary direct tokenisation. The main idea is to define a function enc⁡:Σwr→{0,1}∗\operatorname{enc}:\Sigma_{\mathrm{wr}}\to\{0,1\}^{*} which will replace each symbol in finite-word rank instances with long unique binary blocks.

Reduction 1. Let (X,Kwr)(\mathcal{X},K_{\mathrm{wr}}) define a finite-word rank instance. Now, fix an integer R≥2R\geq2. We construct a tokenisation instance as:

K=Kwr,Σ={0,1}(7)K=K_{\mathrm{wr}},\quad\Sigma=\{0,1\} \tag*{(7)}
D={enc⁡(x1)∘⋯∘enc⁡(x∣x∣)∣x∈X}(8)\mathcal{D}=\{\operatorname{enc}(x_1)\circ\cdots\circ\operatorname{enc}(x_{|x|})\mid x\in\mathcal{X}\} \tag*{(8)}
enc⁡(x)=defenc⁡1(x)∘enc⁡2(x)∘⋯∘enc⁡R(x)(9)\operatorname{enc}(x)\mathrel{\mathop=^{\mathrm{def}}}\operatorname{enc}_1(x)\circ\operatorname{enc}_2(x)\circ\cdots\circ\operatorname{enc}_R(x) \tag*{(9)}
enc⁡r(x)=def11∘0y1∘0y2∘⋯∘0yL∘0(10)\operatorname{enc}_r(x)\mathrel{\mathop=^{\mathrm{def}}}1 1\circ0y_1\circ0y_2\circ\cdots\circ0y_L\circ0 \tag*{(10)}

where y1y2⋯yL∈{0,1}Ly_1y_2\cdots y_L\in\{0,1\}^{L} is a binary fixed-length unique representation of each pair in Σwr×{1,…,R}\Sigma_{\mathrm{wr}}\times\{1,\ldots,R\}; we thus have L=⌈log⁡2(∣Σwr∣R)⌉L=\lceil\log_2(|\Sigma_{\mathrm{wr}}|R)\rceil.

We can now use this reduction to prove our main results regarding direct tokenisation.

Theorem 1. Unless P = NP, binary direct tokenisation under the compression objective Ocomp\mathcal{O}_{\mathrm{comp}} is not in APX. In fact, for every constant ϵ>0\epsilon>0, it admits no polynomial-time N1−ϵN^{1-\epsilon}-approximation.

Proof sketch. See a formal proof in §A.1. For a target approximation factor ρ\rho, apply Reduction 1 to a finite-word rank instance (X,Kwr)(\mathcal{X},K_{\mathrm{wr}}) using R≥⌈ρT⌉R\geq\lceil\rho T\rceil. We can prove

References

  1. [1]Orevaoghene Ahia, Sachin Kumar, Hila Gonen, Jungo Kasai, David Mortensen, Noah A. Smith, and Yulia Tsvetkov. 2023. Do all languages cost the same? tokenization in the era of commercial language models. In Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing, pages 9904–9923, Singapore. Association for Computational Linguistics.
  2. [2]Paola Alimonti and Viggo Kann. 2000. Some APX-completeness results for cubic graphs. Theoretical Computer Science, 237(1–2):123–134.DOI
  3. [3]Sanjeev Arora, Carsten Lund, Rajeev Motwani, Madhu Sudan, and Mario Szegedy. 1998. Proof verification and the hardness of approximation problems. Journal of the ACM, 45(3):501–555.
  4. [4]Sanjeev Arora and Shmuel Safra. 1998. Probabilistic checking of proofs: A new characterization of NP. Journal of the ACM, 45(1):70–122.
  5. [5]Giorgio Ausiello, Pierluigi Crescenzi, Giorgio Gambosi, Viggo Kann, Alberto Marchetti-Spaccamela, and Marco Protasi. 2012. Complexity and approximation: Combinatorial optimization problems and their approximability properties. Springer Science & Business Media.
  6. [6]Kundan Baliga, Aiden Xie, and Aditya Byju. 2026. Pair-merge tokenisation is hard to approximate on binary strings and strongly NP-hard on unary datasets. Preprint, arXiv:2611.13568.arxiv.org/abs/2611.13568
  7. [7]Atieh Bastan, Xiaoling He, A. Pavan, and N. V. Vinodchandran. 2026. Tokenization: Hardness and bicriteria approximation algorithms. Preprint, arXiv:2611.15074.arxiv.org/abs/2611.15074
  8. [8]Piotr Berman and Marek Karpinski. 2001. Efficient amplifiers and bounded degree optimization. Technical Report TR01-053, Electronic Colloquium on Computational Complexity.
  9. [9]Pierluigi Crescenzi. 1997. A short guide to approximation preserving reductions. In Proceedings of the Twelfth Annual IEEE Conference on Computational Complexity, pages 262–273.
  10. [10]Irit Dinur. 2007. The PCP theorem by gap amplification. Journal of the ACM, 54(3):12.DOI
  11. [11]Philip Gage. 1994. A new algorithm for data compression. C Users Journal, 12(2):23–38.DOI
  12. [12]Matthias Gallé. 2019. Investigating the effectiveness of BPE: The power of shorter sequences. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP), pages 1375–1381, Hong Kong, China. Association for Computational Linguistics.
  13. [13]Thamme Gowda and Jonathan May. 2020. Finding the optimal vocabulary size for neural machine translation. In Findings of the Association for Computational Linguistics: EMNLP 2020, pages 3955–3964, Online. Association for Computational Linguistics.DOI
  14. [14]Johan Håstad. 2001. Some optimal inapproximability results. Journal of the ACM, 48(4):798–859.
  15. [15]Andrei Robert Ion, Rares Mateiu, Adrian Miclaus, and Alexandru Popa. 2026. How hard is tokenisation? Algorithms and complexity beyond NP-hardness. Preprint, arXiv:2611.12348.arxiv.org/abs/2611.12348
  16. [16]Violeta Kastreva, Philip Whittington, Dennis Komm, and Tiago Pimentel. 2026. Tokenisation over bounded alphabets is hard. In The Fourteenth International Conference on Learning Representations.
  17. [17]László Kozma and Johannes Voderholzer. 2026. Theoretical Analysis of Byte-Pair Encoding. In 34th Annual European Symposium on Algorithms (ESA 2026), volume 388 of Leibniz International Proceedings in Informatics (LIPIcs), pages 80:1–80:17.
  18. [18]Taku Kudo. 2018. Subword regularization: Improving neural network translation models with multiple subword candidates. In Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 66–75, Melbourne, Australia. Association for Computational Linguistics.arxiv.org/abs/1804.10959
  19. [19]Pietro Lesci, Clara Meister, Thomas Hofmann, Andreas Vlachos, and Tiago Pimentel. 2025. Causal estimation of tokenisation bias. In Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 28325–28340, Vienna, Austria. Association for Computational Linguistics.
  20. [20]Jia Peng Lim, Shawn Tan, Davin Choo, and Hady W. Law. 2025. A partition cover approach to tokenization. In Advances in Neural Information Processing Systems, volume 38, pages 74286–74318.arxiv.org/abs/2501.06246
  21. [21]Clara Meister. 2026. UnigramLM: An attempt at writing the missing manual. Blog post.
  22. [22]Jean Néraud. 1990. Elementariness of a finite set of words is co-NP-complete. RAIRO – Theoretical Informatics and Applications, 24(5):459–470.DOI
  23. [23]Sartaj Sahni and Teofilo Gonzalez. 1976. P-complete approximation problems. Journal of the ACM, 23(3):555–565.DOI
  24. [24]Rico Sennrich, Barry Haddow, and Alexandra Birch. 2016. Neural machine translation of rare words with subword units. In Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 1715–1725, Berlin, Germany. Association for Computational Linguistics.arxiv.org/abs/1508.07909
  25. [25]Jan Tempus, Philip Whittington, Craig W. Schmidt, Dennis Komm, and Tiago Pimentel. 2026. Tokenisation via convex relaxations. Preprint, arXiv:2605.22821.arxiv.org/abs/2605.22821
  26. [26]Philip Whittington, Gregor Bachmann, and Tiago Pimentel. 2025. Tokenisation is NP-complete. In Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 28133–28153, Vienna, Austria. Association for Computational Linguistics.
  27. [27]Ahmetcan Yavuz, Clara Meister, and Tiago Pimentel. 2026. Objective vs. search: Decomposing what makes a good tokeniser. In Proceedings of the 2026 Conference on Empirical Methods in Natural Language Processing, pages 52425–52444, Budapest, Hungary. Association for Computational Linguistics.arxiv.org/abs/2609.19145
  28. [28]Vilém Zouhar, Clara Meister, Juan Gastaldi, Li Du, Mrinmaya Sachan, and Ryan Cotterell. 2023. Tokenization and the noiseless channel. In Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 5184–5207, Toronto, Canada. Association for Computational Linguistics.

Paper details

vlnc:W_gsm4j9pt
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 0

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents