Tokens
What it is
A model does not read letters or words. It reads tokens: pieces of text, each a short word or a fragment of a longer one.
- In English, 1 token is about three quarters of a word, so 100 tokens is roughly 75 words.
- Other languages, code and unusual words use more tokens for the same meaning.
- Everything is counted in tokens: the prompt you send, the answer you get, the limits and the bill.
Why it matters
Tokens are the unit of the whole subject.
- Cost is tokens times a price. Speed is tokens per second. Limits are tokens per minute. A context window is a number of tokens.
- If you can estimate the tokens in a request, you can estimate its cost, its speed and whether it fits.
In the real world: A team budgeting a multilingual assistant finds that the same question costs noticeably more in Tamil than in English, because the text splits into more tokens.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
tokens = enc.encode("Where is my refund for order 88213?")
len(tokens) # about 10 tokens for 7 words
# A rough rule when you cannot count exactly:
# tokens ~ words / 0.75 (English)
# tokens ~ characters / 4Estimating in words or characters and forgetting the conversion. A 3,000-word document is about 4,000 tokens, not 3,000, and in Hindi or in code the gap is larger. Different models also count the same text slightly differently.