Intermediate level · Module 7: Short-Term and Long-Term Memory · Lesson 1 of 3
Short-Term Memory and Context Limits
Learners keep conversations within the context window by trimming and summarising history while preserving the instructions and key facts.
In this lesson you will
- Trim conversation history within a token budget
- Summarise older history safely
Key concepts
message buffertoken estimatesliding windowrunning summarypinned instructions and facts
The lesson
Short-term memory is the message history sent with each request. It grows with every turn, and each token costs money and context space.
A sliding window keeps the most recent messages; a running summary replaces older messages with a short summary written by the model. Instructions and key facts (the user's programme, the question being researched) are pinned so that they are never dropped.
Summaries can lose or distort details, so test that important facts survive summarisation.
Step by step
- Write a function that estimates the tokens of the message list.
- Implement a sliding window with pinned instructions.
- Add a running summary for older messages.
- Test a 15-turn conversation and check that key facts are kept.
Practice activity
Implement short-term memory management for a multi-turn research session.
Expected result: Conversations stay within budget and key facts survive.
Check your understanding
-
Why must instructions be pinned?
Show answer
So that they are never dropped when history is trimmed.
-
What is a risk of summarising history?
Show answer
Important details can be lost or distorted.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.