Skip to content

What are tokens in AI: tokenizers, context windows and cost, with measured examples

How text turns into numbers before it reaches the model, why other languages cost more than English, the strawberry case, context windows, price per token and how to count before you spend.

Ramon Gomes 12 min read Level: Beginner

Also available in: Español, Français, Português

"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.

Vertical infographic with a Portuguese sentence, an isometric tokenizer machine and 13 colored pieces with their IDs directly underneath; desenvolvedores is split into desenvol, ved and ores, and a server rack receives the numeric sequence.
The sentence has 10 words and became 13 tokens. The word 'desenvolvedores' (developers) was cut into three pieces.

From Text to Number, in Four Steps

  1. 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.")
  2. 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.
  3. Each piece becomes a number, its ID in the vocabulary: código is 46734 and the period is 13.
  4. 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.

Old typesetting bench with a tray of metal movable type sorted into compartments and a typesetter's hand assembling a line with pieces of different sizes
The typesetter doesn't build the page letter by letter: the most common pieces come ready-made. The tokenizer does the same with the most frequent chunks of text.

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.

Cartoon of a small robot with a giant magnifying glass trying to count the letters on a strip of paper cut into three big pieces, while a child next to it calmly counts on their fingers
For the model, the word arrives as three pieces glued together. Counting letters means spelling each one from memory.

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:

Four sheets show the same sentence in English, Portuguese, Spanish and French. Two bar charts on the same scale compare GPT-4o, with 8, 13, 14 and 16 tokens, and GPT-4, with 9, 16, 16 and 20; a callout compares the 8 English tokens with the 16 French tokens on GPT-4o.
Same idea, different bills. French used twice as many tokens as English on GPT-4o.
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:

A timeline of six illustrated context sources stands beside an isometric reservoir: instructions, tools, history, documents and the question occupy numbered layers, with room reserved at the top for the answer. History and documents occupy the largest layers.
The question is the smallest part. What usually weighs the most is the history and the documents.
  • 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

Taxi dashboard at night seen from the back seat, with the meter lit up on the console and the passenger's hand holding a phone with a long conversation open
On the API, the ride is billed per token: what you send and what comes back, at different prices.

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:

python
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 cut

Quick check

1. What does the model receive when you type "strawberry"?
Show answer

Answer: A few numbers, one for each piece of the word

On GPT-4o, "strawberry" becomes three tokens: "st", "raw" and "berry".

2. Why does the same sentence use more tokens in Portuguese than in English?
Show answer

Answer: Because the tokenizer was trained on more English and has more ready-made pieces for it

Words that are frequent in training become a single token. Less frequent ones get cut into several.

3. What goes into the context window?
Show answer

Answer: Instructions, tools, history, documents, question and response

Everything shares the same token limit.

4. What usually costs more per token?
Show answer

Answer: Output

At November 2026 prices, output costs about five times as much as input.

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

Source: https://ramongomessilva.com.br/blog/what-are-tokens-in-llms · Accessed on December 02, 2026

Share Share on LinkedIn Share on X
Cite this article

Ready-made reference, with today as the access date.

ABNT

Brazilian standard (NBR 6023). Use it in theses and academic papers submitted in Brazil.

GOMES, Ramon. What are tokens in AI: tokenizers, context windows and cost, with measured examples. Ramon Gomes, 21 nov. 2026. Disponível em: https://ramongomessilva.com.br/blog/what-are-tokens-in-llms. Acesso em: 2 dez. 2026.

APA

American Psychological Association style. Common in scientific papers and journals, especially in the social, behavioral and health sciences.

Gomes, R. (2026, November 21). What are tokens in AI: tokenizers, context windows and cost, with measured examples. Ramon Gomes. https://ramongomessilva.com.br/blog/what-are-tokens-in-llms

BibTeX

Format for LaTeX users (Overleaf, for example). Paste it into your .bib file or import it into Zotero or Mendeley.

@online{gomes2026whataretokens,
  author = {Gomes, Ramon},
  title = {{What are tokens in AI: tokenizers, context windows and cost, with measured examples}},
  organization = {Ramon Gomes},
  year = {2026},
  month = nov,
  date = {2026-11-21},
  url = {https://ramongomessilva.com.br/blog/what-are-tokens-in-llms},
  urldate = {2026-12-02}
}
Found a mistake?

Wrong fact, broken link, outdated information: tell me and I will fix it.

10 to 2,000 characters. If you can, point to the right source.

Only so I can reply. It never shows on any page of the site.

Written by

Ramon Gomes

Software Engineer

Full stack software engineer. I like taking a messy problem, listening to the people who live with it and shipping something that holds up day to day. These days I split my time between my own products, applied AI and client work.

Available for projects

Need Something Like This Built?

I build websites, systems, apps and AI automations, from design to deploy. Tell me what you need or see what I offer.

Related Articles