Demystifying LLM Context Windows: How AI Memory Works (and Why It Fails)
Imagine asking an AI coding assistant to help refactor a complex application. At first, it gives sharp, accurate responses. But 20 messages into the session, it suddenly forgets the architecture rules you set at the beginning, re-introduces previously fixed bugs, or hallucinates functions that don't exist. What went wrong? You just ran into the boundaries of the Context Window . Whether you are a…
Large Language Models (LLMs) struggle with retaining information across an interaction, akin to short-term memory loss in a highly intelligent specialist. Each new user message forces the model to process the entire conversation anew. The context window denotes the maximum amount of information that an LLM can actively remember during a single interaction. Exceeding this limit causes the model to lose older context, leading to mistakes or hallucinations.
The foundation of context windows involves two main steps: tokenization and embeddings. Tokenization breaks text into smaller units, known as tokens, which can be individual words, parts of words, punctuation marks, or even single letters. Embeddings convert these tokens into numerical vectors, facilitating the AI's understanding of meaning.
Traditional embeddings, like Word2Vec and GloVe, assign static vectors to words, while contextual embeddings, used by modern Transformer-based models, generate dynamic vectors based on context.
When you engage with an AI, multiple data streams vie for space in the context window. Key components include system prompts, tool definitions, document and code attachments, retrieval-augmented generation context, and the conversation history. System prompts are hidden instructions set by developers, while tool definitions inform the AI about available external APIs or tools.
Document and code attachments are the files or code snippets shared in the conversation. Retrieval-augmented generation context consists of relevant text snippets fetched from external databases during query processing. Conversation history includes the entire exchange of previous messages.
The engineering behind context windows is governed by the self-attention mechanism in Transformers. This mechanism calculates the relationships between every token and every other token in the context window, resulting in a quadratic scaling complexity (O(N²)). As the context length increases, the computational requirements grow exponentially, making it impossible to have infinitely large context windows. Doubling the context length quadruples the processing power and memory needed.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.