In AI, a token is a small chunk of text (not always a full word) that language models process - and understanding how tokens work helps you manage prompt size, cost, and performance more effectively.
April 19, 2025
Photo by Augustin Guiot on Unsplash
If you've ever worked with language models like ChatGPT or GPT-4, you’ve probably come across the word "token."
But what exactly is a token - and why should you care?
Let’s break it down.
A token is a chunk of text that a language model processes at one time.
Tokens can be as short as one character or as long as one word, depending on the language and the tokenizer.
For example, the sentence:
I’m hungry.
Might break down into these tokens:
['I', '’', 'm', ' hungry', '.']
That’s 5 tokens, even though it’s only 3 words.
Why?
Because many models use subword tokenization to handle rare words, typos, or other variations more efficiently.
Tokens are important because they affect:
Understanding tokens helps you write better prompts, avoid hitting limits, and control costs.
You can see how tokenization works using the tiktoken
library from OpenAI:
import tiktoken
# Use the tokenizer for GPT-4
enc = tiktoken.encoding_for_model("gpt-4")
text = "I'm hungry."
tokens = enc.encode(text)
print(f"Original text: {text}")
print(f"Tokens: {tokens}")
print(f"Number of tokens: {len(tokens)}")
Or use Hugging Face’s tokenizer:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("gpt2")
text = "I'm hungry."
tokens = tokenizer.tokenize(text)
print(f"Tokens: {tokens}")
print(f"Number of tokens: {len(tokens)}")
A token isn’t the same as a word.
It’s a unit of text a model understands.
And when working with language models, understanding tokens helps you write smarter, faster, and cheaper code.