Home / Learn / LLM foundations

LessonLLM foundationsLesson 1 of 1

What's a token?

An AI will take your words & phrases and separate them into "bitesize" chunks for it to compute.

  • LLM foundations
  • Lesson A
The route

What's one of the first things you learn in school? You learn how to read, and AIs aren't much different here. They have to read your text, words, characters. But how does that actually work?

An AI will take your words & phrases and separate them into "bitesize" chunks for it to compute. Let's start with an example

Hello. Welcome to strawberrrrry city!

If we head on over to https://platform.openai.com/tokenizer1 we can see what an AI model will do to separate them

The OpenAI tokenizer showing "Hello. Welcome to strawberrrrry city!" as 10 tokens and 37 characters

You'll notice that message is 10 tokens. Each color represents an individual token that the AI model will "digest". Note that punctuations are single tokens and something interesting is happening in the word "strawberrrrry". It's not split into syllables although you might think that's how it worked.

The actual principle is tokens are this:

Frequently occurring character sequences in the training data, found by an algorithm, that repeatedly merges the most common adjacent pairs. Pure statistics without any prior knowledge of syllables, prefixes, or meaning.

Common things get one token no matter how long. Amsterdam and Florida are common words so they're single tokens. Hamsterdam and Floridia aren't, so the model has to piece them together from fragments.

The OpenAI tokenizer showing "Amsterdam Hamsterdam Florida Floridia" as 6 tokens

Now here's where I lied earlier. Remember when I said "They have to read your text, words, characters"? Well the model doesn't actually read characters, at all. By the time the model gets involved your list of characters, words, phrases, is actually just a bunch of numbers.

Each chunk has a fixed ID. The model never actually receives the word "Amsterdam". "Amsterdam" is actually entry #167754 and THAT is what the model receives. Keep in mind different models have different IDs for tokens; each model family builds its own dictionary.

The same text as token IDs: 167754, 20665, 26128, 13934, 10738, 66780

The GPT-4 and GPT-3.5 tokenizer showing "Amsterdam" as 2 tokens, IDs 6219 and 27398

Let's head on over to the next lesson to learn about context windows!

B - What's a context window?

Or if you want to understand tokenizers in more detail we can go to

A2 WIP

Notes

  1. https://platform.openai.com/tokenizer ↩