Begin with the problem
Without stored information, every request begins almost from scratch. Agent memory decides what should persist, how it is retrieved, and when it should expire.
store useful information → retrieve relevant items → add to context → make the next decision
What you will learn
- Distinguish context, conversation history, state, short-term memory, and long-term memory.
- Follow how stored information is selected and inserted into a later model call.
- Understand that memory is application-managed data, not human-like remembering.
- Design retention, deletion, privacy, and relevance rules for memory.
Current real-system grounding: OpenAI’s official agent quickstart includes tools and handoffs, while Google’s Agents overview lists current agent frameworks and managed agents.
These official links document available product features. They do not reveal every provider’s private implementation, hidden reasoning, default setting, or internal limit.
1. The problem this module solves
You’ve encountered “state” since Module 3 and “context” since Module 5. This module introduces memory properly — and, critically, draws a precise, explicit line between four terms that are easy to conflate: state, context, conversation history, and memory. Getting this distinction right is worth real, dedicated attention.
2. Why Memory Is Needed
Without memory: an agent starts every new session with
zero knowledge of any prior interaction -- even with
the SAME user, about the SAME ongoing issue.
With memory: an agent can recall relevant facts from
PAST sessions -- "this customer previously mentioned
they prefer email contact" -- without that information
needing to be re-stated every single time.
3. The Critical Distinction — Four Different Concepts
This is worth stating with maximum precision, since these terms are , commonly conflated:
| Concept | What It Is | Lifetime |
|---|---|---|
| State | The agent’s evolving understanding within the current task (Module 3, 12) | Exists only for the current task’s duration |
| Conversation History | The raw, turn-by-turn log of what was said | Can span one session, or be persisted longer |
| Memory | persistent knowledge the agent deliberately retains across sessions | Persists across sessions, deliberately curated |
| Context | Everything actually assembled and given to the LLM for one specific decision (Module 3, 5) | Assembled fresh, each time — not stored itself |
The single most important distinction: CONTEXT is not a stored thing at all — it is ASSEMBLED, at each decision point, typically FROM some combination of state, relevant memory, and recent history. Conflating “context” with “memory” is a common source of confusion around this vocabulary.
4. The Human Analogy — Working Memory vs. Long-Term Memory
A human employee's WORKING MEMORY: what they're actively thinking
about RIGHT NOW, for the task in
front of them -- analogous to STATE
A human employee's LONG-TERM MEMORY: things they've LEARNED and RETAINED over time
-- "this customer always prefers
email" -- analogous to an
agent's persistent MEMORY
Just as a human doesn’t re-derive everything they know from scratch every single conversation, a well-designed agent shouldn’t either — this is precisely the problem persistent memory solves.
5. Types of Memory
SHORT-TERM / WORKING MEMORY: scoped to the CURRENT
task -- discarded once the task
completes (closely related to
"state," Module 12 covers this
relationship precisely)
LONG-TERM MEMORY: PERSISTS across
sessions -- deliberately stored
and retrieved when relevant
EPISODIC MEMORY: memory of SPECIFIC PAST
EVENTS or interactions ("last
Tuesday, this customer asked
about a refund")
SEMANTIC MEMORY: general FACTS or
KNOWLEDGE, not tied to a
specific past event
("this customer prefers
email contact")
This directly connects to your RAG course: long-term memory is often implemented using exactly the retrieval mechanisms you already know — embedding past interactions or facts, and retrieving the relevant ones when assembling context for a new decision (Section 3’s “context is assembled FROM memory,” made concrete).
6. Memory Retrieval, Update, and Deletion
RETRIEVAL: given the CURRENT situation, which STORED memories
are RELEVANT to include in context? (directly
your RAG course's retrieval problem, applied here)
UPDATE: when should a NEW fact be ADDED to long-term
memory? (NOT every single interaction
detail -- selectivity matters)
DELETION: when should a memory be REMOVED or considered
STALE? (directly connecting to your RAG course's
freshness and versioning discussion)
7. A Real Developer Example
TechCorp’s support agent, with all four concepts distinguished in one interaction:
| Concept | Concrete Example |
|---|---|
| State | “order_status: late, carrier_status: delivered” — built up during THIS specific ticket |
| Conversation History | The raw back-and-forth: “Customer: my order is late” → “Agent: let me check” → “Agent: I see it was marked delivered” |
| Memory | Retrieved from a PRIOR session: “this customer previously complained about a similar issue and prefers a refund over a replacement” |
| Context (assembled for THIS decision) | Current state + the relevant retrieved memory fact + the most recent observation — combined fresh, right now, to help the LLM decide what to do next |
8. A Simple Agentic AI Connection
Memory is precisely what lets a multi-agent system (Module 15) or a long-running agent maintain real continuity — an agent that retrieves relevant long-term memory before reasoning behaves noticeably more consistently and personally than one that starts fresh every time, directly connecting to Module 13’s “agent with memory” architecture.
9. How Is This Used in AI?
🤖 How Is This Used in AI?
Production agent systems implement long-term memory almost exactly like a RAG system — storing past interactions or learned facts as embeddings, retrieving relevant ones for the current situation, and including them in the assembled context (Section 3) for the LLM’s next decision, directly reusing your RAG course’s entire retrieval pipeline for this purpose.
10. Real-World Applications
- Customer support agents that remember prior interactions with the same customer
- Personal assistants that learn and recall user preferences over time
- Research agents building up reusable knowledge across multiple sessions on a related topic
11. Common Mistakes
Incorrect idea: Conflating context with memory.
Why it is incorrect: As shown directly in Section 3, context is ASSEMBLED fresh from state and relevant memory — it is not itself a stored thing.
Incorrect idea: Storing every single interaction detail in long-term memory indiscriminately.
Why it is incorrect: As shown directly in Section 6, real selectivity in what gets stored matters — not everything is worth persisting.
Incorrect idea: Treating conversation history and long-term memory as the same thing.
Why it is incorrect: As shown directly in Section 3, conversation history is a raw log; memory is DELIBERATELY curated, persistent knowledge.
12. Limitations
- Memory retrieval inherits your RAG course’s limitations — imperfect retrieval can surface irrelevant memories or miss relevant ones (directly connecting to Module 15 of your RAG course)
- Deciding what’s worth persisting to long-term memory (vs. discarding after the task ends) is a real, non-trivial design decision with no universal answer
13. Quick Reference
flowchart TD
State[State<br/>current task, Module 3/12] --> Context[Context<br/>assembled fresh<br/>for THIS decision]
Memory[Long-Term Memory<br/>persists across sessions] -->|relevant retrieval| Context
History[Conversation History<br/>raw turn-by-turn log] -.->|may inform| Context
Context --> LLM[LLM Decision]
14. Code — Implementing All Four Concepts as Distinct
🎯 Target of this example: implement Section 3 and 7’s real developer example directly — state, conversation history, and long-term memory as separate, independently-stored data, with context explicitly ASSEMBLED (not stored) from them at decision time.
Example 1 — Simple
from dataclasses import dataclass, field
@dataclass
class AgentMemorySystem:
"""Demonstrates all FOUR distinct concepts (Section 3)
side by side, using the SAME agent instance."""
state: dict = field(default_factory=dict) # current task's evolving understanding
conversation_history: list = field(default_factory=list) # raw turn-by-turn log
long_term_memory: dict = field(default_factory=dict) # persists ACROSS sessions
# "context" is deliberately NOT stored here -- it's ASSEMBLED
def assemble_context(self, current_observation: str) -> dict:
"""CONTEXT is not a stored thing -- it's built FRESH each
time from state, relevant memory, and the current
observation (Section 3's critical distinction)."""
return {
"state": self.state,
"relevant_memory": self.long_term_memory,
"current_observation": current_observation,
}
system = AgentMemorySystem()
system.state["order_status"] = "late"
system.conversation_history.append({"role": "user", "content": "My order is late"})
system.long_term_memory["customer_preference"] = "Prefers email"
context = system.assemble_context("Package delivered 2 days ago")
print(f"State (evolving task understanding): {system.state}")
print(f"Conversation history (raw log): {system.conversation_history}")
print(f"Long-term memory (persists across sessions): {system.long_term_memory}")
print(f"\nAssembled context (built fresh, not stored): {context}")
Expected Output:
State (evolving task understanding): {'order_status': 'late'}
Conversation history (raw log): [{'role': 'user', 'content': 'My
order is late'}]
Long-term memory (persists across sessions): {'customer_preference':
'Prefers email'}
Assembled context (built fresh, not stored): {'state':
{'order_status': 'late'}, 'relevant_memory': {'customer_preference':
'Prefers email'}, 'current_observation': 'Package delivered 2 days
ago'}
What we conclude from this example: all three separate stores (state, history, memory) exist independently, and context is visibly ASSEMBLED from them plus the current observation — exactly Section 3’s critical distinction, made directly, unambiguously observable in code rather than left as an abstract claim.
Example 2 — Intermediate
from dataclasses import dataclass, field
@dataclass
class ShortTermMemory:
"""scoped to the CURRENT task -- discarded once the
task completes (Section 5)."""
current_task_notes: list = field(default_factory=list)
@dataclass
class LongTermMemory:
"""PERSISTS across sessions -- a different lifetime
from short-term memory (Section 5)."""
facts: dict = field(default_factory=dict)
def simulate_task_lifecycle():
"""Directly demonstrates Section 4's human-analogy claim: working
memory is discarded, long-term memory persists."""
stm = ShortTermMemory()
ltm = LongTermMemory()
stm.current_task_notes.append("Customer's order is #4471")
ltm.facts["customer_preference"] = "Prefers email over phone contact"
print(f"DURING task -- short-term: {stm.current_task_notes}")
print(f"DURING task -- long-term: {ltm.facts}")
# Simulate the task ENDING -- short-term memory is discarded
stm = ShortTermMemory()
print(f"\nAFTER task ends -- short-term: {stm.current_task_notes}")
print(f"AFTER task ends -- long-term: {ltm.facts}")
simulate_task_lifecycle()
Expected Output:
DURING task -- short-term: ["Customer's order is #4471"]
DURING task -- long-term: {'customer_preference': 'Prefers email
over phone contact'}
AFTER task ends -- short-term: []
AFTER task ends -- long-term: {'customer_preference': 'Prefers email
over phone contact'}
What we conclude from this example: short-term memory is empty after the task ends, while long-term memory survives completely unchanged — exactly demonstrating Section 4-5’s lifetime distinction as real, observable behavior rather than an abstract definition.
Example 3 — Production Grade
from dataclasses import dataclass, field
@dataclass
class MemoryEntry:
fact: str
relevance_tags: list
class LongTermMemoryStore:
"""A production-style memory store implementing Section 6's
RETRIEVAL step -- exactly your RAG course's retrieval pattern
(Section 9), applied here to retrieving RELEVANT memories rather
than document chunks."""
def __init__(self):
self.entries: list = []
def store(self, fact: str, relevance_tags: list):
"""UPDATE (Section 6) -- deliberately, selectively adding a
NEW fact, not indiscriminately logging every interaction
detail."""
self.entries.append(MemoryEntry(fact, relevance_tags))
def retrieve_relevant(self, current_tags: list) -> list:
"""RETRIEVAL (Section 6) -- given the current situation,
which stored memories are relevant?"""
return [e.fact for e in self.entries if any(tag in current_tags for tag in e.relevance_tags)]
class Agent:
def __init__(self, memory_store: LongTermMemoryStore):
self.memory_store = memory_store
self.state = {}
def assemble_context(self, current_observation: str, current_tags: list) -> dict:
"""Context ASSEMBLED fresh (Section 3), pulling in ONLY the
relevant subset of long-term memory -- not everything ever
stored."""
relevant_memories = self.memory_store.retrieve_relevant(current_tags)
return {
"state": self.state,
"relevant_memory": relevant_memories,
"current_observation": current_observation,
}
memory_store = LongTermMemoryStore()
memory_store.store("Customer prefers email over phone contact", relevance_tags=["contact_preference"])
memory_store.store("Customer previously had a shipping delay resolved via refund", relevance_tags=["refund_history", "shipping"])
memory_store.store("Customer's favorite product category is electronics", relevance_tags=["preferences", "marketing"])
agent = Agent(memory_store)
agent.state["order_status"] = "late"
context = agent.assemble_context(
current_observation="Order #4471 confirmed delivered by carrier, customer disputes receipt",
current_tags=["shipping", "refund_history"],
)
print("Assembled context for THIS decision:")
print(f" State: {context['state']}")
print(f" Relevant memory retrieved: {context['relevant_memory']}")
print(f" Current observation: {context['current_observation']}")
Expected Output:
Assembled context for THIS decision:
State: {'order_status': 'late'}
Relevant memory retrieved: ['Customer previously had a shipping
delay resolved via refund']
Current observation: Order #4471 confirmed delivered by carrier,
customer disputes receipt
What we conclude from this example: only the relevant memory (tagged “shipping” or “refund_history”) was retrieved and included in the assembled context — the customer’s electronics preference, though stored, was correctly excluded as irrelevant to THIS specific decision. This directly demonstrates Section 6’s retrieval principle: memory isn’t dumped wholesale into context, it’s selectively retrieved, exactly mirroring your RAG course’s entire retrieval discipline.
15. Interview Questions
Q: Precisely distinguish state, conversation history, memory, and context — four terms that are easy to conflate.
Ans: State is the agent’s evolving understanding within the current task, existing only for that task’s duration. Conversation history is the raw, turn-by-turn log of what was said. Memory is persistent knowledge the agent deliberately retains across sessions, distinct from a single task’s state. Context is everything actually assembled and given to the LLM for one specific decision — critically, context is not itself stored; it’s built fresh each time, typically from some combination of current state, relevant retrieved memory, and recent observations.
Q: Why is conflating “context” with “memory” considered the most common mistake in this area?
Ans: Context is an assembled, transient combination of information prepared specifically for one reasoning step, while memory is a persistent, deliberately curated store of knowledge that outlives any single decision or task. Treating them as the same thing obscures an important architectural reality: an agent’s memory store might contain many facts, but only a relevant subset gets pulled into context for any given decision — the memory store and the assembled context for one specific moment are different things with different lifetimes and different scopes.
Q: How does long-term memory retrieval in an agent system relate to the retrieval covered in a RAG course?
Ans: They’re the same underlying mechanism, applied to a different kind of content. In RAG, you retrieve relevant document chunks from a knowledge base to include in a generation prompt. In agent memory, you retrieve relevant past facts or interactions from a persistent memory store to include in the context for the agent’s current decision. Both involve determining what’s relevant to the current situation and selectively including just that subset, rather than including everything ever stored.
Q: Design a memory system for a customer support agent — what would you choose to persist to long-term memory, and what would you deliberately let expire as short-term state at the end of each ticket?
Ans: I’d persist reusable facts likely to matter across future interactions — stated preferences (like preferring email contact), patterns in past issues (like a history of shipping delays), and resolutions that worked well previously. I’d let ticket-specific details expire as short-term state once a ticket closes — the specific order number involved, the exact sequence of tool calls made during that particular investigation, and other details that were only relevant to resolving that one, now-completed issue and wouldn’t inform a future, unrelated interaction.
16. What You Should Remember
- State, conversation history, memory, and context are four distinct concepts with different lifetimes and purposes — verified directly through code storing all three (state, history, memory) separately while explicitly assembling context fresh from them.
- Short-term memory is discarded after a task; long-term memory persists across sessions — verified directly by observing short-term memory reset to empty while long-term memory survives unchanged.
- Long-term memory retrieval directly reuses your RAG course’s retrieval discipline — verified directly through a memory store that correctly retrieves only relevant facts, not everything ever stored.
17. Quick Practice
For an agent in a domain of your choosing, list three distinct pieces of information and classify each one as belonging to state, conversation history, or long-term memory, using this module’s precise definitions.
18. Next Step
Next: Module 12 — Agent State — closing Level 5: state transitions, persistence, and precisely how state evolves through a multi-step execution.
- Author
- TechByteByByte Editorial Team
- Reviewed by
- TechByteByByte Admin
- Published
- Last reviewed