BeginnerCoder

Lesson 18 — Short-term memory: the conversation window

Keeping the history small

Models read text in tokens — small pieces of words (roughly 3–4 English characters each). Every model has a context window: the most tokens it can read in one call. Providers also charge per token, so a long history is slower and costs more on every call.

Short-term memory is simply the message list. To keep it small, agents use a window: keep the system message (the rules must never be lost) plus only the last few messages.

rest[-4:]   # the last 4 items of a list

Watch out: build a new list — do not delete from the original history; you may want it later. And rest[-0:] is the whole list, not an empty one.

Why it matters: long chats break agents that never trim — they hit the context limit or get expensive.

Want it another way?
Task

Make trim_history() return the system message followed by only the last keep_last messages.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with AI Agents Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.