Tokens and the Context Window
🎯 Introduction
When chatting with AI, you may have seen a conversation exceed a length limit, or noticed that attaching lengthy reference material makes the same question more expensive.
Models measure text in tokens, rather than simply counting characters. The context window also limits how many tokens a model can handle at once.
We will start with text segmentation, then explore input tokens, output tokens, and context windows, including their effects on cost and response quality.
🧱 Why Token Count Is Different from Word Count
Previously, we learned that models generate tokens one at a time. What exactly is a token?
A token is a basic unit used by models to process text. It does not necessarily correspond to a single character or a complete English word.
For example, given:
Hello, world!
Suppose a model splits it into four parts for this illustration:
[Hello] [,] [ world] [!]
There are four tokens. Notice that the space before world belongs to that token.
A tokenizer performs this segmentation using the model's rules. It converts text into tokens and maps each to an ID for computation. After generation, the application can convert the IDs back into readable text.
This is only an illustration; different models may segment the text differently. The result depends on the tokenizer. For example, the same Chinese sentence may be split into individual characters, words, or smaller fragments.
Character counts and word counts cannot simply replace token counts. For an accurate count, use the model's tokenizer or the usage statistics returned by its API.
🧱 Input and Output Tokens
Suppose we are building a simple AI translation feature. The program sends the following to tell the model what to do:
Application instruction:
You are a translation assistant. Translate the user's Chinese text into English.
User question:
Please translate: 今天下雨了。
The model returns:
It is raining today.
The diagram distinguishes input from output in this request:
These are the main content categories. Actual token usage may also include message formatting overhead.
If we also send previous conversation turns, those messages usually count as input tokens.
Input tokens are the content supplied to the model for this request; output tokens are the content it generates during the request.
An API may also count message formatting and other overhead, so reported usage may differ from a count of the visible text alone.
🧱 What Is a Context Window?
Both input and output use tokens. How much content can a model handle at once?
This capacity limit is called the context window, usually measured in tokens. Here, context means the content available for the current response, including the current question, relevant application instructions, and conversation history.
Suppose a model has an 8,000-token context window. All the numbers below are teaching examples, not the actual limits of a particular model.
Assume history uses 4,000 tokens, application instructions and the current question use 1,000, and we reserve 2,000 for the response.
This diagram shows the space used by each part:
Example capacity for illustration
4,000
1,000
2,000
1,000
4,000 + 1,000 + 2,000 + 1,000 = 8,000
Reserved space is available for future output. The model's separate input and output limits still apply.
Subtracting the history, current input, and reserved response space from 8,000 leaves 1,000 tokens.
Reserved response space is not already generated tokens. It is room left for the answer. Actual output usage depends on how much the model generates.
If history grows to 6,000 tokens with everything else unchanged, the total becomes 9,000, exceeding this example's capacity. The application may need to remove irrelevant history or compress some content; otherwise, the request may fail. Platforms handle this differently.
Another related concept is maximum output length:
- Context window: the total capacity available for processing input and generated content.
- Maximum output length: the most tokens the model may generate in this response.
For example, even with plenty of context space, a separate 1,000-token output limit prevents a single 3,000-token answer.
Models may impose different context, input, and output limits. Consult the documentation for the model you use.
💡 Effects on Cost and Quality
More Tokens Can Mean Higher Costs
Many model APIs charge separately for input and output tokens. A model API is an interface that lets your program call an AI model.
Suppose a model uses these prices (hypothetical teaching figures, not actual prices):
| Type | Price per million tokens |
|---|---|
| Input tokens | ¥2 |
| Output tokens | ¥8 |
A request uses 2,000 input tokens and generates 500 output tokens:
Input cost = 2,000 ÷ 1,000,000 × 2 = ¥0.004
Output cost = 500 ÷ 1,000,000 × 8 = ¥0.004
Total cost = ¥0.008
Even if the user's question stays the same, attaching lengthy history to every request increases input tokens and may increase cost. Actual billing can also depend on caching, model choice, and other factors; focus on the basic calculation for now.
Fitting the Content Does Not Guarantee a Better Answer
Suppose you ask the model to summarize order complaints but also send large amounts of unrelated advertising, old conversations, and repeated instructions.
Even within the context window, the model may struggle to identify important information and lose focus.
Consider both how much content fits and which content is useful. Later lessons on context engineering cover selecting and organizing it.
❓ Knowledge Check
Q1: Does a larger context window guarantee more accurate answers?
No. It allows more content in a request, but using it correctly also depends on relevance, clarity, and the model's capabilities.
Q2: Why does AI seem to remember earlier messages?
Chat applications can save earlier messages and resend relevant content in later requests. If you first say “I run an online store,” then ask “Write an introduction for my store,” the application can send both messages so the model knows which store you mean.
This does not mean the model retrains itself during every chat or permanently remembers the entire conversation. History storage, long-term memory features, and the information included in each request depend on the product.
🧪 Exercise
Suppose an AI support request contains:
- Application instruction: “Be polite and concise.”
- Historical message: “I previously bought a blue jacket.”
- Current question: “When will it ship?”
- Model response: “I need to check the order information to confirm the shipping date.”
Answer these questions:
- Which items are input tokens, and which is output?
- With an 8,000-token context window, 6,500 input tokens, and up to 1,000 output tokens planned, does the request fit? How much space remains?
- As history grows, what would you remove first? Why should you preserve the current question and important business rules?
Success criteria: The first three items are input and the fourth is output. 8,000 - 6,500 - 1,000 = 500, so it fits in this simplified example. Remove irrelevant and repeated history first while preserving information essential to the task.
🎉 Congratulations! You now understand text tokenization, input and output token usage, and how context windows limit length and affect cost. Next, we will explore model types and generation settings.
How LLMs Work
Understand text generation through a sentence completion example, and learn about training, inference, attention, and hallucinations.
Model Types and Parameters
Compare general-purpose, reasoning, and multimodal models, understand temperature, top-p, and output limits, and choose models for your tasks.
