Short-term and Long-term Memory in AI Applications
When an AI application runs out of room during a long conversation, a tempting shortcut is to assume that the oldest messages are the least useful.
Try it: Stack and Queue
The difference between a stack (last in, first out) and a queue (first in, first out).
How it works
- A stack adds (push) and removes (pop) at the same end, the top — the last item added leaves first.
- A queue adds (enqueue) at the back and removes (dequeue) from the front — the first item added leaves first.
- peek / front look at the next item without removing it.
- Adding to a full structure is an overflow; removing from an empty one is an underflow.
Default run (3 steps): Stack (bottom → top): 4, 7. 2 of 8 slots used. Choose an operation. Example: push 9. … Stack is now: 4, 7, 9. Size 3/8.
Simplified: Fixed capacity of 8 items so overflow can be shown; Python lists and collections.deque grow as needed.
Loading the simulation…
Why Old Messages Are Not Always Expendable
When an AI application runs out of room during a long conversation, a tempting shortcut is to assume that the oldest messages are the least useful. The reasoning is that the conversation has moved on, so the earliest exchanges must be stale, and removing them should cost very little. This belief is common enough that it shapes how many memory systems are built. It is worth examining carefully, because in some conversations it is fatally wrong. The earliest messages can carry the most information, especially when they state what the conversation is for.
The Oldest-First Assumption in Action
Follow what happens to the conversation from the previous example under the simplest possible rule. Each new message enters at the end of the queue. When there is no more room, the message at the front, which is the oldest one, is the first to leave. The diagram traces that sequence. The first message, which sets the goal of the whole task, is the earliest entry, so it is the first to be removed, even though the recent formatting questions are the least important part of the work.
How the FIFO Rule Decides What Leaves
First-in, first-out (FIFO) is a memory strategy that removes the oldest information first. It is the simplest strategy in the source material because it needs no judgment about content. It only tracks order. Each new message enters at the end of the queue, and when the queue must shrink, the oldest entry leaves.
The rule that decides which message leaves is never based on what the message says. It is based only on when the message arrived. That is why FIFO is easy to implement and easy to reason about, and also why it has a predictable blind spot. Because it ignores content, it cannot tell a casual greeting from a sentence that defines the whole task.
Some real systems follow this pattern at the level of the whole conversation. The source notes that API providers such as OpenAI might start removing the beginning of a conversation as it grows. It also notes that frameworks such as LangChain might allow the retention of the last N messages or the last N tokens. In both cases the system keeps the recent part of the conversation and lets the start fall away.
One Context Budget Shared by Two Memories
A more complete picture places FIFO inside a larger design with two memories. Short-term memory holds the recent working material. Long-term memory can store the overflow from short-term memory. For any query, the context given to the model contains both its short-term memory and information retrieved from long-term memory. The two memories therefore draw from the same fixed context limit, and that shared budget is what makes overflow a design problem rather than a storage problem.
The source gives a concrete allocation. If 30 percent of the context is reserved for retrieved material, then the model can use at most 70 percent of the context limit for short-term memory. When that threshold is reached, the overflow can be moved to long-term memory. The key lesson is that the two memories share one fixed budget, so reserving more for one leaves less for the other. Increasing the space for retrieved information shrinks the room available for recent conversation, and that in turn changes how much overflow is produced in the first place.
Tracing a Queue Under a Tight Limit
Applying FIFO to a four-slot short-term memory
Short-term memory can hold four messages. The conversation starts with a goal statement and then receives five more messages. Using FIFO, which messages remain in short-term memory at the end, and what has the system lost?
- 1Message 1 arrives
The goal statement enters the queue. The queue holds one message: the goal.
- 2Messages 2 to 4 arrive
The queue now holds four messages, which is the full capacity. Nothing has been removed yet.
- 3Message 5 arrives
The queue is full, so FIFO removes the oldest entry, which is the goal statement. Message 5 enters at the end.
- 4Message 6 arrives
Message 2 is now the oldest entry and leaves. The queue holds messages 3 through 6.
At the end, short-term memory holds messages 3 to 6. The goal statement is gone, even though it was the most important message for the whole task. The system has no record of what the conversation was for, and every later response is made without that purpose in view.
Reducing Memory Instead of Dropping It
The source describes strategies that avoid dropping information in bulk. Their starting point is human language, which contains redundancy to enhance clarity and to compensate for potential misunderstandings. Repetition and restatement help people, but a memory store pays for every repeated sentence. If redundancy can be detected automatically, the memory footprint shrinks, and fewer items need to be pushed out at all. Two approaches appear in the source: redundancy removal and reflection-based updating.
Comparing the Three Ways to Handle Old Memory
The three approaches differ in what they consult when they change memory. FIFO consults only arrival order. Redundancy removal consults the content, looking for repeated material and recovering what a summary missed. Reflection consults the newest information and the existing memory at every step, so it can decide to insert, merge, or replace. The diagram groups the decision rule and the outcome of each approach side by side.
Mistakes Beginners Make With Memory Strategies
Common mistake Assuming the oldest message is always the least useful
What it looks likeDropping the first message of a data migration conversation because it is oldest.
Why it failsThe source says the earliest messages may carry the most information, especially when they state the purpose of the conversation.
Do this insteadCheck whether an early message states the goal before treating its age as a sign that it can be removed.
Common mistake Treating short-term and long-term memory as separate budgets
What it looks likeReserving more space for retrieved material and expecting short-term memory to be unaffected.
Why it failsBoth memories draw from one context limit, so a larger reserved share leaves less room for short-term memory.
Do this insteadCompute the short-term share from what remains after the retrieved allocation is subtracted.
Common mistake Trusting a summary blindly
Why it failsA summary can drop details. The classifier approach in the source exists because summaries miss key information.
Do this insteadCompare the summary against the original sentences to recover details that were missed.
Common mistake Deleting every contradiction automatically
Why it failsThe source notes that contradictions can confuse an agent but can also help it draw from different perspectives.
Do this insteadDecide, for the use case, which disagreements must be resolved and which can be kept.
Deciding Between Newer Information and Model Judgment
Contradictions are a special case of the memory problem. The source gives two common approaches. Some people keep the newer piece of information when two pieces contradict each other. Others ask an AI model to judge which one to keep. The source adds that how to handle a contradiction depends on the use case, so neither approach is correct everywhere. The flowchart turns that advice into a sequence of questions you can ask about your own application.
When a Contradiction Is Worth Keeping
The source also points out that contradictions are not only a problem. Having contradictions can cause an agent to be confused, but can also help it draw from different perspectives. The design task is therefore not to eliminate every disagreement in memory. It is to decide, for a given use case, which disagreements must be resolved and which can be kept. A single answer is essential when the agent must act on one fact, while several perspectives may be useful when the agent is weighing options.
Applying the Trade-offs to a New Application
A travel-planning assistant stores memory with a four-message short-term limit. The first message says the user wants a budget trip to Lisbon. Later messages discuss hotels and then a conflicting note says the user now wants a luxury trip. Which memory strategy would you choose for the goal statement and for the conflicting notes, and why? Explain which strategy would lose the budget goal, and how your choice would keep the newest preference from silently overwriting something the agent still needs.
Hints
- Ask first whether FIFO would remove the goal statement given the queue order.
- Consider whether a contradiction in the user's preferences is a stale fact or a legitimate change that needs a single answer.
- The flowchart asks whether the use case needs a single answer before deciding whether recency is enough.
Reviewing What Each Memory Strategy Protects
- 1FIFO removes the oldest entry because of its arrival time, not its content, so it can discard the early messages that state a conversation's purpose.
- 2Short-term and long-term memory share one context limit, so each reserved share reduces the room available to the other, and overflow moves from short-term into long-term memory.
- 3Redundancy removal compresses repeated content with summaries and entity tracking, and can recover details a summary missed, while reflection updates memory after each action by inserting, merging, or replacing entries.
- 4For contradictions, keeping the newer entry is a simple rule, asking a model to judge is a more flexible one, and the right choice depends on whether the use case needs a single answer.
Key Takeaways
- FIFO is the simplest memory strategy because it tracks only arrival order, which means it can remove the early message that states a conversation's purpose.
- Short-term and long-term memory share one fixed context budget, so reserving space for retrieved material reduces short-term capacity and determines when overflow moves to long-term memory.
- Redundancy removal and reflection-based updating avoid bulk dropping: one compresses and recovers repeated content, the other updates memory after each action.
- For contradictions, keeping the newer entry is simple, while asking a model to judge is more flexible; the choice depends on the use case, and some disagreements are worth keeping.