Fit a chat into the context window
Words stand in for tokens. A real version counts with the model's tokenizer.
The context window is the fixed number of tokens a model can handle in one call, input and output together. The API remembers nothing between calls, so your app re-sends the history each time and must trim it when it grows too long.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write total_tokens(messages, count) that returns the sum of count(text) over (role, text) pairs. Use count = lambda t: len(t.split()) on two messages.
Write drop_oldest(messages, budget, count) that keeps the first message and removes the second, third... one by one until the total is at most budget (never removing the last). Return the kept list of texts.
Explain it without notes
What is the context window, and what counts against it?
Why do chat apps cost more per turn as a conversation gets longer?
How would you handle a conversation that no longer fits the window?
What does lost in the middle mean for how you build prompts?
Expected output
window 60: kept [0, 1, 2, 3, 4, 5] using 47 of 50 input tokens, dropped []
window 40: kept [0, 4, 5] using 24 of 30 input tokens, dropped [1, 2, 3]
window 20: cannot fit, needs 13 tokens but input budget is 10