Beginner
Trim a conversation to a token budget
estimate_tokens(m) and total(messages) are given. Write trim(messages, budget) that returns a new list: it keeps every system message, then drops the oldest other messages until total is at most budget.
Afterwards, if the first remaining non-system message is from the assistant, drop it too (its question is gone). Do not change the list you were given.
What Check tests
- A big budget keeps everything
- Old turns are dropped first and the system message stays
- The result never starts (after system) with an assistant message
- The input list is not changed
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.