Grokking AI: 50 Questions Every Engineer Asks
Vote

0% completed

​

What Is a Context Window and What Happens When It Is Full?

Part 1: What Counts Toward the Window

Part 2: How Large a Window Is

Part 3: What Happens When the Window Is Full

Part 4: The Symptom You See

Common Wrong Beliefs

What This Means for Your Work

Try It Yourself

Check Your Understanding

Key Takeaways

Short answer: The context window is the largest number of tokens that a model can process in one request, and the input and the output are counted together. A long conversation grows until it is larger than the window. The API then returns an error, or the application removes or summarizes the older messages. After the application removes or summarizes messages, the model no longer receives their original text.

The conversation grows and the window keeps its size. The model receives only the messages inside the window
The conversation grows and the window keeps its size. The model receives only the messages inside the window

Look at the two blocks on the left. Messages 1 to 14 are outside the window, and message 1 holds a rule that the engineer wrote at the start.

This lesson follows one long chat. An engineer works with a coding assistant on an order service, and the first message includes this rule:

This service must stay on Python 3.9.

The assistant follows the rule for a long time. In its reply to message 40, it suggests this code:

match order.status: case "paid": ship(order)

The match statement was added in Python 3.10, so this code does not run on Python 3.9. The parts below explain what happened to the rule.

Part 1: What Counts Toward the Window

A model counts text in small pieces called tokens, and What Is a Token and Why Do Models Not Read Words? explains them. The window is a limit on tokens, and almost everything in a request uses some of them.

What Happens Inside an LLM When I Ask a Question? shows that the application builds a full input for every request. That input holds the instructions of the developer (the system prompt), every earlier message of the chat, and any files. It also holds the descriptions of the tools (functions that the application can run for the model) and the results of those tools. All of it counts toward the window.

The parts of one request. The input and the output share one context window
The parts of one request. The input and the output share one context window

Notice the two parts on the right. The answer counts toward the same window, and the thinking tokens that some models write before the answer count toward it too. When the input almost fills the window, little room remains for the answer.

In the example, the messages of the engineer are a small part of the total. The assistant reads source files and test output, and each of them stays in the conversation as a tool result.

Part 2: How Large a Window Is

Every model has a fixed window size, and the sizes differ greatly. The documentation of one provider says that early models could process 8,000 tokens at a time, which is about 6,000 English words by a rough estimate. Many current models accept hundreds of thousands of tokens, and some accept about a million. The same documentation describes a million tokens as about 50,000 lines of code.

Even a large window fills during real work. Suppose that the assistant reads 40 files of 2,500 tokens each. This size is illustrative, which means that it is invented for the example. Those files alone are 100,000 tokens, and every later request sends them again.

A window is also a limit and not a target. Why Not Paste Everything Into a Large Context Window? explains why filling a large window is often the wrong choice.

Part 3: What Happens When the Window Is Full

Three things can happen when the next request does not fit.

Three things that can happen when a conversation no longer fits in the window
Three things that can happen when a conversation no longer fits in the window

Notice the last line of each card, which says what happens to the rule about Python 3.9.

  • An error. The API rejects a request whose input is larger than the window. This is the usual behavior when your own code calls an API directly, and your code must then shorten the input.
  • Dropping the oldest messages. The application removes messages from the start of the conversation until the rest fits. The rule about Python 3.9 is in message 1, so the rule is in one of the first messages that the application removes.
  • Summarizing. The application asks a model to write a short summary of the older messages and replaces those messages with the summary. This method is often called compaction. A summary keeps the main points and can lose exact details, such as a version number.

Many chat applications and coding assistants apply the second or the third behavior automatically, and the user may not notice the change. One more case exists: the window fills while the model writes the answer. The answer then stops early, and Why Did the Model Stop in the Middle of Its Answer? explains that case.

Part 4: The Symptom You See

No error appears when the application drops or summarizes messages. Some applications show a short notice, and in many others the only visible sign is a change in the answers of the assistant.

The model keeps nothing between requests. In the request for message 40, the input holds a summary, the recent messages, or both, and the rule is in none of them. The model produces likely code for the task, and in current Python the match statement is a likely choice. Nothing in the input makes that choice less likely.

The same cause produces other symptoms. The assistant asks for information that you already gave, repeats a mistake that you corrected earlier, or reads the same file a second time. Each of these symptoms suggests that early content is no longer in the input.

Common Wrong Beliefs

BeliefWhat actually happens
The assistant stopped paying attention to my rule.The application removed or summarized the message that held the rule. The model did not receive the rule in the later requests.
The context window counts only the text that I type.The system prompt, the tool descriptions, files, tool results, the answer, and thinking tokens all count toward the same window.
A window of a million tokens cannot become full.Reading 40 files of 2,500 tokens already uses 100,000 tokens, a tenth of that window. More files, test output, and other tool results can use the rest in a long session.
A summary of the chat keeps everything important.A summary holds only what the summarizing model wrote. The summary may not include exact details such as version numbers and names.

What This Means for Your Work

  • Put permanent rules where the application keeps them. Applications send the system prompt with every request and normally do not drop it. Many coding assistants also load a project instructions file at the start of every session, and a rule such as "stay on Python 3.9" belongs there.
  • Repeat an important rule in a long chat. One sentence that repeats the rule places it among the recent messages, inside the window.
  • Start a new chat for a new task. Write a short summary of the important points and begin the new chat with it. The new chat then keeps what you chose and not what the application chose.
  • Count tokens in your own application. Before each request, add the input tokens and the room for the answer, and choose in code which messages to drop or summarize.
  • Treat a broken rule from early in the chat as a sign of a full window. Check the length of the chat before you decide that the model is unreliable.

Try It Yourself

The program below is a very simple chat application with a window of 1,200 tokens. The numbers are illustrative, and a real window is far larger. The first message holds the rule, and the program drops the oldest messages when the conversation no longer fits. Run it and find the turn (one question and its answer) where the last column changes.

Python3
Python3

. . . .

The first run reports that the rule left the input at turn 6. Now change WINDOW to 2400 and run the program again. The rule now leaves the input at turn 12. A window twice as large delays the problem and does not remove it.

Check Your Understanding

Quiz

A chat application drops the oldest messages when a conversation no longer fits in the context window. In message 3, a user wrote "never change the public API". The chat is now far longer than the window, and at message 60 the assistant suggests a change to the public API. What is the most likely explanation?

Click an option to answer

Key Takeaways

  • The context window is a limit on tokens per request. The system prompt, the earlier messages, files, tool results, and the output all count toward it.
  • A full window has three possible results. The API returns an error, or the application drops the oldest messages, or it replaces them with a summary.
  • Text outside the window has no effect on the answer. The model keeps nothing between requests, so the model cannot follow a rule that is not in the input.
  • Permanent rules need a permanent place. Put them in the system prompt or a project instructions file, and repeat them in long chats.

A long chat causes problems even before the window is full. The next lesson, Why Does a Long Conversation Get Slower and Less Accurate?, explains them.

Reading Progress

0%


Vote for new content

On This Page

Part 1: What Counts Toward the Window

Part 2: How Large a Window Is

Part 3: What Happens When the Window Is Full

Part 4: The Symptom You See

Common Wrong Beliefs

What This Means for Your Work

Try It Yourself

Check Your Understanding

Key Takeaways