AI Agents
Intermediate 13 min read AI Agents

Agent Memory

Carrying context beyond one context window

Memory is what an agent carries beyond its current context: working context, episodic history of past tasks, and semantic knowledge retrieved on demand. Good memory design decides whether an agent feels coherent — or forgets mid-sentence.

memoryworking memoryepisodicsemanticcontextrecall
Prerequisites: Agent State
Before we start

What you will learn

  • Enumerate the memory tiers
  • Design recall that finds what matters
  • Manage stale and private memories
Introduction

What is this?

Memory tiers: working (in-context turn state), episodic (past tasks: inputs, decisions, outcomes), and semantic (extracted knowledge: user prefs, domain facts) — stored in vector DBs or tables and retrieved when relevant. Good memory retrieves precisely, expires stale entries, and never leaks across tenants.

Motivation

Why do you need it?

  • Long tasks outgrow the context window.
  • Returning users expect continuity across sessions.
  • Semantic memory reduces repeated tool calls and errors.
  • Tiered memory keeps cost sane: only fetch what today's turn needs.
Think about it

Mental model

Imagine

A barista who knows your order

Think of

Semantic memory

Working memory is the order pad for THIS drink (current turn). Episodic memory: 'last time you grabbed a flat white at 9am'. Semantic memory: 'you prefer oat milk, double shot'. The good barista checks the working note, keeps the episodic trail, and consults the preference card — without paging through every order you've ever made.

Mechanics

How it works

  • Working: assemble recent turns + current step context.
  • Episodic: append completed tasks with outcomes to a store.
  • Semantic: extract durable facts; embed and index.
  • Recall: on turn start, fetch relevant memory (scored, bounded).
  • Manage: expiry, privacy boundaries, tenant isolation.

Memory tiers.

Working
Episodic
Semantic
Recall
Use
Real world

In production

  • Support agents recall user's equipment, history, and open issues.
  • Coding agents recall repo conventions across sessions.
  • Personal assistants fetch preferences on relevant turns.
Guidelines

Best practices

  • Fetch little and high-precision: 3-6 memory hits max.
  • Store with metadata (time, source, confidence) for expiry and attribution.
  • Isolate memory by tenant/user; never cross-leak.
  • Let users view/delete stored memories (privacy).
Watch out

Common mistakes

  • Dumping the whole memory store into every prompt.
  • No expiry — 'you mentioned X last year' becomes wrong guidance.
  • Cross-tenant memory bleed.
Root cause analysis

Common issues

Error
Semantic recall misses the right fact
Root cause

Bad extraction or embedding

Fix

Improve extraction prompts; hybrid keyword+vector recall.

Error
Memory contradicts current truth
Root cause

Stale entries never expired

Fix

Timestamp and decay; deprecate on observed contradiction.

When things break

Debugging tips

  • Agent forgets between turns → working memory assembled wrong; check what's included.
  • Old facts mislead → expiry and confidence scoring absent.
  • Recall noise → score threshold too low; tighten scoring.
Interviews

Interview checkpoint

Practice

Hands-on lab

Scratchpad and Rolling Summary
25 min · intermediate
In progress
Objectives
  • Append observations into working memory
  • Compact memory when it grows past a budget
  • Recall facts from memory by keyword

Scratchpad and Rolling Summary

25 min · intermediate

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Semantic memory means…

2

Recalling the entire memory store is a fine strategy.

Go deeper

Official documentation