Tokens, Context Windows, and What They Cost
How text becomes tokens, how to count them, what a context window really limits, and why input and output tokens are priced differently.
Every number that matters about an AI feature is measured in tokens. Your bill is priced per token. Latency grows with tokens generated. The context window, the hard limit on how much a model can take in, is a token count. And the most common surprise in a new AI feature's first month is a token surprise: a customer pastes a 300-page contract, a chat thread grows to 200 messages, or a "small" prompt turns out to be 12,000 tokens of system instructions repeated on every call.
In this lesson you'll learn what tokens actually are, how to count them exactly in code, what a context window does and doesn't limit, why output tokens cost several times more than input tokens, and how to estimate what a feature will cost before you ship it.
What a token is (and why it isn't a word)
A tokenizer splits text into pieces from a fixed vocabulary, typically tens of thousands to a few hundred thousand entries, and the model reads and writes only those pieces. Common words in common languages tend to be single tokens. Rare words, names, and identifiers get split into several. Whitespace and punctuation are tokens too.
You can't see a model's token boundaries through the API, but the counts tell the story. The patterns that matter for builders:
- English prose is the cheap case. A rough rule of thumb is three to four characters per token, but treat that only as a back-of-the-envelope estimate.
- Other languages often cost more. Many languages, especially those with non-Latin scripts, take noticeably more tokens for the same meaning. If your product serves an international audience, your cost per request varies by locale.
- Code, IDs, and structured data are dense. UUIDs, base64, long numbers, and minified JSON split into many short tokens.
- Formatting has a cost. Pretty-printed JSON spends tokens on indentation. Sending 500 records with two-space indentation can cost noticeably more than the same records minified.
- Images and PDFs are tokens too. When you send an image or a document, it's converted into input tokens and billed like text.
Two warnings. First, every model family has its own tokenizer, and different generations of the same provider's models can tokenize differently. A count from OpenAI's tokenizer is not a count of Claude tokens. Second, don't build anything important on a characters-per-token ratio. Count.