Command Palette

Search for a command to run...

Hectal
Phase 14: Interviews and Capstone
LLM Engineering · Exercise 3 of 5 PreviousNext
Intermediate

Trim the history to a token budget

Write trim_history(messages, budget). The first message may be a system message; always keep it. Then keep the newest messages that fit in the remaining budget, stopping at the first message that does not fit (no gaps). Use the given count_tokens.

After choosing, drop any leading kept messages that are not from the user, so the history starts with a user message. If the system message alone is bigger than the budget, raise ValueError. Also raise ValueError when there are messages after the system message but none can be kept, because a request without the user's latest question is useless.

What Check tests

  1. Everything fits: nothing is dropped
  2. The system message is always kept and the history fits the budget
  3. The newest message is kept and the history starts with a user message
  4. No gaps: older small messages are not used to fill space
  5. A system message over the budget raises ValueError
  6. If not even the newest message fits, it raises ValueError

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.