Short-term memory is the technique of passing a conversation's history into every LLM call so the AI always knows what was said before — and this lesson shows you exactly how to build it.
Picture this: you're using EngineerPrep's AI tutor to prepare for a Java interview. You type: 'Explain the difference between a List and a Set in Java.' The tutor nails it. Clear, concise, exactly what you needed. So you follow up: 'Which one should I use when I need fast lookups?' The tutor replies: 'Could you clarify what you're referring to?' It's not broken. It just has no memory. Every time you send a message, the AI sees only that one message — nothing before it. It has no idea you were just talking about Lists and Sets. Your first instinct might be: 'Just store the conversation in a database and load it when the user comes back.' That's a good instinct — and it's actually part of the solution. But there's a subtlety that trips up almost every engineer the first time. Storing the conversation and giving it to the AI are two different things.…
Here's the key insight: an LLM (Large Language Model — the AI brain behind the tutor) has no persistent memory between calls. Every time you call it, it starts completely fresh. That's how current LLM APIs are designed. But there's a straightforward workaround. You can remind it of the whole conversation by including every previous message in the call you're making right now. Think of it like a phone call with a colleague who has no memory of your previous conversations. Before asking your question, you read them the transcript of your last exchange. Now they're fully caught up — every single time. That transcript is what engineers call short-term memory (also called conversation history or chat memory ). It lives only for the duration of a session. When the session ends, it's gone — just like how you might forget the details of a quick hallway chat by end of day.…
Imagine a whiteboard with two columns: Your App on the left, The LLM on the right. --- Step 1 — The first message arrives. The user types: 'What is a HashMap?' Your app creates a fresh history list. It holds exactly one item: USER: 'What is a HashMap?' This list is sent to the LLM. The LLM sees one message, answers it. What changed: the history list now has two items — the user's question and the AI's reply. USER: 'What is a HashMap?' AI: 'A HashMap stores key-value pairs and provides average O(1) lookups...' --- Step 2 — The follow-up arrives. The user types: 'How is it different from a TreeMap?' Your app fetches the history list (2 items) and appends the new message: USER: 'What is a HashMap?' AI: 'A HashMap stores key-value pairs...' USER: 'How is it different from a TreeMap?' This 3-item list is sent to the LLM.…