A model can't read text. It can only do maths with numbers. So before anything else, text must be cut into tokens, and each token turned into a number. That's the tokenizer's job.
A tokenizer has two jobs. First, it splits text into tokens. Second, it looks up each token in its vocabulary, which is a big list of every token it knows. Each token has a number in that list, called its ID. Every model comes with its own tokenizer, and you must always use the matching one.
Let's see it. Auto tokenizer loads the tokenizer that matches our model. Tokenize splits a customer message into tokens. Run it. Nine tokens. Common words are one token each. But Hobbit is split into two pieces, Hob, and bit. And that strange G symbol means there's a space before the word.
app.pypython
from transformers import AutoTokenizertok = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M")text = "Where is my copy of The Hobbit?"tokens = tok.tokenize(text)print(tokens)print("Number of tokens:", len(tokens))
Output
['Where', 'Ġis', 'Ġmy', 'Ġcopy', 'Ġof', 'ĠThe', 'ĠHob', 'bit', '?']
Number of tokens: 9
Why not just use whole words? Because there are too many words, names, and typos. With pieces, the tokenizer can build any word, even one it has never seen. Common words stay whole. Rare words become a few pieces. A rough rule. One token is about three quarters of an English word.
Let's recap. A tokenizer splits text into tokens, and turns each token into an ID. Rare words become several pieces. And you must always use the model's own tokenizer. Next, token IDs, and special tokens.