Semantic Memory (Vectorised Facts)
Free-floating facts the agent learned about the user or world, stored in a vector index.
Semantic Memory (Vectorised Facts) is a free AI Agents lesson on CoddyKit — lesson 2 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.
What Is Semantic Memory?
Semantic memory holds free-floating facts the agent knows — about the user, the world, the company. Unlike episodic, there is no timestamp tied to a specific event.
Examples:
- "The user's name is Alice."
- "Alice prefers Celsius."
- "The current pricing tier is Pro at $20/mo."
Storage Format
A vector store + structured metadata:
CREATE TABLE semantic_facts (
id UUID PRIMARY KEY,
user_id TEXT NOT NULL,
fact TEXT NOT NULL,
embedding vector(1536),
source TEXT, -- 'extracted' / 'user-stated' / 'system'
confidence REAL DEFAULT 1.0,
created_at TIMESTAMPTZ DEFAULT NOW()
);Extracting Facts from Conversations
After each session, run an LLM extractor:
extract_prompt = '''
From the conversation below, list any new facts about the user.
Return JSON: [{"fact": "...", "confidence": 0.0-1.0}]
Conversation:
{transcript}
'''
print(extract_prompt.strip())
Saving Extracted Facts
import uuid
for f in extracted:
vec = embed(f['fact'])
conn.execute('''
INSERT INTO semantic_facts (id, user_id, fact, embedding, confidence, source)
VALUES (%s, %s, %s, %s, %s, 'extracted')
''', (str(uuid.uuid4()), user_id, f['fact'], vec, f['confidence']))Retrieval at Inference Time
For each new query, retrieve relevant facts and inject into the system prompt:
def relevant_facts(user_id, query, k=5):
qvec = embed(query)
rows = conn.execute('''
SELECT fact FROM semantic_facts
WHERE user_id = %s
ORDER BY embedding <=> %s LIMIT %s
''', (user_id, qvec, k))
return [r[0] for r in rows]
facts = relevant_facts(user_id, user_question)
system = SYSTEM_PROMPT + '\n\nKnown facts:\n' + '\n'.join(facts)Updating Facts
What if a fact changes? "Alice prefers Celsius" -> "Alice now prefers Fahrenheit". Two strategies:
- Mark the old fact obsolete (add updated_at, soft delete)
- Or: store contradictions; resolve at retrieval
Fact Reconciliation
When extracting, check for contradictions with existing facts:
def reconcile(user_id, new_fact):
similar = relevant_facts(user_id, new_fact, k=3)
response = llm.invoke(f'Is the new fact ({new_fact}) consistent with existing facts ({similar})? If contradicting, return the obsolete one.')
if response.has_contradiction:
mark_obsolete(response.obsolete_id)
save_fact(new_fact)Confidence-Weighted Retrieval
rows = conn.execute('''
SELECT fact FROM semantic_facts
WHERE user_id = %s AND confidence > 0.6
ORDER BY embedding <=> %s LIMIT %s
''', ...)Fact Decay
Some facts go stale ("current project is X"). Add a TTL or decay confidence over time:
UPDATE semantic_facts
SET confidence = confidence * 0.95
WHERE created_at < NOW() - INTERVAL '30 days';Privacy-Sensitive Facts
Encrypt PII before storing. Some facts (health, finances) need extra protection — store in a separate, more locked-down table.
User-Editable Facts
Show users their stored facts and let them edit/delete. Builds trust; reduces creepy memory mistakes.
Memory as a Tool
Expose memory operations as tools the agent itself can call:
tools = [
{'name': 'remember_fact', 'description': 'Save a new fact about the user'},
{'name': 'forget_fact', 'description': 'Remove a stored fact'},
{'name': 'recall_facts', 'description': 'Search relevant facts for a query'}
]
import json
print(json.dumps(tools, indent=2))
Letta and MemGPT
The Letta framework (formerly MemGPT) implements agent memory at scale, with separate "core" memory (always in context) and "archival" memory (vector-searched).
Semantic vs Episodic
How does semantic differ from episodic memory?
Recap
Vector-store facts about the user, retrieve the relevant K on every turn, inject into the system prompt. Reconcile contradictions; decay or expire over time.
Frequently asked questions
Is the “Semantic Memory (Vectorised Facts)” lesson free?
Yes — the full text of “Semantic Memory (Vectorised Facts)” 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 “Semantic Memory (Vectorised Facts)”?
Free-floating facts the agent learned about the user or world, stored in a vector index. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Semantic Memory (Vectorised Facts)” 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