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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based AI Agents Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for AI Agents Foundations books — check the publication year so the examples match current versions.
Search Amazon →