Code-Switching and Mixed-Language Text › Tokenization Economics
Language AI HandbookAll chapters Token counts determine how much of a token-limited context the input occupies. To estimate costs, apply the deployed service’s billing rules to those counts. Measure latency and quality separately on the serving path.
Train two byte-level BPE tokenizers with the same 360-entry vocabulary budget and the same preprocessing. The first sees eight English training sentences. The second sees four of those English sentences and four Hindi sentences in Devanagari. The four manually written test variants are held out. The Hindi text and its romanization share meaning, though they use different spaces, punctuation, and encodings where those conventions differ.
PythonIn [21]Expand
english_training = [
"Please check the cable before bringing the laptop to the office.",
"I will ask the team about the schedule this morning.",
"They are bringing work to the next meeting.",
"Could you come over after we finish charging the battery?",
"The room is busy tonight because several people are there.",
"Please call me at home and tell me how the party went.",
"I could not go after all because I had other work.",
"We shared the plans and moved the morning meeting.",
]
hindi_training = [
"मैं आज सुबह काम पर जा रहा हूँ।",
"आज रात घर पर खाना नहीं है।",
"मैं कल बैठक में आ रहा हूँ क्योंकि काम है।",
"तुम पार्टी में कब आ रहे हो और फिर कहाँ जाओगे।",
]
forms = {
"English": "I am not coming to the party tonight because I have work.",
"Devanagari Hindi": "मैं आज रात पार्टी में नहीं आ रहा हूँ क्योंकि मेरा काम है।",
"Romanized Hindi": "Main aaj raat party mein nahi aa raha hoon kyunki mera kaam hai.",
"Mixed Hindi–English": "Yaar main aaj night party mein nahi aa raha, because I have work.",
}
def train_byte_bpe(corpus):
tokenizer = Tokenizer(models.BPE())
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)
tokenizer.decoder = decoders.ByteLevel()
tokenizer.train_from_iterator(
corpus,
trainer=trainers.BpeTrainer(
vocab_size=360,
initial_alphabet=pre_tokenizers.ByteLevel.alphabet(),
show_progress=False,
),
)
return tokenizer
training_sets = {
"English training": english_training,
"English + Hindi training": english_training[:4] + hindi_training,
}
bpe_models = {
name: train_byte_bpe(texts) for name, texts in training_sets.items()
}
counts = {}
for name, tokenizer in bpe_models.items():
encoded = [tokenizer.encode(text).ids for text in forms.values()]
assert all(
tokenizer.decode(ids) == text
for ids, text in zip(encoded, forms.values())
)
counts[name] = [len(ids) for ids in encoded]
form_names = list(forms)
form_positions = np.arange(len(form_names))ConsoleOut [22]Expand
English {'English training': 24, 'English + Hindi training': 42}
Devanagari Hindi {'English training': 147, 'English + Hindi training': 42}
Romanized Hindi {'English training': 47, 'English + Hindi training': 54}
Mixed Hindi–English {'English training': 36, 'English + Hindi training': 54}Adding Devanagari training text gives the second tokenizer opportunities to learn Devanagari merges. It has no Romanized Hindi training sentences, so the result for Romanized Hindi remains a measure of what it can reuse from the training mixture. The output varies with the training fixtures and the chosen input. In the displayed order (English, Devanagari Hindi, Romanized Hindi, mixed Hindi–English), English-only training gives 24, 147, 47, and 36 tokens. English-plus-Hindi training gives 42, 42, 54, and 54 tokens.
VisualizationOut [23]Expand

The experiment measures segmentation under explicitly limited training mixtures. An application comparison should use several independently validated examples, the exact deployed tokenizers, and end-task measurements in the intended languages. Petrov et al. (2023) use a parallel corpus to compare token-count disparities across many real tokenizer and language pairs.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!