Tokens, context windows, and why they matter
12 minWhat tokens are, what a context window is, and how to count tokens before you send a request.
At the end of Module 0 you noticed something: as your chat went on, input_tokens kept climbing. By the end of this lesson you'll know exactly why, and why it matters for cost, speed and quality.
What is a token?
Language models don't read letters or whole words. They read tokens: chunks of text, often a word or part of a word. Common words are usually one token. Rare words, names and code get split into several.
Roughly speaking:
- A short English word is often one token.
- A typical English sentence is 10–25 tokens.
- Code, numbers, and non-English text usually use more tokens per character than plain English.
Tokens are the unit of everything: what you pay for, what fills the context window, and what takes time to generate.
Why not just count words?
Because the model's tokenizer decides how text is split, and different models split differently. "Roughly four characters per token" is a decent mental model for English prose, but it's only an estimate. When the number matters, measure it.
Counting tokens properly
The Claude API has a token counting endpoint. It tells you exactly how many input tokens a request will use, for the specific model you choose, without sending the request to the model.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const result = await client.messages.countTokens({
model: "claude-opus-5-5",
messages: [{ role: "user", content: "How many tokens is this sentence?" }],
});
console.log(result.input_tokens);import anthropic
client = anthropic.Anthropic()
result = client.messages.count_tokens(
model="claude-opus-5-5",
messages=[{"role": "user", "content": "How many tokens is this sentence?"}],
)
print(result.input_tokens)The context window
The context window is everything the model can "see" in a single request: the system prompt, the whole conversation history, any documents you include, and the answer it's writing. Think of it as the model's short-term memory for that one request.
Current Claude models have a 1 million token context window. That's roughly a few thousand pages of text, enough for entire codebases or long reports.
Big, but not infinite. And here's the catch you discovered in Module 0:
Every request sends the whole conversation again.
Turn 1 sends one message. Turn 10 sends all ten turns plus the replies. So input tokens grow with every message, and that has three effects:
- Cost goes up. You pay for input tokens on every request, including the history you resend.
- Speed goes down (a little). More input means more for the model to read before it replies.
- Focus can drift. A very long context full of old, irrelevant material is harder to use well. Clean, relevant context gives better answers.
Input vs output tokens
Your usage object reports two numbers, and they're priced differently:
- Input tokens: everything you send (system prompt, history, documents, your question).
- Output tokens: everything the model generates, including its internal thinking.
Output tokens cost several times more than input tokens, and they're also what takes time to produce. We'll put real numbers on this in the "Costs and latency" lesson.
Try it
- Run
count.ts(orcount.py) with a sentence of your choice. Note the number. - Now count the same idea written as code (for example
const total = items.reduce((a, b) => a + b, 0);) and as a non-English sentence. Which uses the most tokens per character? - Paste a long article or document into the
contentand count it. How many tokens is a typical web page?
Keep these numbers in mind. You'll use them to estimate costs in two lessons' time.