Tokens and Context Windows Explained
Blog / Tokens and Context Windows Explained
What actually are tokens and context windows? And why can an AI chatbot suddenly forget what you told it earlier?
What is a Token
A large language model does not read words or characters. A tokenizer splits text into tokens, which are often parts of words.It uses a method like Byte Pair Encoding. This starts with single characters and iteratively merges the most frequent adjacent pairs into single tokens until a target vocabulary size is reached.For English text, a common rule of thumb is that one token is about four characters, or roughly three quarters of a word. Code, special characters and non-English languages map less efficiently, so they use more tokens per word.Each token becomes a specific number, known as a token ID. The neural network of the model only ever sees and processes these numbers.
The Context Window
The context window is the maximum total number of tokens a model can handle in one request.This limit is filled by:- The system prompt.
- Tool definitions.
- The conversation history.
- Any retrieved documents.
- The model's own reply.

Input vs Output Tokens
You pay per token when using these LLM APIs. Input tokens and output tokens are priced separately, with output tokens usually costing significantly more.Because the context window is a combined limit, the reply's tokens also take space in the window. A very long prompt literally leaves less room for the answer.The Model Has No Memory
The model itself keeps nothing between requests. Large language models are completely stateless.To maintain a conversation, the client application resends the whole conversation history from scratch every time. Because this history grows, each new turn costs more than the last.When It Overflows
So what happens when the conversation no longer fits?If the combined tokens exceed the limit, the API simply returns an error. To prevent crashes, the chat application silently drops the oldest messages.This is why the model suddenly forgets earlier instructions.Managing the Context Window
There are three main strategies to manage an overflowing history:- Truncation: you drop the oldest messages entirely. This is simple, but the model completely forgets those messages.
- Compaction: when the window is nearly full, older messages are summarised and the short summary replaces them. This keeps the key facts in far fewer tokens, but it can lose granular details.
- Retrieval: you keep documents outside the window and pull in only the relevant chunks. We cover this step in RAG Explained and Embeddings and Vector Databases.

Bigger Isn't Always Better
A bigger context window is not a free fix for these problems. Longer prompts cost more money and take longer to process.Models also tend to use information at the start and end of a long prompt better than information in the middle. This effect is known as lost in the middle.The model struggles to extract or use facts buried deep inside a very long string of tokens.Recap
| Term | Definition |
|---|---|
| Token | A sub-word unit of text processed by the model |
| Tokenizer | The algorithm that splits text into tokens |
| Token ID | The integer number representing a specific token |
| Context window | The maximum total tokens a model can handle in one request |
| Input tokens | The tokens sent to the model in the prompt |
| Output tokens | The tokens generated by the model in its reply |
| Truncation | Dropping the oldest messages when the window is full |
| Compaction | Summarising older messages to save space |
| Retrieval | Pulling in only relevant document chunks just in time |
| Lost in the middle | The effect where a model struggles to use facts buried in a long prompt |
About TechPrep
Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.