AI Engineer Roadmap All lessons Interactive Quiz MernStackDev GitHub

Context Window

Play the interactive lesson: Context windowRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Beginner · Reading time: 7 min · Phase: 3 · LLM Concepts

In one line: The context window is the maximum number of tokens a model can handle in one request, counting your input and its output together.

Before you start: KV Cache (optional, only for step 4 of "How it works").

Words to know:

Explain it like I'm new

Think of a whiteboard. Everything the model can "see" right now must fit on it: the system instructions, the chat history, any documents you pasted, and the answer it is writing. When the board is full, something has to be erased.

The model has no memory between requests. Each API call starts with a blank board. Chat apps give the illusion of memory by re-sending the conversation every time. That is why long chats cost more and eventually hit the limit.

Sizes are measured in tokens, not words. In English a token is often about three-quarters of a word, but it varies by language and tokenizer. Code, numbers, and non-English text often use more tokens per word.

Watch it

Context window: The most tokens a model can handle in one request, input and output together.
Context window: The most tokens a model can handle in one request, input and output together.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Your request is turned into tokens: system prompt + messages + tool definitions + documents.
  2. The model also reserves room for output, often set by a max_tokens parameter.
  3. If input_tokens + max_output_tokens exceeds the window, the API returns an error or the app must cut something.
  4. Attention is the step where every token looks at every other token. Its cost grows with the square of the length (n²). The KV cache (stored per-token data the model reuses) grows in step with the length. So longer contexts are slower and more expensive.
|<------------------- context window (e.g. 200k tokens) ------------------->|
| system prompt | tool specs | retrieved docs | chat history | new question | answer |
                                                                            ^ max_tokens reserved

Worked example

Window = 8,000 tokens. System prompt 500, tools 700, history 3,000, question 300, reserve 1,000 for the answer.

  1. Add up what is already used: 500 + 700 + 3,000 + 300 + 1,000 = 5,500.
  2. Subtract from the window: 8,000 - 5,500 = 2,500 tokens left for retrieved documents.
  3. Now the user pastes a 3,000-token document. 3,000 is more than 2,500, so it is 500 tokens over.
  4. Something must give. Options: summarize the history (say it shrinks from 3,000 to 1,000, freeing 2,000, so the document now fits with 1,500 to spare), or cut the document.

Bigger is not always better. Studies such as Lost in the Middle found that models often use information at the start and end of a long context better than information buried in the middle. Long windows also add cost and latency.

Ways to manage a full window:

Code example

A simple token budget with a rough estimate (real code should use the provider's tokenizer or token-counting endpoint).

def rough_tokens(text):
    return max(1, len(text) // 4)      # rule of thumb only, ~4 characters per token in English

def fit_history(system, history, question, window=8000, reserve=1000):
    budget = window - reserve - rough_tokens(system) - rough_tokens(question)
    kept = []
    for msg in reversed(history):       # keep the newest messages first
        cost = rough_tokens(msg)
        if cost > budget:
            break
        kept.insert(0, msg)
        budget -= cost
    return kept

history = [f"message {i} " * 200 for i in range(20)]
print(len(fit_history("You are helpful.", history, "What did we decide?")))  # how many old turns still fit

In real projects count tokens with the actual tokenizer, for example OpenAI's tiktoken package or the Anthropic API's token-counting endpoint, because the 4-characters rule can be far off.

When to use it / when not to

ApproachCost per requestFreshnessGood for
Stuff everything in contexthighper requestone long document
RAGlowupdate the indexbig knowledge bases
Summarize historylowlossylong chats

Common mistakes

Recap

If you remember only three things:

Interview questions

1. Does the context window include the output? <details><summary>Answer</summary> Yes. Input tokens plus generated tokens must fit inside the window. APIs usually also cap output separately with max_tokens. </details>

2. Why can a longer prompt give worse answers? <details><summary>Answer</summary> Relevant facts can be diluted by noise, models tend to use the middle of long contexts less reliably, and contradictory or outdated text may be included. Cost and latency also rise. </details>

3. How would you handle a chat that exceeds the window? <details><summary>Answer</summary> Keep the system prompt, keep recent turns, summarize or drop older ones, and move durable facts into long-term memory or a retrieval store that you query when needed. </details>

Go deeper

Prev: KV Cache · Next: Temperature · Back to phase: Phase 3