The Context Window

How the context window limits what a model can read in one call, what apps do when a conversation no longer fits, and how to avoid losing important facts.

12 minLesson 2 of 8
On this page
  1. What counts toward the limit
  2. What apps do when the conversation does not fit
  3. A bigger window does not mean better reading
  4. Pitfalls when sizing and trimming context
  5. What to do in practice
  6. What to practise next

The context window is the maximum amount of text, measured in tokens, that a model can take in and produce in a single call. Because the app resends the whole conversation on every call (What Is Context), a long conversation or a large document eventually stops fitting, and the app has to decide what to leave out. If you do not know how that decision is made, important instructions and facts can disappear without any error message.

A chat that has run for an hour shows the problem. The user said early on that they are allergic to peanuts, the app trimmed the oldest messages to make room, and the model now suggests a peanut topping because it never sees the allergy.

What counts toward the limit

The limit applies to everything sent in one call, not only to what the user typed. The instructions, the descriptions of any tools, the results that tools returned earlier, retrieved documents, every earlier message and the new message all share the same budget, and users usually underestimate how full it is because they see only their own messages.

The reply counts as well. The model writes its answer into the same window, so if the input fills the window almost completely, the reply is cut off or the call is rejected. The size of the window depends on the model and has changed over time, so read it from your model's documentation instead of relying on a number you remember.

Many APIs also set a separate maximum for the length of a single reply, which is usually much smaller than the window. A model that thinks step by step before answering may write that reasoning into the same budget, so a reply can use more of the window than the visible answer suggests.

instructions tools documents history new msg reply input: what the model reads output all of it must fit in one window
Everything the model reads and writes in one call has to fit in a single window, so space for the reply must be reserved before the history is filled in.

What apps do when the conversation does not fit

An app has a few options when the input plus the reserved reply would exceed the window. Each one loses something different, so it matters which one your app or tool uses.

Refuse the request

The app returns an error and asks the user to shorten the input or start a new conversation. Nothing is lost silently, which makes this the safest option, but it interrupts the user. It is the right choice when dropping content would make the answer wrong, for example when the user asks for a summary of one specific document that is too long.

Drop the oldest messages

The app keeps the instructions and the newest messages and leaves out whatever does not fit, starting from the oldest. This is the simplest approach and the most common one, and it fails silently. The model has no way to know that something was removed, so it answers as if the dropped messages never existed. The tracer below shows how that happens with a toy window of 120 tokens, of which 20 are reserved for the reply, so the input may use 100.

Fitting a chat into 100 input tokensUse the arrows or ← → keys
used = system + new messagefor each old message, newest first:  if used + its tokens > budget: stop  keep it, used += its tokenssend system + kept + new message
  1. message
    0allergy
    1ok
    2dinner?
    3recipes
    4noodles?
    5noodle idea
    tokens
    015
    110
    210
    335
    410
    525
    budget
    100
    used
    30

    1There are six earlier messages, oldest on the left. The first says "I'm allergic to peanuts." The instructions (20 tokens) and the new message "Add a crunchy topping?" (10 tokens) are always sent, so 30 tokens are used before any history is added.

  2. message
    0allergy
    1ok
    2dinner?
    3recipes
    4noodles?
    5noodle ideai
    tokens
    015
    110
    210
    335
    410
    525
    budget
    100
    used
    55

    2The loop starts with the newest message, because recent turns usually matter most. 30 + 25 = 55 is within the budget, so the message is kept.

  3. message
    0allergy
    1ok
    2dinner?
    3recipes
    4noodles?i
    5noodle idea
    tokens
    015
    110
    210
    335
    410
    525
    budget
    100
    used
    65

    355 + 10 = 65, which still fits, so the next message is kept.

  4. message
    0allergy
    1ok
    2dinner?
    3recipesi
    4noodles?
    5noodle idea
    tokens
    015
    110
    210
    335
    410
    525
    budget
    100
    used
    100

    4The long recipe list adds 35 tokens: 65 + 35 = 100. That is exactly the budget, so it still fits.

  5. message
    0allergy
    1ok
    2dinner?i
    3recipes
    4noodles?
    5noodle idea
    tokens
    015
    110
    210
    335
    410
    525
    budget
    100
    used
    100

    5Adding the next message would give 100 + 10 = 110, which is over the budget, so the loop stops. Every older message is left out as well.

  6. message
    0allergy
    1ok
    2dinner?
    3recipes
    4noodles?
    5noodle idea
    tokens
    015
    110
    210
    335
    410
    525
    kept
    3
    dropped
    3
    allergy seen
    no

    6The model receives the instructions, three recent messages and the question. The allergy message is gone, so crushed peanuts looks like a reasonable suggestion, and nothing in the app warned anyone.

1 / 6

The most important fact in this conversation was also the oldest one, so a rule of "oldest out first" removed it first. The same function in code looks like this.

// history is oldest first. Each item has a token count.
function fitToWindow(system, history, newMessage, budget) {
  let used = system.tokens + newMessage.tokens;
  const kept = [];
  for (let i = history.length - 1; i >= 0; i--) { // newest first
    if (used + history[i].tokens > budget) break;
    kept.push(history[i]);
    used += history[i].tokens;
  }
  kept.reverse(); // back to oldest first
  return { request: [system, ...kept, newMessage], used };
}
 
const history = [15, 10, 10, 35, 10, 25].map((tokens, i) => ({ id: i, tokens }));
const result = fitToWindow({ tokens: 20 }, history, { tokens: 10 }, 100);
console.log(result.used, result.request.length); // 100 5
# history is oldest first. Each item has a token count.
def fit_to_window(system, history, new_message, budget):
    used = system["tokens"] + new_message["tokens"]
    kept = []
    for msg in reversed(history):  # newest first
        if used + msg["tokens"] > budget:
            break
        kept.append(msg)
        used += msg["tokens"]
    kept.reverse()  # back to oldest first
    return {"request": [system, *kept, new_message], "used": used}
 
history = [{"id": i, "tokens": t} for i, t in enumerate([15, 10, 10, 35, 10, 25])]
result = fit_to_window({"tokens": 20}, history, {"tokens": 10}, 100)
print(result["used"], len(result["request"]))  # 100 5

The loop stops at the first message that does not fit instead of skipping it and continuing with older, smaller ones. Skipping would leave a gap in the middle of the conversation, and the model would see answers to questions that are no longer there.

Summarize the old turns

The app asks a model to condense the older part of the conversation into a short summary and sends the summary in place of the original messages. This keeps the gist of a long conversation in far fewer tokens. The weakness is that a summary keeps what the summarizer judged important, and it can leave out an exact value, a name or a constraint that turns out to matter later. It also costs an extra model call. Memory covers this approach in more detail.

Retrieve only the relevant parts

When the problem is a large body of documents instead of a long chat, the app can store the documents outside the window and put only the passages relevant to the current question into the context. This lets the model work with far more material than fits in any window, but it depends on the search finding the right passages. RAG Basics explains how it works.

A bigger window does not mean better reading

Fitting in the window and being used well are different things. Several effects get stronger as the context grows.

Cost and delay grow, because the whole context is read again on every call, so a long conversation is more expensive and slower on each turn than a short one. Models also tend to use information near the start and the end of a long input more reliably than information in the middle, so one important sentence in the middle of a very long document is easier to miss than the same sentence at the top. And irrelevant material distracts: if you ask about one product while ten similar product sheets are in the context, the model can mix up details between them. The goal is to include what the task needs, not everything that fits. Context Rot covers how long conversations degrade.

Pitfalls when sizing and trimming context

What to do in practice

Work out the budget before you build the prompt. Start with the window size, subtract the space for the reply, then subtract the fixed parts (instructions and tool descriptions), and what remains is what history and documents can use. When a conversation is long, decide on purpose what is allowed to be dropped and what is not, instead of letting the oldest-first rule decide.

If you only use a chat product and do not control the app, the same ideas apply from the outside. Start a new conversation when the topic changes, restate the constraints that matter in your message when a chat has become long, and send the relevant section of a document instead of the whole file when the question concerns one part.

Try it: Does it fit?

A toy model has a 2,000-token window. Your app sends instructions (300 tokens), four tool descriptions (400 tokens in total), a document (1,100 tokens) and the conversation including the new message (200 tokens). You reserve 500 tokens for the reply. Does everything fit? If not, by how much is it over, and what would you cut first?

Show a sample answer

The total is 300 + 400 + 1,100 + 200 + 500 = 2,500 tokens, which is 500 tokens over the window.

The options, roughly in the order you would try them:

  • Trim the document, because it is the largest item. If the question is about one section, send only that section. Cutting the document to 600 tokens fixes the budget by itself: 300 + 400 + 600 + 200 + 500 = 2,000.
  • Remove tools that this task cannot use. Two irrelevant tools would save about 200 tokens and also remove options that the model could misuse.
  • Do not shrink the reply reserve. That only moves the failure to a truncated answer.
Quick check

Which of these use up the context window on a call?

An app trims old messages to fit. Early in the chat the user said "Always answer in Spanish." Forty turns later the replies switch to English. What is the best fix?

Your document fits in the window with room to spare. Why might you still send only the relevant section?

What to practise next

Context Engineering covers how to choose, shrink and order what goes into the window, and Context Rot covers why long chats get worse over time.