What would break if you tokenized a Llama prompt using tiktoken's cl100k_base instead of the Llama tokenizer?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
A developer saves API calls by tokenizing Llama-3 prompts client-side using tiktoken with cl100k_base for cost estimation, then sends the same text to a Llama-3 API endpoint. Separately, they try to use tiktoken-encoded token IDs to index directly into a Llama embedding matrix. Describe what breaks in each case and why.
tiktoken and the Llama tokenizer have different vocabularies, so the same text yields different token counts (bad cost estimates) and the IDs are not portable (garbage embedding lookups).
Imagine two libraries that each give books their own shelf numbers. Book #4891 in one library might be a cookbook, while #4891 in the other is a poetry collection. The numbers look the same but point to totally different things. A tokenizer is like that shelving system: it turns text into numbered slots. tiktoken, which is OpenAI's system, and the Llama system number their slots differently. So if you count slots with one library's catalog to guess how big a job is in the other, your guess drifts. And if you hand one library's shelf numbers to the other and ask it to fetch the matching books, it grabs whatever happens to sit at those numbers, which is the wrong content every time.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: separate vocabularies + fertility differs by content + IDs are vocab-specific pointers + wrong embedding rows or out of range + bind tokenizer to model.
# Wrong: using tiktoken to count Llama-3 tokens
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
prompt = "Explain tokenization briefly."
print("cl100k estimate:", len(enc.encode(prompt))) # off vs Llama
# Right: use Llama's own tokenizer
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B")
print("Llama-3 actual:", len(tok.encode(prompt))) # matches cost
# Fatal: cl100k IDs into Llama's embedding matrix
cl100k_ids = enc.encode(prompt) # IDs for the cl100k vocab
# llama_model.embed_tokens(torch.tensor([cl100k_ids])) # wrong rows!Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming token IDs are a universal standard. Each tokenizer maps its own integers to its own strings, so the same integer means different text in different vocabularies.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.