Sub topic 7
Episodic, Semantic, and Procedural Memories
This chapter assumes you know basic Python, have called a chat-style LLM API, and have a rough idea of what an embedding is. It builds on the long-term store from the previous chapter. Here you decide what kind of thing each memory is, because the kind affects how you write it, retrieve it, and update it.
Three kinds of memory
Cognitive science splits human long-term memory into three types. Agent designers borrow the same split because each type behaves differently in code.
| Type | What it holds | Example | Typical shape |
|---|---|---|---|
| Episodic | A specific past event | "On Monday the user's deploy failed because of a missing env var." | Timestamped record of what happened |
| Semantic | A general fact | "The user's name is Priya and she prefers Python." | Short statement, no time attached |
| Procedural | How to do something | "When asked for a report, query the sales table first, then format as a table." | Instructions, rules, or a prompt |
Episodic memory
Episodic memory records experiences: what was asked, what the agent did, and how it turned out. It is useful for learning from past attempts, such as "last time this API call timed out, so retry with a smaller batch."
Store the time and enough context to make the event understandable on its own. The created field in the MemoryStore from the earlier example is what makes episodes useful, because you can sort by recency or ignore old ones.
Semantic memory
Semantic memory holds facts that stay true across many conversations: preferences, names, project details. It is usually written as a short, self-contained sentence. Facts change, so semantic memories need updating. If Priya moves to a new job, the old fact must be replaced and not left next to the new one.
Procedural memory
Procedural memory is knowledge about how to act. In agents it most often lives in the system prompt or in a set of rules the agent loads at the start. Unlike the other two, it changes how the agent behaves and is not just information to quote. An agent can improve it by rewriting its own instructions after feedback, but that should be done carefully and reviewed, since a bad edit affects every later run.
Tagging memories by kind
The write method already accepts a kind argument. Using it lets you filter at read time.
import math
import time
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
norm_a = math.sqrt(sum(x * x for x in a))
norm_b = math.sqrt(sum(y * y for y in b))
return dot / (norm_a * norm_b) if norm_a and norm_b else 0.0
class MemoryStore:
def __init__(self, embed):
self.embed = embed
self.items = []
def write(self, user_id, text, kind="semantic"):
self.items.append({
"user_id": user_id,
"text": text,
"kind": kind,
"vec": self.embed(text),
"created": time.time(),
})
def search(self, user_id, query, k=3, kind=None):
q = self.embed(query)
own = [m for m in self.items
if m["user_id"] == user_id and (kind is None or m["kind"] == kind)]
own.sort(key=lambda m: cosine(q, m["vec"]), reverse=True)
return own[:k]
# Toy embedding: counts of a few keywords. Use a real model in practice.
WORDS = ["python", "deploy", "failed", "report"]
embed = lambda text: [float(text.lower().count(w)) for w in WORDS]
store = MemoryStore(embed)
store.write("priya", "Priya prefers Python.", kind="semantic")
store.write("priya", "The deploy failed on Monday: missing env var.", kind="episodic")
for m in store.search("priya", "why did the deploy fail", kind="episodic"):
print(m["kind"], "-", m["text"])The kind filter here is added to search as an optional argument. Procedural memory is not in this store. It stays in the system prompt text.
Common errors
- Storing everything as one type. A raw transcript saved as a "fact" retrieves poorly and is hard to update.
- Keeping stale semantic facts. Without an update step, contradictory memories both get retrieved.
- Letting the agent rewrite procedural memory without review.
- Dropping timestamps from episodes, which makes recency ranking impossible.
Exercise
Classify each as episodic, semantic, or procedural: (a) "The user's company uses PostgreSQL." (b) "Yesterday the agent's search returned no results for 'Q3 numbers'." (c) "Always ask for a date range before running a report."
Show answer
(a) Semantic: a general fact. (b) Episodic: a specific past event with a time. (c) Procedural: a rule about how to act.
Exercise
Why should semantic memories have an update step, while episodic memories usually do not?
Show answer
A fact can become false (a changed job or preference), so the old version must be replaced to avoid contradictions. An episode is a record of something that happened, and it stays true as history even when circumstances change.