Tokens
Model-specific units used to represent text and other inputs
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 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.
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.
| Exact text | cl100k_base | o200k_base |
|---|---|---|
| The house is red | 4 | 4 |
| La casa es roja | 5 | 4 |
Sources and documentation
A visual explanation
Watch on YouTube ↗ 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 ↗Continue in the portal
Related terms
Read more on this
APIs and platforms: a practical guide to 173 tools
A practical guide to 173 tools and resources for AI, APIs, automation, infrastructure, blockchain, and building on the internet.
Independent Media Is Ecosystem Infrastructure
Why independent media is as critical to decentralized ecosystems as the code itself
The Creator Who Doesn't Need Permission
Nobody gave me a green light.