Episodic Memory (Per-Session History)
Per-conversation logs of what happened, indexed by time and session id.
Episodic Memory (Per-Session History) is a free AI Agents lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Types of Memory
Inspired by cognitive science, agent memory is usually split into three kinds:
- Episodic — what happened (this conversation, last week's session)
- Semantic — facts about the world / user
- Procedural — skills (successful action sequences)
This lesson covers episodic.
What Is Episodic Memory?
Episodic memory stores specific events with timestamps and context. For an agent, this is the per-session conversation log.
It is the most basic form of memory and the easiest to build.
Data Model
One row per turn or per session:
CREATE TABLE episodes (
id BIGSERIAL PRIMARY KEY,
user_id TEXT NOT NULL,
session_id TEXT NOT NULL,
role TEXT NOT NULL, -- system/user/assistant/tool
content TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
metadata JSONB
);Saving Each Turn
def save_turn(user_id, session_id, role, content, metadata=None):
conn.execute('''
INSERT INTO episodes (user_id, session_id, role, content, metadata)
VALUES (%s, %s, %s, %s, %s::jsonb)
''', (user_id, session_id, role, content, json.dumps(metadata or {})))Loading a Session
def load_session(session_id):
rows = conn.execute('SELECT role, content FROM episodes WHERE session_id = %s ORDER BY id', (session_id,))
return [{'role': r[0], 'content': r[1]} for r in rows]Multi-Session History
Sometimes you want context from previous sessions of the same user — e.g. "what did Alice ask yesterday?":
def recent_sessions(user_id, n=5):
rows = conn.execute('''
SELECT DISTINCT session_id, MIN(created_at) AS started
FROM episodes WHERE user_id = %s
GROUP BY session_id ORDER BY started DESC LIMIT %s
''', (user_id, n))
return [r[0] for r in rows]Embedding Past Sessions
To retrieve old episodes by topic, embed each session summary:
def index_session(session_id):
summary = summarise(load_session(session_id))
vec = embed(summary)
save_to_vector_db(session_id, vec, summary)Episodic Retrieval
When a user asks "what did we discuss about pricing last week?", search by embedding:
def search_episodes(user_id, query):
qvec = embed(query)
candidates = vector_db.query(qvec, k=10, filter={'user_id': user_id})
return [load_session(c.id) for c in candidates]Privacy and Retention
Episodic logs are PII-heavy. Apply:
- Encryption at rest
- Per-user delete on account closure
- Auto-purge after N days
- Mask or pseudonymise before any analytics
Compacting Old Episodes
Keep recent turns verbatim; compact older ones into summaries:
if session_age_days > 30:
summary = summarise(load_session(sid))
conn.execute('UPDATE episodes SET content = %s WHERE session_id = %s', (summary, sid))
conn.execute('DELETE FROM episodes WHERE session_id = %s AND created_at < NOW() - INTERVAL \'1 day\'', (sid,))Episodic vs Short-Term Context
Short-term = current messages list (what the model sees right now). Episodic = the database — searchable, persistent, larger.
Linking Episodic to Other Memory
From an episode you can extract:
- Semantic facts ("user prefers Celsius")
- Procedural skills (sequence of tool calls that worked)
These feed into the other two memory types.
Replay for Eval
Save raw episodes so you can replay them. When you change the agent, re-run old episodes and check the new output is still good.
What is Episodic?
What does episodic memory store?
Recap
Episodic memory = timestamped session logs in a DB. Searchable by user, by session, by embedding. Compact old ones. Feed it into semantic and procedural memory.
Frequently asked questions
Is the “Episodic Memory (Per-Session History)” lesson free?
Yes — the full text of “Episodic Memory (Per-Session History)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Episodic Memory (Per-Session History)”?
Per-conversation logs of what happened, indexed by time and session id. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Episodic Memory (Per-Session History)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Episodic Memory (Per-Session History)
- Semantic Memory (Vectorised Facts)
- Procedural Memory (Skill Library)
- Memory Decay and Garbage Collection