Memory & Context Engineering

Short-term conversation memory and long-term persistent memory for agents.

Every agent resets by default — memory is what you add deliberately. This is consistently the layer that separates a demo from something that feels like it actually remembers you.

Short-Term / Conversation Memory

Long-Term / Persistent Memory

What Is It?

Memory lets agents remember prior turns, user facts, and retrieved docs across sessions.

Short-term lives in the context window; long-term uses databases and embeddings.

Child pages cover compaction, caching, and persistent stores.

Why Should I Care?

Demo agents feel smart with memory; production agents fail without eviction and privacy rules.

Memory bugs leak data across users — design tenancy early.

Real World Analogy

Short-term memory is the sticky note on your monitor; long-term memory is the filing cabinet across the hall — agents need both on purpose.

Visual Mental Model

text
context window (short)
       +
  vector/DB store (long)

Beginner Explanation

Agents reset every session unless you add memory. This hub splits short-term (context, compaction) and long-term (vectors, structured stores).

Smallest Possible Example

python
HISTORY = ["user: hi", "assistant: hello"]
LONG_TERM = {"user_id": "prefs"}
print(len(HISTORY), LONG_TERM.keys())

Common Mistakes

❌ Dumping entire chat logs into every prompt.

You blow token budgets and leak irrelevant noise.

❌ One global memory bucket for all users.

Partition by user/tenant ID always.

❌ No deletion path for GDPR requests.

Long-term memory needs erase APIs.

Real Project Example

ChatGPT memory features and Notion AI workspace context follow these patterns.

Support copilots at Zendesk-scale store ticket summaries in vector stores.

Build This Mini Project

Sketch what should be short-term vs long-term for a fake tutoring bot; write max token budget for each.

Interview Question

Short-term vs long-term agent memory?

Short-term is conversation context within window limits, managed via trimming/compaction; long-term persists facts or embeddings across sessions in external stores with retrieval.

Key Takeaways

  • Memory is designed, not automatic.
  • Hub links short vs long chapters.
  • Tenant isolation is mandatory.
  • Plan compaction and deletion.

YouTube learning

Core concepts from video walkthroughs:

Threads in the same domain as this chapter — go deeper on iHateReading without leaving the roadmap.

Vector search using vector embeddings cover

Vector search using vector embeddings

Embeddings, similarity search, and RAG-shaped memory for apps.

Jev, explained like you're five (then like you're building with it) cover

Jev, explained like you're five (then like you're building with it)

System One decision models for fast, typed judgments inside software.

Agent harnessing: the part of building AI agents nobody talks about cover

Agent harnessing: the part of building AI agents nobody talks about

Loop, tools, memory, sandbox, and tracing live in your code — not the model weights.

AI Agent cover

AI Agent

What agents are and how they differ from a single chat completion.