Tokens and the context window
A model reads and writes tokens, not words, and everything it can use for one answer has to fit in its context window.
Words on this page (12)
- token
- : A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
- context window
- : The maximum number of tokens a model can look at in one call: your prompt, the conversation, tool results and the answer together.
- system prompt
- : Standing instructions placed at the start of every call that set the model's role and rules.
- prompt
- : The input text you give a model, including instructions, examples and data.
- tool
- : A function the model can ask your program to run, described by a name, description and argument schema.
- API
- : Application programming interface: a way for one program to call another, usually over HTTP.
- context rot
- : Answers getting worse as the input gets longer, even on simple tasks.
- JSON
- : A text format for structured data: keys and values in braces, like {"name": "Ada"}.
- tokenizer
- : The program that splits text into tokens and maps each one to an integer id.
- agent
- : A model run in a loop that chooses its own tools and steps until the task is done.
- training
- : Adjusting a model's parameters on data so its predictions improve.
- BPE
- : Byte-pair encoding: a common way tokenizers build their vocabulary by merging frequent pairs of characters.
What it is
A is a piece of text, usually a word or part of a word. "unbelievable" might be three tokens. Code, numbers and non-English text usually cost more tokens per character than plain English prose. A rule of thumb for English is about 4 characters or 0.75 words per token.
The is the maximum number of tokens the model can see at once. It holds your , the whole conversation so far, any files or results, and the answer being written. When it is full, something has to be dropped or summarized.
Why it matters
Cost, speed and quality all follow token count. Developers who use a model through an pay per input and output token. Long inputs are slower. Past a point, quality drops too: models get worse at using facts buried in the middle of a very long context, a failure often called .
Try it
Paste a paragraph of prose, a blob and some code into a viewer and compare tokens per character. Then count the tokens in the longest message you have ever sent a chatbot.
Worked example: counting tokens and cost
The sentence "I love learning about AI!" is about 7 tokens. A 1,000-word email is around 1,300 tokens. If a model charges $3 per million input tokens, that email costs about $0.004 to read. Cheap once, but an that re-reads a 50,000-token conversation 30 times pays for 1.5 million tokens: about $4.50. Tokens are the unit of cost, speed and memory.
How text becomes tokens
A tokenizer holds a fixed list of pieces, built by repeatedly merging character pairs that appear often in text (this method is called ). To encode text it starts from single characters and joins pairs using those learned merges, in the order it learned them. For example, "t" and "h" join into "th", then "th" and "e" into "the". Then it replaces each resulting piece with an ID number. The model only ever sees those numbers.
Common words are usually one piece. Rare words split into several. An invented split: "unbelievable" becomes "un", "believ", "able". This also explains why models struggle to count letters. The letters were never given to the model, only piece IDs.
Why long chats get expensive
Every turn resends the whole conversation as input. Invented numbers: each turn adds 500 tokens. Turn 1 sends about 500. Turn 2 sends turns 1 and 2 together, about 1,000. Turn 3 sends all three, about 1,500, and so on. Over 10 turns the total sent is 500 × (1 + 2 + ... + 10) = 500 × 55 = 27,500 tokens, although the conversation holds only 5,000 tokens of text. Cost grows faster than the number of turns. Providers can discount text that repeats at the start of every call, but the window still fills.
What goes wrong when the window fills
Symptoms are easy to spot:
- The assistant forgets an instruction from the start, because the app trimmed old messages.
- A reply ends mid-sentence, because the limit was reached.
- Costs per message keep rising during one session.
To confirm, count tokens before sending. Most APIs also report why generation stopped, for example hitting the length limit.
Common mistakes
Assuming a bigger window means a better answer. More text can bury what matters. Another mistake is forgetting that the answer also uses window space, so a nearly full window leaves little room to reply.
A user pastes a whole contract into a chatbot and asks one question about clause 14. The answer is vague. Nothing is broken: the contract filled most of the model's context window, and clause 14 was one needle in a very large haystack. A short, well-chosen excerpt would have worked better and cost less.
Check yourself
You paste a 300-page PDF and the answers get vague. What is the most likely cause?
Not quite. Long contexts dilute attention. Retrieving or summarizing the relevant part usually works better than pasting everything.
Which text costs more tokens for the same number of characters?
Not quite. Tokenizers are tuned on common English, so code and rare words split into more pieces.
A chat has used 95 percent of the context window. What is a problem with asking for a long answer?
Not quite. The reply also uses window space, so a nearly full window leaves little room to answer.
You got 0 of 3 right.
Go deeper
- ToolTiktokenizerTool
Vercel · Paste text, see the exact tokens for different models.
- Video
Andrej Karpathy · Two hours on why tokenization causes so many odd model behaviors.
- ArticleContext RotArticle
Chroma · Measures how model accuracy degrades as the input gets longer.
- Article
Anthropic · Treats context as a finite budget to spend carefully.
Spotted a mistake or something unclear? Report it on GitHub.