Command Palette

Search for a command to run...

Hectal
Phase 1: How LLMs Work (for Engineers)
LLM Engineering · Exercise 5 of 5 Previous
Intermediate

Trim a chat to fit the context window

Write trim_history(messages, budget, count). messages is a list of (role, text) pairs, count(text) returns a token count and budget is the most input tokens allowed.

Keep the first message (the system prompt) and the last message. While the total is over budget, remove the oldest message between them. Return the kept list.

If the first and last messages alone are over budget, raise ValueError with a message that contains the word budget.

What Check tests

  1. Nothing is dropped when everything fits
  2. The oldest middle messages go first
  3. The system prompt and the newest message are always kept
  4. The input list is not changed
  5. Too big even alone raises ValueError mentioning budget

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.