The Context Window: Why Models Forget

A model reads only a fixed number of tokens at once. As a chat grows, the oldest words fall out of the window, and the model can no longer see them.

AI · beginner · 6 min · 17 steps · free

What you'll learn

context_window.py

# A model can only read a fixed number of tokens at once: its context window.
window = 6
chat = []

for message in ["my name is asha", "i like tea", "what is my name"]:
    chat += message.split(" ")
    seen = chat[-window:]     # anything older falls out of the window
    print(seen)

remembers = "asha" in seen
print("can it still see the name?", remembers)

Step by step

  1. This model can read 6 tokens at once. Real models read thousands to millions, but the limit always exists.
  2. The whole conversation so far.
  3. The user's first message.
  4. Add its words to the chat.
  5. The model sees the last 6 words. The chat has only 4, so it sees everything, including "asha".
  6. It prints ['my', 'name', 'is', 'asha'].
  7. message changes from 'my name is asha' to 'i like tea'.
  8. Three more words: the chat now has 7.
  9. 7 words but a window of 6: the first word, "my", has fallen out.
  10. It prints ['name', 'is', 'asha', 'i', 'like', 'tea'].
  11. message changes from 'i like tea' to 'what is my name'.
  12. Four more words: 11 in all.
  13. The model now sees "like tea what is my name". The name has gone.
  14. It prints ['like', 'tea', 'what', 'is', 'my', 'name'].
  15. Python moves to the next item of the loop: for message in ["my name is asha", "i like tea", "what is my name"].
  16. Is "asha" anywhere in what the model can see?
  17. No. Asked "what is my name", this model cannot know: the answer has fallen out of its window. Apps work around this by summarising old messages, keeping key facts pinned, or looking things up (RAG).

Common mistakes

Best practices

Key takeaways

Check yourself

A model's window is 4 tokens. The chat is "a b c d e f". What can it see?

Answer: c d e f. It keeps the most recent 4 tokens.

Which does NOT help with a small context window?

Answer: Asking the same question louder. Only putting the right information inside the window helps.

·

More in Inside a Language Model