Tokens

Updated · 5 min read
mediumPython 3.13aillmtokenstokenizerbeginnersinterview

How many words does a model read in "Srihitha lives in San Francisco"? Five, you might say. The model sees eight pieces, and the name Srihitha alone is three of them. Models don't read words; they read tokens.

This lesson shows what tokens are, using a real tokenizer, and why they decide what you pay and how much a model can read.

What a Token Is

A token is a small piece of text that a model reads or writes as one unit. A token can be a whole word, part of a word, a punctuation mark, or a word with a space in front of it. A tokenizer splits text into tokens and turns each one into a number the model can work with.

Real tokens from a real tokenizer

Common words are usually one token; rare words and names are split into pieces. "France" is one token, but "Srihitha" becomes Sri, h and itha, because the tokenizer learned its pieces from common text. As a rough guide, one token is about three-quarters of an English word, but it varies.

Counting Tokens in Python

The tiktoken package is an open-source tokenizer. Its o200k_base encoding is used by several OpenAI models; other providers use different tokenizers, so counts differ slightly between models.

Python
import tiktoken  # install first: pip install tiktoken
 
encoder = tiktoken.get_encoding("o200k_base")  # a tokenizer used by recent OpenAI models
 
for text in ["The capital of France is Paris.", "Srihitha lives in San Francisco.", "unbelievably"]:
    ids = encoder.encode(text)
    pieces = [encoder.decode([i]) for i in ids]
    print(len(ids), "tokens:", pieces)
Output
7 tokens: ['The', ' capital', ' of', ' France', ' is', ' Paris', '.']
8 tokens: ['Sri', 'h', 'itha', ' lives', ' in', ' San', ' Francisco', '.']
3 tokens: ['un', 'bel', 'ievably']

Why Tokens Matter

WhyWhat it means for you
CostModel APIs charge per token, usually separately for input (your prompt) and output (the reply)
LimitsA model's context window, from "Context and Context Windows", is measured in tokens
SpeedLonger outputs take longer, because the model generates one token at a time
LanguagesThe same message can need more tokens in some languages, so it can cost more

Common Mistakes

Assuming one word equals one token. Names, rare words and long words often split into several.

Forgetting that output tokens cost too. A long reply can cost more than the prompt.

Comparing token counts across different models directly. Each tokenizer splits text a little differently.

Interview Corner

What is a token?

The basic unit of text an LLM reads and writes: a whole word, part of a word, punctuation or a space. A tokenizer splits text into tokens and maps each to a number.

Why do tokens matter in practice?

They set cost and limits. APIs charge per input and output token, and context windows are measured in tokens, so long prompts and replies cost more and can hit the limit.

Practice

Exercise: count tokens. Use a tokenizer tool, or the Python example, to count the tokens in three sentences: one ordinary English sentence, one with your full name, and one in another language you know. Which used the most tokens per word, and why?

Check your understanding:

  1. Why is "Srihitha" three tokens while "France" is one?
  2. Name two things tokens affect.
  3. Why do token counts differ between models?

Quick check: tokens

4 questions
1What is a token?
2Why is a rare name often split into several tokens?
3What does a model API usually charge for?
4Why might the same message cost more in another language?

Key Takeaways

Quick reference

TermMeaning
TokenThe basic unit of text for an LLM
TokenizerThe tool that splits text into tokens and numbers
Input tokensThe tokens in your prompt
Output tokensThe tokens in the model's reply

Was this tutorial helpful?

Lesson 29 of 186