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.
Text split into tokens such as Token, izers and handle, each with its token ID below, and the IDs going into the neural network, which sees only 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.
Modern frontier models support massive context windows. Typical limits in late 2026 range from 128,000 tokens up to 1 or 2 million tokens.
A context window filled by the system prompt, tool definitions, conversation history, retrieved documents and the reply, above typical limits of 128,000, 1 million and 2 million tokens drawn to scale

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.
The three strategies: truncation drops the oldest messages, compaction replaces them with a summary and frees space, and retrieval pulls a relevant chunk in from documents outside the window

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

TermDefinition
TokenA sub-word unit of text processed by the model
TokenizerThe algorithm that splits text into tokens
Token IDThe integer number representing a specific token
Context windowThe maximum total tokens a model can handle in one request
Input tokensThe tokens sent to the model in the prompt
Output tokensThe tokens generated by the model in its reply
TruncationDropping the oldest messages when the window is full
CompactionSummarising older messages to save space
RetrievalPulling in only relevant document chunks just in time
Lost in the middleThe 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.