Back to Blog

Understanding Context Windows in LLMs

The One Thing Nobody Explains Properly

If you have ever used ChatGPT, Gemini, Claude, or any other large language model, you have run into the context window — whether you realized it or not.

Maybe you were in the middle of a long conversation and the model suddenly "forgot" something you told it twenty messages ago. Maybe you tried pasting an entire document and got an error saying the input was too long. Maybe the model started contradicting itself halfway through a coding session.

All of these problems trace back to one concept: the context window.

This blog is going to break it down from scratch. No jargon soup. No hand-waving. By the end, you will understand what a context window actually is, why it exists, what happens when you hit its limits, and how to work with it instead of fighting against it.

So What Is a Context Window?

Let me start with an analogy.

Imagine you are sitting at a desk. On this desk, there is a whiteboard of a fixed size. Every time someone talks to you, they write their message on the whiteboard. Every time you respond, you write your response on the same whiteboard.

The whiteboard has a fixed amount of space. Once it fills up, you have two choices: stop writing, or erase the oldest stuff to make room for new stuff.

That whiteboard is the context window.

More precisely, the context window is the maximum amount of text (measured in tokens — more on that soon) that a large language model can "see" at any given moment. It includes everything: the system instructions, your messages, the model's responses, any documents you pasted in, any code you shared — all of it.

The model does not have a separate long-term memory. It does not remember your previous conversations (unless the application layer saves and re-injects them). The context window is literally everything the model knows about the current interaction.

Let that sink in for a second. When you talk to ChatGPT, it does not "remember" you from yesterday. What happens is the application saves your old messages and quietly pastes them back in when you open the same conversation. The model itself only sees what is on the whiteboard right now.

Wait, What Are Tokens?

Before we go deeper, we need to talk about tokens, because context windows are measured in tokens, not words or characters.

A token is a chunk of text that the model processes as a single unit. Think of it like this: when you read, you do not read letter by letter. You recognize whole words, and sometimes common word parts (like "un-" or "-ing"). Tokens work similarly.

Here are some rough rules of thumb for English text:

  • A short, common word like "the" or "cat" is usually 1 token.
  • A longer word like "understanding" might be 1 or 2 tokens.
  • A very long or uncommon word like "pneumonoultramicroscopicsilicovolcanoconiosis" will be split into several tokens.
  • Spaces and punctuation are often bundled with adjacent text into a single token.
  • On average, 1 token is roughly 3/4 of a word, or about 4 characters.
So when someone says a model has a "128K context window," they mean it can process about 128,000 tokens at once — which is roughly 96,000 words or about 300-400 pages of a novel.

When someone says a model has a "4K context window," that is only about 3,000 words — maybe 6-8 pages.

The number matters a lot.

A Quick History: How Context Windows Have Grown

This part is interesting because it shows how fast things are moving.

The original GPT-2 (released in 2019) had a context window of 1,024 tokens. That is about 750 words. You could barely fit a short essay in there.

GPT-3 (2020) bumped it up to 2,048 tokens for the base model, and later versions went up to 4,096.

GPT-3.5 (early 2023) gave us 4,096 tokens, and then a 16K variant arrived.

GPT-4 (2023) launched with an 8K version and a 32K version.

Claude 2 (2023) made a splash with 100K tokens — enough to process an entire book in one go.

By 2024-2025, we started seeing models with 128K, 200K, and even 1 million+ token context windows (Gemini 1.5 Pro pushed to 1M tokens, and later models expanded further).

But here is the catch — bigger is not always better. We will get to why in a bit.

Why Does the Context Window Have a Limit at All?

This is a fair question. If bigger context windows are useful, why not just make them infinite?

The answer comes down to how these models work under the hood. I will keep this simple.

LLMs are built on something called the Transformer architecture. At the heart of a Transformer is a mechanism called self-attention, which is how the model figures out which parts of the input are relevant to which other parts.

Here is the problem: self-attention needs to compare every token with every other token. If you have 1,000 tokens, that is 1,000 × 1,000 = 1,000,000 comparisons. If you have 100,000 tokens, that is 100,000 × 100,000 = 10,000,000,000 comparisons.

The computational cost grows quadratically — meaning if you double the context length, you roughly quadruple the cost. Triple the context, and the cost goes up by nine times.

This has three practical consequences:

  1. Memory: Longer context windows require more GPU memory. A 128K context window needs significantly more VRAM than a 4K one.
  2. Speed: More tokens means more computation, which means slower responses. You might notice that when you paste a very long document, the model takes longer to respond.
  3. Cost: For API users, you typically pay per token. Sending 100K tokens in a single request costs way more than sending 1K.
Researchers have been working hard on making attention more efficient (things like FlashAttention, sparse attention, linear attention, and ring attention), which is partly why context windows have been able to grow so dramatically. But the fundamental tradeoff still exists.

The "Lost in the Middle" Problem

Here is something that surprised a lot of people when researchers first demonstrated it.

Even when a model has a huge context window — say, 128K tokens — it does not pay equal attention to every part of that window. Studies have shown that models tend to recall information near the beginning and the end of the context much better than information buried in the middle.

This is called the "Lost in the Middle" phenomenon, and it was documented in a 2023 paper by researchers at Stanford and elsewhere.

Think of it like reading a really long email. You remember how it started, you remember how it ended, but the stuff in paragraphs 7 through 12? It gets blurry.

What this means practically:

  • If you paste a 50-page document and ask a question about something on page 25, the model might miss it or give a vague answer.
  • If you put your most important instructions at the very beginning or the very end of your prompt, the model is more likely to follow them.
  • Just because a model CAN accept 128K tokens does not mean it processes all 128K tokens with equal quality.
This is one of the reasons why "bigger context window" does not automatically mean "better." The quality of attention across the window matters just as much as the size.

What Actually Goes Into the Context Window?

This is something beginners often do not realize. The context window is not just your messages. It includes a bunch of stuff you might not see.

Here is a typical breakdown of what fills up the context window during a ChatGPT-style conversation:

1. System Prompt

This is the hidden instruction that tells the model how to behave. You usually do not see it, but it is there. For ChatGPT, this can be several hundred to a few thousand tokens. It includes things like "You are ChatGPT, a helpful assistant..." along with various behavioral rules.

2. Conversation History

Every message you have sent, and every response the model has given, in the current conversation. All of it gets re-sent to the model every single time you send a new message. The model is not "continuing" a conversation — it is re-reading the entire conversation from scratch each time.

3. Your Latest Message

Whatever you just typed or pasted.

4. Any Attached Files or Context

If you upload a PDF, paste code, or attach images (for multimodal models), those get converted into tokens and stuffed into the context window too.

5. Tool/Function Definitions

If the model has access to tools (like web search, code execution, or file reading), the descriptions of those tools are included in the context window. This can eat up a surprising number of tokens.

6. The Model's Own Response

The response the model is currently generating also counts toward the context window. So if the model writes a 2,000-token response, that is 2,000 tokens of the window used up.

All of these compete for space in the same fixed-size window. This is why long conversations eventually "break" — the accumulated history fills the window, and older messages get dropped.

What Happens When You Hit the Limit?

Different applications handle this differently, but here are the common strategies:

Strategy 1: Truncation

The simplest approach. Once the conversation gets too long, the application simply chops off the oldest messages. The model never sees them again. This is why the model "forgets" things from earlier in a long conversation — those messages were literally deleted from the context to make room.

Strategy 2: Sliding Window

A slightly smarter version of truncation. The application keeps the system prompt and the most recent N messages, dropping everything in between. Some implementations also keep a summary of the dropped messages.

Strategy 3: Summarization

The application periodically asks the model (or a smaller model) to summarize the conversation so far. The summary replaces the full history, freeing up space. This preserves the gist but loses specific details.

Strategy 4: RAG (Retrieval-Augmented Generation)

Instead of stuffing everything into the context, the application stores information externally (in a database or vector store) and only retrieves the relevant pieces when needed. This is how many enterprise AI products work — they search your documents and inject only the relevant snippets into the context.

Strategy 5: Hard Rejection

The application simply refuses the request. You get an error like "This message is too long" or "Please shorten your input." This is common with API usage where there are strict token limits.

Each strategy has tradeoffs. Truncation is fast but lossy. Summarization preserves more but introduces inaccuracy. RAG is powerful but adds complexity.

Context Window vs. Memory: They Are Not the Same Thing

This is a distinction that confuses a lot of people, so let me be very clear.

The context window is the model's working space for a single interaction. It is temporary. When the conversation ends, the context window is gone.

Memory (in the way apps like ChatGPT use the term) is a feature built on top of the model by the application layer. The app saves certain facts ("User prefers dark mode," "User is a software engineer in Bangalore") and injects them into the system prompt at the start of each new conversation.

The model itself has no memory. It is stateless. Every time it generates a response, it starts from scratch with whatever is in the context window. The feeling of continuity is an illusion maintained by the application.

This also means that if an application has a "memory" feature, those memories take up tokens in the context window. The more memories saved, the less room there is for your actual conversation.

How Context Windows Affect Different Use Cases

Let me walk through some practical scenarios so you can see why context windows matter in real life.

Chatbots and Assistants

For casual chatting, a 4K-8K window is usually fine. Short questions, short answers. But if you are having a deep, multi-turn conversation about a complex topic, you will start hitting limits around 20-30 exchanges. The model will start losing track of what you discussed earlier.

Code Generation

This is where context windows really matter. A single source file can easily be 500-2,000 tokens. If you are working on a feature that spans multiple files, you might need 10,000-50,000 tokens just to give the model enough context about your codebase. Models with larger context windows can "see" more of your code at once, leading to more coherent suggestions.

Document Analysis

Want to ask questions about a 100-page PDF? That PDF might be 40,000-60,000 tokens. You need a model with a large enough context window to fit the entire document, plus room for your question and the model's answer. This is exactly why the jump to 100K+ context windows was such a big deal.

Summarization

Summarizing a long document requires the model to "see" the whole thing. If the document is longer than the context window, you have to summarize it in chunks and then summarize the summaries — which introduces information loss at each step.

Translation

Translating a book chapter by chapter works fine with smaller context windows. But if you want consistent terminology across chapters, the model needs to see enough context to maintain consistency.

Agents and Tool Use

AI agents that use tools (browsing the web, running code, querying databases) accumulate a lot of context very quickly. Each tool call and its result gets added to the context window. Complex agent tasks can burn through a 128K window faster than you would expect.

Practical Tips: Working With Context Windows

Now that you understand how context windows work, here are some practical tips for getting the most out of them.

1. Front-Load Important Information

Put your most critical instructions, constraints, and context at the very beginning of your prompt. Because of the "lost in the middle" effect, information at the start gets more attention.

2. Be Concise

Every unnecessary word uses tokens. If you are pasting code, trim out the parts that are not relevant. If you are sharing a document, extract the relevant sections instead of pasting the whole thing.

3. Start Fresh When Conversations Get Long

If your conversation has been going on for a while and the model starts making mistakes or forgetting things, start a new conversation. Copy over only the essential context. A fresh, focused conversation often works better than a stale, overloaded one.

4. Use Structured Formats

When giving the model a lot of information, use clear headers, bullet points, and labeled sections. This helps the model parse and prioritize the information more effectively. Think of it as making the whiteboard organized instead of cluttered.

5. Repeat Key Instructions

If you have a critical instruction that the model must follow throughout a long conversation, repeat it periodically. This counteracts the tendency for the model to "drift" away from early instructions as the context fills up.

6. Choose the Right Model for the Job

If you are analyzing a short email, you do not need a 1M-token model. If you are processing a research paper, a 4K model will not cut it. Match the model's context window to your task.

7. Understand Token Counting

Most model providers offer token-counting tools or APIs. Use them. Knowing how many tokens your input consumes helps you plan and avoid hitting limits unexpectedly. OpenAI has a tokenizer tool on their website. Anthropic publishes token counts in their API responses.

The Future: Where Are Context Windows Heading?

Context windows have been growing rapidly, but the trend is not just about raw size. Several interesting directions are emerging:

Longer Windows, Better Attention

Researchers are working on attention mechanisms that scale more efficiently, making it possible to have million-token windows without the quadratic cost explosion. Techniques like Ring Attention, Infini-Attention, and various sparse attention methods are making this practical.

Hybrid Approaches

Instead of cramming everything into the context window, future systems will likely combine short context windows with external memory systems. The model uses its context window for the immediate task and retrieves information from external storage as needed — kind of like how humans use short-term memory plus the ability to look things up.

Persistent Memory

Some models are starting to develop true persistent memory — the ability to remember information across conversations without it being manually saved and re-injected. This is still early and comes with privacy and accuracy concerns, but it is a clear direction.

Compression

Rather than making the window bigger, another approach is to make the content inside it smaller. Context compression techniques can condense long documents into shorter representations that preserve the key information but use fewer tokens.

Caching

Services like Anthropic have introduced "prompt caching," where if you send the same large document repeatedly (like in an API application), the system caches it so you do not pay the full cost each time. This does not change the context window size but makes large-context usage more economical.

Common Misconceptions

Let me clear up a few things that people often get wrong.

"The model remembers our conversation from last week." No. The application might save and re-send parts of it, but the model itself has no memory between sessions.

"A 1M token context window means the model is 10x smarter than a 100K one." Not at all. Context window size and model intelligence are different things. A smaller model with a smaller window can be "smarter" (better at reasoning, more accurate) than a larger model with a bigger window.

"I should always use the maximum context window." Not necessarily. Larger context means higher cost, slower responses, and potential quality degradation in the middle. Use what you need.

"The model reads the context window like a human reads a book — start to finish." Not quite. The model processes the entire context in parallel through attention mechanisms. It does not read sequentially. But due to positional encoding and training patterns, it still has preferences for where information appears (beginning and end tend to get more attention).

"Tokens are the same as words." Close but not exact. Tokens are sub-word units. The exact tokenization depends on the model's tokenizer. Different models tokenize the same text differently, which is why token counts can vary between models.

Putting It All Together

The context window is, in many ways, the most important constraint of current LLMs. It determines:

  • How much of your conversation the model can consider
  • How large a document it can analyze
  • How much code it can work with at once
  • How long a conversation can go before quality degrades
  • How much it costs to run a query
Understanding it gives you a real advantage. Instead of being frustrated when the model "forgets" something or gives a weird answer to a long prompt, you will know exactly why it happened and what to do about it.

The context window is not a bug. It is a fundamental architectural feature. And while it keeps getting bigger and more efficient, it will remain a key consideration for anyone building with or using LLMs for the foreseeable future.

The next time you are mid-conversation with an AI and something feels off — the model contradicts itself, loses the thread, or misses something obvious — take a step back and think about the whiteboard. How full is it? What is on it? What got erased?

Once you start thinking in terms of context windows, a lot of the quirks of LLMs suddenly make a lot more sense.

Thanks for reading. If this helped you understand something that felt confusing before, that is exactly what I was going for. Drop your questions or thoughts — I would love to hear what clicked for you.