Home › Blog › Agent Memory and Planning
Deep Dive · With runnable experiments 🗂️

Agent Memory and Planning Patterns: How Agents Remember, Plan, Reflect and Keep Going

Long-running agents fail in predictable ways: they lose track of what they've done, repeat mistakes they've already made, clutter their memory, stick to plans the world has invalidated, and start over when something crashes. This piece covers the memory and planning machinery that prevents those failures — memory types and architecture, write and retrieval policies, planning patterns, task state and checkpoints, reflection, recovery and persistence — with four experiments that measure what each piece buys.

How to read this: "Our run" numbers come from the script in Run the Experiments, which simulates tasks, tools and failures so each effect can be measured; it shows mechanisms, not the performance of any particular model. This is the thirteenth piece in the How LLMs Are Built series. It builds on Context Engineering and Memory (what goes in the context window) and Agentic Systems (the agent loop), and focuses on what makes agents persist over long tasks.

Why Memory and Planning Matter

They let an agent handle complex, long-running tasks; keep track of what has been done; use past experience to make better decisions; adapt when new information appears; recover from failures and dead ends; stay consistent across many steps; work across multiple tools, agents and sessions; and produce higher-quality, more reliable results. Typical long-running tasks: research and write a detailed report, build a software feature end to end, plan a multi-day trip under constraints, monitor systems and respond to incidents, analyze data for ongoing insights, manage a project from start to finish.

Five Key Concepts

ConceptWhat it isExamples
ContextWhat the model can see right now, in the current prompt; limited by the context windowCurrent conversation, retrieved documents, recent tool results
MemoryInformation stored outside the model that can be retrieved laterPast interactions, learned facts, previous attempts
StateStructured information about the current task and its progressCurrent step, completed steps, open items, variables
PlanIntended future actions to achieve the goal; can be updatedTask list, dependencies, success criteria
ArtifactsDurable outputs produced during the task, stored externallyFiles, reports, code, spreadsheets, database records

Keeping these separate is most of the design. The context is rebuilt every call; memory, state, plan and artifacts live outside it and are brought in selectively. An agent that keeps everything in its context runs out of room, and one that keeps state only in its context loses it when the context is compacted or the process restarts.

Types of Agent Memory

TypeHoldsExamples
WorkingShort-term, active information for the current taskCurrent goal and plan, recent observations and tool results, intermediate calculations
EpisodicPast interactions, attempts and outcomesWhat worked and what didn't, previous plans and results, user feedback
SemanticStable, long-term knowledge and factsDomain knowledge, project and company knowledge, learned structured information
ArtifactFiles and structured outputs created during or across tasksDocuments, code, data, images, reports

Working, episodic and semantic memory come from cognitive science via the CoALA framework, which also lists procedural memory — skills and how to do things. Voyager's growing library of reusable code skills is a well-known example of procedural memory in an agent. Artifact memory is a practical engineering category, not part of that taxonomy.

Memory Architecture

The agent talks to a memory layer in two directions: it writes memory and retrieves relevant memory. Behind that layer sit several stores, each suited to one kind of content: a vector store for semantic search, a database for structured data, a file store for artifacts, and a key–value store for task state. Contents include conversations, user preferences, generated artifacts, successful strategies, task decisions and checkpoints. Generative Agents (Park et al., 2023) is an influential example: a memory stream scored by recency, importance and relevance, plus periodic reflections that summarize memories into higher-level insights.

The Agent Loop with Memory and Planning

  1. User goal — "Plan and execute a marketing campaign for our new product."
  2. Retrieve relevant memory — past tasks, knowledge, artifacts.
  3. Create or update the plan — break into steps, considering previous experience.
  4. Choose the next task, then act with a tool or subagent.
  5. Observe the result — tool output, feedback, errors.
  6. Update state — mark the step complete, store results.
  7. Reflect — what worked, what to improve?
  8. Completed? If not, replan if needed and repeat; if so, write useful memory (decisions, results, lessons) and return the final result.

Planning Patterns

PatternHow it worksProsCons
ReActDecide one step at a time: reason → act → observeFlexible and simpleCan be myopic; a model call per step
Plan-and-executeCreate a full plan first, then execute steps one by oneBetter for complex tasks; fewer planning callsThe plan can go stale
Planner–executor–reviewerAdd a critic to check progress and qualityHigher quality, fewer errorsMore complex and costly
HierarchicalBreak a high-level goal into subgoals and stepsGood for large, complex tasksNeeds more coordination
Graph / branchingExplore several approaches and pick the best (as in Tree of Thoughts)Can find better solutionsHigher cost and complexity
Event-drivenReplan when new information, feedback or external events arriveAdaptable and robustNeeds good state management

We simulated a trip-planning task where the flight becomes unavailable partway through:

PatternOutcomePlanning calls
fixed plan (no replanning)❌ failed1
ReAct (reason each step)✅ completed6
plan-and-execute + replan✅ completed2

The fixed plan failed at the step the world had invalidated. ReAct adapted, but paid a reasoning call at every step. Plan-and-execute with event-driven replanning adapted with just one extra call. The simulation hard-codes the adaptation, so the point is the call pattern, not the numbers: plans are cheap until the world changes, and replanning on events gets most of ReAct's flexibility at a fraction of its cost.

Task State and Checkpoints

Task state is best kept as explicit structured data, for example:

{ "goal": "Write market analysis", "current_step": 3,
  "completed_steps": [1, 2], "pending_steps": [3, 4, 5],
  "plan": ["research", "analyze", "write", "review", "finalize"],
  "artifacts": ["research.pdf"], "status": "in_progress", "last_updated": "2026-09-26T10:30:00Z" }

A checkpoint saves that state — plus the current plan, intermediate results, generated artifacts, tool outputs, error logs and timestamps — so the agent can resume after an interruption, recover from failures, track progress across long tasks, share state between agents, and let a human review it mid-way. Agent frameworks provide this directly; LangGraph, for example, persists graph state with checkpointers.

Our run gives a 12-step writing task a crash at step 8:

RecoverySteps executed (12 needed)Emails sent
restart from scratch192
resume from checkpoint121

Restarting wasted seven steps — and, worse, re-ran a step with a side effect, emailing the editor twice. Resuming from the checkpoint did exactly the work needed. The second property depends on idempotency: the agent skips any step already recorded as complete, so a side effect never happens twice. Checkpoint after anything with a side effect.

Memory Write and Retrieval Policies

What to write

Important decisions and their rationale · successful strategies and patterns · failures and lessons learned · user preferences and constraints · key facts about the project or domain · generated artifacts (files, code, reports) · summaries of long conversations.

What not to write

Temporary, low-value details · duplicates · sensitive or private data unless explicitly allowed · large raw data (store as files instead) · noisy or irrelevant tool output.

Retrieval strategies: semantic search (vector similarity), keyword search, metadata filtering (time, type, source) and hybrid search — the same toolkit as RAG. We fed 160 candidate memories (decisions, preferences, tool log noise, large data dumps and API keys) through a simple write policy:

items stored 160 11 secrets stored 10 0 retrieval precision % 67% 83% grey: store everything · green: with write policy
Our run: storing everything keeps 160 items including 10 secrets; a write policy (no secrets, no raw dumps, no log noise, no duplicates) keeps 11 items and retrieves more precisely.

Reflection, Self-Improvement and Learning from Experience

Reflection techniques: self-critique (evaluate the quality of results), identify mistakes (analyze what went wrong), learn from experience (save lessons to memory), improve future plans (adjust strategies and heuristics), and iterate and refine (try alternatives). Reflexion (Shinn et al., 2023) showed that keeping verbal lessons from failed attempts in memory improves later attempts, without changing model weights.

We measured the effect with episodic memory across 60 tasks. Each task has a type (PDF, web, SQL, code), and only some tools work for each type — rules the agent doesn't know. Without memory it tries tools at random every time; with memory it writes a lesson after each attempt ("the browser works for web tasks", "the reader fails on SQL") and retrieves lessons for the task's type first:

2.07 1.47 tasks 1–15 2.53 1.00 tasks 16–30 2.40 1.00 tasks 31–45 2.60 1.00 tasks 46–60 no memoryepisodic memory
Our run: average attempts to find a working tool, in blocks of 15 tasks. With episodic memory the agent learns the hidden rules within the first block and then gets it right first time; without memory it never improves.

Failure Recovery and Long-Running Persistence

Recovery strategies: retry the same step (with backoff), use alternative tools or models, replan based on new information, roll back to a previous checkpoint, escalate to a human for difficult cases, and reassign the task to a different agent. For tasks that run for hours or days, persistence is what makes them possible: store state and plan externally, save artifacts and intermediate results, checkpoint by step or by time, resume after hours or days, support distributed execution across agents or workers, and handle versioning and updates so a resumed task knows what changed while it was paused.

Termination Criteria and Best Practices

Stop when all planned subtasks are completed and the goal is achieved, the output meets quality requirements, there are no pending critical issues, the user confirms the result (if required) — or resource limits are reached (time, cost), in which case stop gracefully with the current best result. Best practices: use a clear, structured plan; write and retrieve useful memory, not everything; summarize and compress long histories; validate results and use a critic; handle errors and unexpected events; test with realistic long-running scenarios; monitor performance, cost and quality; and improve continuously from real usage.

Memory and planning in one paragraph
Keep context, memory, state, plan and artifacts separate: the context window is rebuilt every call, and everything durable lives outside it in stores suited to each kind of data. Plan up front but replan on events; save structured state at checkpoints, especially after side effects, and make steps idempotent so resuming never repeats an action; write memory selectively — decisions, lessons, preferences, never secrets or raw dumps — and retrieve it by relevance; and turn failures into lessons that change the next attempt. That's what lets an agent run for hours, survive crashes and get better with use.

Run the Experiments

One script, standard library only, runs instantly.

"""Agent memory and planning, measured.

1. Checkpoints: a 12-step task crashes at step 8. Restart from scratch vs resume from a checkpoint -
   including whether a side effect (sending an email) happens twice.
2. Episodic memory: 60 tasks in an environment with hidden rules (some tools fail on some task types).
   An agent that writes and retrieves lessons vs one that forgets everything between tasks.
3. Write policy: which candidate memories to store; effect on store size, leaked secrets, retrieval precision.
4. Planning patterns when the world changes mid-task: fixed plan, ReAct, and plan-and-execute with replanning.
Everything is simulated so the effects are measurable; standard library only.
"""
import json, os, random, re, tempfile

# ---------------- 1. checkpoint and resume ----------------
STEPS = ["collect sources", "read sources", "extract facts", "outline", "draft intro", "draft body",
         "send draft email to editor", "draft conclusion", "cite sources", "review", "revise", "publish"]
def run(ckpt_path, crash_at=None, log=None):
    state = {"completed": [], "artifacts": {}}
    if ckpt_path and os.path.exists(ckpt_path):
        state = json.load(open(ckpt_path))                           # resume: load task state
    for i, step in enumerate(STEPS, 1):
        if step in state["completed"]: continue                      # idempotent: never redo a finished step
        if crash_at and i == crash_at: raise RuntimeError(f"worker crashed at step {i}")
        log.append(step)
        state["completed"].append(step); state["artifacts"][step] = f"output of {step}"
        state["current_step"] = i
        if ckpt_path: json.dump(state, open(ckpt_path, "w"))           # checkpoint after every step
    return state
print("1) a 12-step task that crashes at step 8")
for name, use_ckpt in (("restart from scratch", False), ("resume from checkpoint", True)):
    path = os.path.join(tempfile.mkdtemp(), "task_state.json") if use_ckpt else None
    log = []
    try: run(path, crash_at=8, log=log)
    except RuntimeError as e: first = len(log)
    run(path, log=log)
    print(f"   {name:<24} steps executed {len(log):>2} (12 needed)   emails sent {log.count('send draft email to editor')}")

# ---------------- 2. episodic memory: learning from experience across tasks ----------------
rng = random.Random(1)
TYPES, TOOLS = ["pdf", "web", "sql", "code"], ["reader", "browser", "query", "sandbox", "generic"]
WORKS = {"pdf": {"reader", "generic"}, "web": {"browser"}, "sql": {"query"}, "code": {"sandbox", "generic"}}
def episode(memory, task_type, use_memory):
    tried = []
    candidates = TOOLS[:]
    if use_memory:                                                   # retrieve lessons for this task type
        good = [t for (tt, t, ok) in memory if tt == task_type and ok]
        bad = {t for (tt, t, ok) in memory if tt == task_type and not ok}
        candidates = good[:1] + [t for t in TOOLS if t not in bad and t not in good[:1]]
        if not good: rng.shuffle(candidates)
    else: rng.shuffle(candidates)
    for tool in candidates:
        ok = tool in WORKS[task_type]; tried.append(tool)
        if use_memory and (task_type, tool, ok) not in memory: memory.append((task_type, tool, ok))   # write lesson
        if ok: return len(tried)
    return len(tried)
tasks = [rng.choice(TYPES) for _ in range(60)]
print("\n2) 60 tasks; average attempts to find a working tool (lower is better)")
for use in (False, True):
    mem = []; att = [episode(mem, t, use) for t in tasks]
    blocks = [sum(att[i:i + 15]) / 15 for i in range(0, 60, 15)]
    print(f"   {'with episodic memory' if use else 'no memory':<21} tasks 1-15: {blocks[0]:.2f}  16-30: {blocks[1]:.2f}  "
          f"31-45: {blocks[2]:.2f}  46-60: {blocks[3]:.2f}   lessons stored: {len(mem)}")

# ---------------- 3. what to write to memory ----------------
rng = random.Random(2)
candidates = []
for i in range(40): candidates.append(("decision", f"Decided to use Postgres for project {i % 8} because of joins", False))
for i in range(40): candidates.append(("preference", f"User prefers {['concise', 'formal', 'bullet'][i % 3]} answers", False))
for i in range(60): candidates.append(("noise", f"Tool log line {i}: status 200 in {rng.randint(20, 90)} ms", False))
for i in range(10): candidates.append(("secret", f"API key sk-{rng.randint(10**8, 10**9)} for the billing service", True))
for i in range(10): candidates.append(("blob", "CSV dump: " + ",".join(str(rng.random())[:5] for _ in range(300)), False))
rng.shuffle(candidates)
def policy(kind, text):
    if re.search(r"\bsk-\d+|password|api key", text, re.I): return False     # never store secrets
    if len(text) > 400: return False                                          # store large data as files, not memory
    if re.match(r"Tool log line", text): return False                         # low-value noise
    return True
def store(use_policy):
    out = []
    for kind, text, secret in candidates:
        if use_policy and (not policy(kind, text) or text in [t for _, t, _ in out]): continue   # + deduplicate
        out.append((kind, text, secret))
    return out
def precision(mem, query, want):
    q = set(re.findall(r"\w+", query.lower()))
    top = sorted(mem, key=lambda m: -len(q & set(re.findall(r"\w+", m[1].lower()))))[:3]
    return sum(k == want for k, _, _ in top) / 3
QUERIES = [("which database did we decide on", "decision"), ("how does the user prefer answers", "preference")]
print("\n3) memory write policy over 160 candidate memories")
for use in (False, True):
    m = store(use)
    p = sum(precision(m, q, w) for q, w in QUERIES) / len(QUERIES)
    print(f"   {'with write policy' if use else 'store everything':<18} stored {len(m):>3}   secrets stored {sum(s for _, _, s in m):>2}   "
          f"top-3 retrieval precision {p:.0%}")

# ---------------- 4. planning when the world changes mid-task ----------------
def world(step, t):                                                  # at time 3 the "flight" option disappears
    return not (step == "book flight" and t >= 3)
PLAN = ["search options", "book hotel", "check budget", "book flight", "write itinerary"]
def fixed_plan():
    calls, t = 1, 0                                                  # one planning call up front
    for step in PLAN:
        t += 1
        if not world(step, t): return False, calls
    return True, calls
def react():
    calls, t = 0, 0
    for step in PLAN:
        calls += 1; t += 1                                           # one reasoning call per step
        if not world(step, t): step = "book train"; calls += 1       # observes the failure and adapts
    return True, calls
def plan_execute_replan():
    calls, t, plan = 1, 0, PLAN[:]
    while plan:
        step = plan.pop(0); t += 1
        if not world(step, t):
            calls += 1; plan = ["book train"] + plan                 # event-driven replan: one extra planning call
    return True, calls
print("\n4) the flight becomes unavailable mid-task")
for name, fn in (("fixed plan (no replanning)", fixed_plan), ("ReAct (reason each step)", react),
                 ("plan-and-execute + replan", plan_execute_replan)):
    ok, calls = fn(); print(f"   {name:<28} {'completed' if ok else 'FAILED   '}   planning calls: {calls}")

Output (deterministic — fixed random seeds):

1) a 12-step task that crashes at step 8
   restart from scratch     steps executed 19 (12 needed)   emails sent 2
   resume from checkpoint   steps executed 12 (12 needed)   emails sent 1

2) 60 tasks; average attempts to find a working tool (lower is better)
   no memory             tasks 1-15: 2.07  16-30: 2.53  31-45: 2.40  46-60: 2.60   lessons stored: 0
   with episodic memory  tasks 1-15: 1.47  16-30: 1.00  31-45: 1.00  46-60: 1.00   lessons stored: 11

3) memory write policy over 160 candidate memories
   store everything   stored 160   secrets stored 10   top-3 retrieval precision 67%
   with write policy  stored  11   secrets stored  0   top-3 retrieval precision 83%

4) the flight becomes unavailable mid-task
   fixed plan (no replanning)   FAILED      planning calls: 1
   ReAct (reason each step)     completed   planning calls: 6
   plan-and-execute + replan    completed   planning calls: 2

⚠️ Notes

  • The infographic's "Memory Architecture" panel has garbled and duplicated labels in its "Memory contents" list (for example "Generated artifacts" appears twice, and two entries are illegible); the list above uses the evident intended items.
  • The memory types mix a published taxonomy (working, episodic, semantic — CoALA, which also lists procedural memory) with a practical category (artifact memory).
  • Our planning comparison is a scripted scenario: it illustrates the call pattern of each strategy, not measured performance of real models.
Checked against sources: CoALA (memory taxonomy); Generative Agents (memory stream with recency, importance and relevance; reflection); Reflexion; MemGPT; Voyager (skill library); Tree of Thoughts; ReAct; Plan-and-Solve; LangGraph persistence; Anthropic's context-engineering guidance on structured note-taking. All "our run" figures are real outputs of the included script.

🔗 References