Command Palette

Search for a command to run...

Hectal
Phase 2: Calling a Model
LLM Engineering · Exercise 2 of 6 PreviousNext
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

  1. A big budget keeps everything
  2. Old turns are dropped first and the system message stays
  3. The result never starts (after system) with an assistant message
  4. 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.