Memory and state: history, notes and long tasks
Agents & Tool Calling · 10 questions
Short notes
Key idea
Agents forget everything outside the context window. Short-term memory is the conversation history; long-term memory is what you save outside it (notes, facts, task state) and load back when needed.
Syntax
- History management: keep recent turns, summarise older ones, drop large tool outputs once used.
- Scratchpad / notes: the agent writes progress to a file or table ("done: steps 1–3; next: email customer").
- Facts store: user preferences and key facts, retrieved by key or similarity.
- Task state: a structured record (status, step, IDs) so a long task can resume after a crash.
Example
A research agent working for an hour writes findings to notes.md after each source. When the context fills, it's summarised; the agent rereads its notes and continues without redoing searches.
Traps
- Saving everything makes memory noisy and retrieval worse; store what's useful later.
- Stale or wrong memories keep misleading the agent; let them be updated or removed.
- Memory leaks between users are a privacy bug; key memory by user.
- Summaries drop exact values; store IDs and numbers verbatim.
Want the full lesson with step-by-step examples, a project and a certificate? Agents & Tool Calling course, ₹180 →
- Question 1easy
Where does an agent's long-term memory about a customer actually live?
- Question 2medium
Why label recalled notes as "facts, not instructions" in the prompt?
- Question 3hard
Write
allowed(text)that returns False if the text is shorter than 10 or longer than 200 characters, or contains any run of 4 or more digits, or the words otp, password or pin (any case); otherwise True. Store[allowed(n) for n in notes]inresult.Store your answer in
result. - Question 4medium
The agent proposes three notes; the save tool keeps only allowed ones. How many are stored?
import re store = [] def save_note(text): if len(text) < 10 or re.search(r"\d{4,}|otp|password", text, re.I): return {"error": "not allowed"} store.append(text) return {"saved": True} for t in ["Prefers replies in Tamil", "OTP 9981 for login", "Card ends 4421"]: save_note(t) print(len(store)) - Question 5easy
What does this print?
import json case = {"case_id": "R-57", "step": "pickup_booked"} saved = json.dumps(case) resumed = json.loads(saved) print(resumed["step"] == case["step"])
Memory and state: history, notes and long tasks
Memory and state: history, notes and long tasks
Memory and state: history, notes and long tasks
Memory and state: history, notes and long tasks
Memory and state: history, notes and long tasks
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.