The Context Window12 of 37
This exercise is optional. It's for readers who know a little Python, and it practises what the Everything It Can See lesson taught: everything in the request, plus the reply, has to fit in the context window.
Your program gets the size of a context window and the token counts of what goes into one request:
window 950
reply 200
fixed system 300
fixed tools 150
fixed notes 100
message 100
message 100
message 100
message 50window is the context window, in tokens.reply is how many tokens to keep free for the model's reply.fixed line is something that is always in the context (the app's instructions, the tool descriptions, notes): it can't be dropped.message line is one message of the conversation, oldest first. The last one is the newest: the question the model has to answer now.Write the function messages_to_drop. It returns how many of the oldest messages must be removed so that the fixed part, the messages that are kept and the reply all fit in the window together:
0;-1.The code that reads the input and prints the answer is already written. For the example above it should print:
drop the oldest 2 message(s)
room left for the reply: 250Real apps are cleverer than this (they summarise old messages instead of just dropping them, as you'll see in When the Window Fills Up), but the arithmetic underneath is the same.
Sign in to try this exercise.