Skip to content

Contact LV

lv@dablock.com

Juan Lago (LV) uses your details to reply to enquiries (legitimate interest), fulfil a contract with you, or take steps you request before entering one. Privacy and your rights

Tokens

Model-specific units used to represent text and other inputs

LV glossary Updated

What is an AI token?

Tokens are units a model uses to represent input and output. In text, a token can encode a whole word, part of a word, punctuation or a space together with the letters that follow it. The tokenizer assigns each unit an ID. Counting those IDs gives the token length of that text for that encoding.

How it works

La casa es roja
Encoding

The space and “ro” share a token. “ja” is a separate token.

“roja” and its leading space fit in a single token.

The symbol marks a space.

Key takeaways

  • The count depends on the exact text and the chosen encoding.

  • A request can include instructions and tool definitions in addition to the text a user sees.

▸

Illustrative example

In the sentence “La casa es roja”, cl100k_base splits the last word across two tokens. The first holds the preceding space and “ro”, and the second holds “ja”. With o200k_base, the space and “roja” form one token. Both encodings recover the same sentence.

How do you count tokens before sending a prompt?

Choose the encoding used by the target model, encode the exact text and count the IDs. This Python example uses tiktoken to count the same sentence with two encodings. For an API request that also carries chat history or tool definitions, use the provider’s request counter or reported input usage to account for the entire message.

count_tokens.py
import tiktoken

text = "La casa es roja"
for name in ("cl100k_base", "o200k_base"):
    encoding = tiktoken.get_encoding(name)
    print(name, len(encoding.encode(text)))

Do four words use the same number of tokens in English and Spanish?

Each sentence below contains four words. The counts use the exact text shown, with single spaces and no final punctuation. Changing the encoding changes the Spanish count even though the words stay the same.

Scroll the table to see every column.

Do four words use the same number of tokens in English and Spanish?
Exact textcl100k_baseo200k_base
The house is red44
La casa es roja54

Sources and documentation

A visual explanation

Watch on YouTube ↗

3Blue1Brown · Video in English ·

Transformers, the tech behind LLMs

Follow tokens and their vectors through a transformer, with an illustrated written version alongside the video.

Read the illustrated lesson ↗