← Lesson
Working Memory in the Loop
Joeven
Run
Reset
Python loads on first run
class AgentMemory: def __init__(self, window=4): self.scratch = [] self.notes = [] self.summary = "" self.window = window def add_turn(self, act, obs): self.scratch.append({"act": act, "obs": obs}) if len(self.scratch) > self.window: old = self.scratch.pop(0) extra = old["act"] + " -> " + str(old["obs"]) if self.summary: self.summary = self.summary + "; " + extra else: self.summary = extra def remember(self, text): self.notes.append(text) def retrieve(self, q): hits = [] for n in self.notes: if q.lower() in n.lower(): hits.append(n) return hits def for_assembler(self, q): return { "summary": self.summary, "scratch": self.scratch, "notes": self.retrieve(q), } m = AgentMemory(window=2) m.add_turn("search", "docs mention Paris") m.add_turn("search", "rain 12C") m.add_turn("finish", "umbrella") m.remember("user: always wants C not F") ctx = m.for_assembler("user") print("scratch len", len(ctx["scratch"])) print("summary", ctx["summary"]) print("notes", ctx["notes"])
Run to execute this in your browser. Nothing is sent to a server.