Short-Term Memory
~3 min read
Concept & How It Works
Why Does It Exist?
Multi-turn conversations need continuity without paying to re-send the entire history every call or permanently storing trivial chitchat.
Real-World Analogy
Short-term memory is a whiteboard in a meeting room — useful for the session, erased when everyone leaves.
Visual Workflows
What is Short-Term Memory?
Example
Scenario
A travel-planning chat remembers you said 'budget under $2000' and 'prefer direct flights' across 10 messages, then clears when you close the tab.
Solution
In Agent Memory, apply Short-Term Memory to this scenario: A travel-planning chat remembers you said 'budget under $2000' and 'prefer direct flights' across 10 messages, then clears when you close the tab. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.
Practice Task
Do this before moving to the next module — reading alone is not enough.
Open the Code Walkthrough below and run it locally. Change one parameter related to Short-Term Memory (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.
Code Walkthrough
Highlighted lines show where Short-Term Memory happens in the code.
1class ShortTermMemory: # define a data structure or component2 def __init__(self, max_messages=20): # define a reusable function3 self.messages = [] # key line for Short-Term Memory4 self.max_messages = max_messages # key line for Short-Term Memory5 def add(self, role, content): # define a reusable function6 self.messages.append({"role": role, "content": content}) # key line for Short-Term Memory7 if len(self.messages) > self.max_messages: # key line for Short-Term Memory8 self.messages = self.messages[-self.max_messages:] # key line for Short-Term Memory9 def get_context(self): # define a reusable function10 return self.messages # return the resultCommands to Remember
Commands to Remember
pip install chromadb # vector store for long-term memorypip install redis # fast session / working memorypip install tiktoken # count tokens before injecting memory
Common Mistakes
- Treating Short-Term Memory as a black box without evaluation
- Ignoring cost and latency in production
- Skipping error handling for short term memory
Cheat Sheet
Quick recap — the most important points from this module.
Cheat Sheet
quick ref- •Short-Term Memory
- •Message Buffer
- •Session State
- •Summarization