"How many tokens is this text?" The question shows up when you pay the APIContract that defines how one program asks another for data or actions: which endpoints to call, what to send and what comes back. On the web, usually HTTP with JSON. bill, when you try to understand why the model "forgot" the start of the conversation, or why the same task costs more in Portuguese. Almost everyone has heard that a token is "roughly a word". Sometimes it is. Often it isn't, and the difference explains things that look like bugs.
This article follows the path from text to model, with examples I measured myself, and ends with what matters day to day: cost, context window and how to count before you spend.
From Text to Number, in Four Steps
- The text goes in. "A inteligência artificial ajuda desenvolvedores a escrever código mais rápido." (Portuguese for "Artificial intelligence helps developers write code faster.")
- The tokenizer cuts it into pieces that exist in its vocabulary:
A,inteligência,artificial,ajuda,desenvol,ved,ores,a,escrever,código,mais,rápido,.. Notice the space sticks to the start of the word. - Each piece becomes a number, its ID in the vocabulary:
códigois 46734 and the period is 13. - Each number becomes a vector (the embeddingList of numbers that represents the meaning of a text. Similar texts produce nearby lists, which lets you search by meaning, not just matching words.), and those vectors are what the model does its math on. For the response, the path runs the other way: the model picks the next ID, which turns back into text.
These numbers came from tiktoken, OpenAI's tokenization library, with the o200k_base vocabulary, the one GPT-4o uses. Other models have other tokenizers, and the same text gives different counts in each one.

Why Chunks, and Not Letters or Words
Letter by letter, the text would be far too long: every sentence would turn into hundreds of steps for the model. Word by word, the vocabulary would never end: proper names, typos, code, words that don't exist yet. Today's tokenizers sit in the middle, with a method called BPE (byte pair encoding): they start from bytes and keep merging the pairs that show up most in the training text, until they reach the target vocabulary size.
The result: common words become a single token, rare words become several, and anything can still be written, even if byte by byte. Andrej Karpathy has a two-hour lecture building a tokenizer from scratch, "Let's build the GPT Tokenizer", with the code in the minbpe repository. It's the best material if you want to see how it works inside.
| Word | GPT-4 (cl100k) | GPT-4o (o200k) |
|---|---|---|
| strawberry | str aw berry (3) |
st raw berry (3) |
| morango (strawberry) | mor ango (2) |
mor ango (2) |
| inconstitucionalissimamente | 8 tokens | 5 tokens |
| Olá, tudo bem? (Hi, how are you?) | 6 tokens (Ol and á split) |
5 tokens |
GPT-4o's larger vocabulary (200,019 tokens, versus 100,277 for GPT-4) is what brings the count down: more ready-made pieces, fewer cuts. Meta made the same move with Llama 3, with a 128K-token vocabulary that, according to the model card, produces up to 15% fewer tokens than Llama 2's.
The "Strawberry" Case
Ask some models how many "r"s are in "strawberry" and the answer may come back wrong. It's not a lack of intelligence: the model receives three numbers, 302, 1618 and 19772, not ten letters. To count the "r"s, it has to know from memory how each piece is spelled. Newer models get it right more often, partly because they reason through more steps before answering, but the cause is still there.

The same goes for reversing a word, counting the characters in a text or doing math with long numbers, which also get cut into pieces. When the task depends on characters, ask the model to use a code tool instead of trusting its direct answer.
Same Text, Different Languages
Tokenizers are trained on more English text, so English gets more ready-made pieces. I measured the same sentence in the four languages of this blog:
| Language | GPT-4 (cl100k) | GPT-4o (o200k) |
|---|---|---|
| English | 9 | 8 |
| Portuguese | 16 | 13 |
| Spanish | 16 | 14 |
| French | 20 | 16 |
In practice, if you use the API in Portuguese, you pay more for the same task and fill the context window faster. A study by Aleksandar Petrov and coauthors (2023) measured differences of up to 15 times between languages on the same tokenizer; the hardest hit are languages with non-Latin scripts. OpenAI's rule of thumb, "1 token ≈ 4 characters ≈ ¾ of a word", holds for English. For other languages, expect more.
Special Tokens
Besides text, the vocabulary has tokens that never show up in your message but that the model uses to find its way: <|endoftext|> marks the end of a document, and chat models have tokens that separate the system instructions, your message and the model's response. On Hugging Face, they appear as bos_token, eos_token and pad_token in each model's config.
You don't need to write these tokens: the provider's library assembles everything. But they count toward the total, which is why the API count is usually a bit higher than the plain text count.
Context Window: Everything Shares the Same Space
The context window is the maximum number of tokens the model considers at once, input and output combined. Claude Opus 5.5 has 1 million tokens of context and up to 128K of output; GPT-6.2 Sol has 1,050,000.
It sounds endless, but everything goes into that count:
- System instructions, which go into every call.
- Tool definitions, if the model uses tools: name, description and schema for each one.
- Conversation history, which grows with every exchange.
- Retrieved documents, in a RAGTechnique that retrieves relevant passages from a document base and hands them to the model with the question, so it answers from sources, not memory alone. system.
- The question and the response, which also takes up space.
A big window doesn't mean the model makes good use of everything in it. That's why what goes into the context is a design decision, the subject of the article on context engineering.
Cost: The Meter Runs Both Ways

APIs charge per million tokens, with separate prices for input and output. Output usually costs five times more:
| Model | Input | Cached input | Output |
|---|---|---|---|
| Claude Opus 5.5 | $4 | fraction of the input price | $20 |
| Claude Sonnet 5.5 | $2 | fraction of the input price | $10 |
| GPT-6.2 Sol | $2 | $0.10 | $10 |
Prices per million tokens, checked in November 2026 on the official pages. They change: check before you do the math.
An example: a call to Sonnet 5.5 with 10K input tokens and 1K output tokens costs $0.02 for input and $0.01 for output. Looks like nothing, until it turns into a chat with thousands of users, where every message resends the whole history.
Prompt caching is the most direct saving when the start of the message repeats (instructions, tools, a fixed document). On OpenAI, cached input comes out up to 95% cheaper, starting at 1,024 tokens. On Anthropic, writing to the cache costs 1.25x the normal price (5-minute cache) or 2x (1 hour), and reading costs 10% of the price, or 5% on the 5.5 models. The difference between prompt cachingModel provider feature that stores the repeated start of an already processed prompt, charging less and answering faster on later calls. and other ways to reuse context is in the article RAG or CAG.
In the RAG app I maintain, every model call logs input and output tokens along with the purpose (chat, summary, mind map). That's how I found out which feature weighed the most on the bill, and it wasn't the chat.
How to Count Before You Spend
| Tool | What for |
|---|---|
| Tiktokenizer | See the cuts and the IDs, with several tokenizers side by side |
| OpenAI Tokenizer | Official count for OpenAI models |
| Tokenizer Playground | Tokenizers from open models (Llama, Mistral, Gemma) in the browser |
tiktoken (Python) |
Count in code, without calling the API |
Anthropic's count_tokens |
Free count before sending, for Claude models |
Anthropic's count is an estimate, and the docs themselves warn that the tokenizer for models from Claude 4.7 onward produces about 30% more tokens for the same text than the one in earlier models. If you switched models and the bill went up, that might be why.
Counting in code is simple:
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
ids = enc.encode("A inteligência artificial ajuda desenvolvedores.")
print(len(ids)) # how many tokens
print([enc.decode([i]) for i in ids]) # how it was cutQuick check
Frequently Asked Questions
What is a token in AI?
It's a chunk of text the model treats as one unit: a word, part of a word, a punctuation mark or a space attached to a word. Each token has a number in the tokenizer's vocabulary, and that number is what the model receives.
How many words are in a token?
For English, OpenAI uses the rule of 1 token for about ¾ of a word, or 4 characters. In Portuguese the ratio is worse: in the sentence I measured, 10 words became 13 tokens on GPT-4o.
Why does ChatGPT get the number of letters in a word wrong?
Because it doesn't see the letters. The word arrives cut into pieces that became numbers, and counting letters means spelling each piece from memory. For tasks like that, the best move is to ask the model to use code.
What is a context window?
It's the maximum number of tokens the model considers in one call, adding up instructions, tools, history, documents, question and response. The frontier models of 2026 have windows of about 1 million tokens.
How do I calculate the cost of an API call?
Multiply the input tokens by the input price and the output tokens by the output price, both per million tokens. If part of the input is cached, that part comes out cheaper.
How do I count tokens before sending?
With Tiktokenizer or OpenAI's official tokenizer in the browser, with the tiktoken library in code, or with Anthropic's count_tokens endpoint for Claude models.
References
- OpenAI, tiktoken (version 0.14, used for the measurements in this article in November 2026) and Tokenizer; What are tokens and how to count them.
- Anthropic, Token counting, Prompt caching and pricing, checked in 2026. OpenAI, API pricing and Prompt caching, checked in 2026.
- Meta, Llama 3 model card, 2024.
- Aleksandar Petrov et al., Language Model Tokenizers Introduce Unfairness Between Languages, 2023.
- Andrej Karpathy, Let's build the GPT Tokenizer and minbpe, 2024.
- Hugging Face, Tokenizers and The Tokenizer Playground.