0%
⭐ 0 XPMap
Chapter 4 · Quest 4 of 5 · 10 XP

Too Much to Remember

🧠 Models can only read so much at once: that limit is the context window. When a chat gets long, agents keep the system prompt and only the newest messages. Slicing a list makes that easy:

nums = [1, 2, 3, 4, 5]
print(nums[-2:])
print(nums[1:])
print(nums[:1])
[4, 5]
[2, 3, 4, 5]
[1]

[-2:] means "the last 2", [1:] "everything after the first" and [:1] "just the first". + joins two lists into one.

Instructions

  1. Make trim(messages, n).
  2. It returns the first message (the system prompt) plus the last n other messages.
  3. If there are n messages or fewer after the system prompt, it returns them all.
  4. Print how many messages trim(history, 4) keeps.
💡 Hint

messages[:1] is the system prompt in a list. messages[1:] is everything else, and [-n:] on that keeps its last n. Join them with +.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 4: Memory

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.