Lesson 4 of 5 · 2 min
The context window
Everything the model can use for a reply has to fit in its context window: instructions, history, documents and the answer itself. Anything outside it does not exist for that call.
limit = 120
history = [f"message {i} " + "word " * 20 for i in range(10)]
kept, used = [], 0
for m in reversed(history):
size = len(m.split())
if used + size > limit:
break
kept.insert(0, m)
used += size
print(len(kept), "of", len(history), "messages fit:", [k.split()[1] for k in kept])
