AIAI Academy

Module 1: Foundations · 3 min read

Tokens and the Context Window: The Units AI Reads and Writes In

Why some languages "cost" more than English, what the context window is, and why the model forgets things in long conversations.

In the lesson on language models we saw that the model produces text unit by unit. Those units, tokens, are the concept that most affects day-to-day work with AI: price, speed, and how much information you can give the model at once.

What a token is

A language model doesn't read letters or whole words. Before text enters the model, a tool called a tokenizer breaks it into pieces the model recognizes. A token can be a whole word, part of a word, a punctuation mark or a space.

According to OpenAI, in English a token is on average about 4 characters, and 100 tokens are roughly 75 words. You can see the breakdown for yourself in OpenAI's free Tokenizer tool: paste in text and see each token in a different color.

Why some languages need more tokens

The tokenizer is built from the texts in the training data, mostly English. Common English words become a single token. In languages that appear much less often, such as Hebrew or Arabic, a word is often split into several tokens. Prefixes and suffixes add more splits.

So the same content in another language usually uses more tokens than in English. If your business pays per token through an API, that shows up directly on the bill. Newer model generations have narrowed the gap, but it's worth checking in the tool rather than assuming.

🎬 Let's build the GPT Tokenizer · Andrej Karpathy

The context window: short-term memory

The context window is the maximum number of tokens the model can "see" at once. It includes everything: the instructions, the documents you attached, the entire conversation history and the answer the model is writing.

In recent years windows have grown a lot. Leading models now accept hundreds of thousands of tokens, and some a million or more. In practice that's enough for a whole book or a large code file.

Two important things to remember:

  1. The model doesn't remember between conversations unless the system stores information separately and feeds it back in. The "memory" products offer is exactly that kind of mechanism.
  2. When the window fills up, something gets cut. In a very long conversation, the product may truncate or summarize the earlier part. That's one reason that in long conversations the model "forgets" instructions you gave at the start.

Big doesn't always mean precise

A study by researchers from Stanford and other institutions, called "Lost in the Middle," showed that models find information better when it's at the beginning or end of the text, and less well when it's buried in the middle. Models have improved since, but the practical principle remains:

  • Put the important instruction at the beginning or end of the message.
  • Don't attach 200 pages when five relevant ones are enough.
  • In a long conversation that's started to get muddled, open a new one with a short summary.

We'll go into these techniques in the lesson on prompt engineering.

Tokens and money

AI providers price API usage by tokens, and output tokens (what the model writes) are usually more expensive than input tokens (what you sent). With a monthly subscription you don't see this, but tokens are what stands behind usage limits. We'll expand on the economics of this in the economics module.

Bottom line: A token is the model's unit of text, and the context window is how many tokens it sees at once. Two practical facts: languages other than English often use more tokens, and focused information works better than a pile of documents.

Sources

Read next