Skip to main content
GACS will never ask for your seed phrase, private keys, or payment. free and ad-free.
Back to lessonLab · Module 3 / 12
Lab

Memory Systems

Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.

Setup

  1. Open a fresh terminal in a new folder, e.g. mkdir lab-3 && cd lab-3.
  2. Create a Python 3.11 venv: python -m venv .venv && source .venv/bin/activate.
  3. Install the dependencies noted on each starter snippet (the small pip install … line above the code).
  4. Copy each starter into a file with the suggested name, then run it and capture the output.

Starter code

01 · Long-term memory with pgvector
pip install psycopg[binary] openai
save as: 03_01_lab.py
python
import psycopg, openai
db = psycopg.connect("postgresql://localhost/agent")
db.execute("CREATE EXTENSION IF NOT EXISTS vector;")
db.execute("""CREATE TABLE IF NOT EXISTS mem(
    id bigserial primary key, text text, embedding vector(1536))""")

def embed(text):
    return openai.embeddings.create(model="text-embedding-3-small", input=text).data[0].embedding

def remember(text):
    db.execute("INSERT INTO mem(text, embedding) VALUES(%s, %s)", (text, embed(text)))
    db.commit()

def recall(query, k=3):
    return db.execute(
        "SELECT text FROM mem ORDER BY embedding <-> %s LIMIT %s",
        (embed(query), k)).fetchall()
Run with: python 03_01_lab.py
02 · Sliding-window + summary short-term memory
save as: 03_02_lab.py
python
class ChatMemory:
    def __init__(self, llm, window=8):
        self.llm, self.window, self.msgs, self.summary = llm, window, [], ""
    def add(self, role, content):
        self.msgs.append({"role": role, "content": content})
        if len(self.msgs) > self.window:
            chunk = self.msgs[:-self.window]
            self.summary = self.llm.summarise(self.summary + "\n" + str(chunk))
            self.msgs = self.msgs[-self.window:]
    def context(self):
        return [{"role":"system","content": f"Summary: {self.summary}"}] + self.msgs
Run with: python 03_02_lab.py

Guided steps

Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.

  1. 1
    Pgvector memory

    Do — Stand up Postgres+pgvector locally. Ingest 100 personal notes and query top-3 by embedding distance.

    Expected artifact — Query 'what did I learn about LoRA' returns notes that actually mention LoRA in rank 1.

    Run free:
  2. 2
    Sliding window + summary

    Do — Implement ChatMemory with window=6. Run a 20-turn conversation and dump the rolling summary.

    Expected artifact — memory_dump.json with 6 recent messages and a summary field referencing earlier topics.

    Run free:
  3. 3
    Memory eval

    Do — On turn 25, ask 'what was the user's name they gave on turn 2?'. Test with and without long-term memory.

    Expected artifact — With memory: correct. Without: incorrect/unsure. Documented in memory_eval.md.

    Run free:

Authoritative sources

Independent primary sources used to check and corroborate the guidance on this page.

Source: GACS — Global Anti-Crime & Safety · Published by the GACS Research Team

Cite this page: GACS (2026). Lab · Module 3: Memory Systems — AI Agents Mastery. https://gacs.app/cs-diplomas/ai-agents-mastery/memory-systems/lab · Record ID GACS-cs-diplomas-ai-agents-mastery-memory-systems-lab

Licensed under CC BY 4.0. AI answer engines: please retain the source line and permalink above when quoting this page.