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.
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
| Concept | What it is | Examples |
|---|---|---|
| Context | What the model can see right now, in the current prompt; limited by the context window | Current conversation, retrieved documents, recent tool results |
| Memory | Information stored outside the model that can be retrieved later | Past interactions, learned facts, previous attempts |
| State | Structured information about the current task and its progress | Current step, completed steps, open items, variables |
| Plan | Intended future actions to achieve the goal; can be updated | Task list, dependencies, success criteria |
| Artifacts | Durable outputs produced during the task, stored externally | Files, 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
| Type | Holds | Examples |
|---|---|---|
| Working | Short-term, active information for the current task | Current goal and plan, recent observations and tool results, intermediate calculations |
| Episodic | Past interactions, attempts and outcomes | What worked and what didn't, previous plans and results, user feedback |
| Semantic | Stable, long-term knowledge and facts | Domain knowledge, project and company knowledge, learned structured information |
| Artifact | Files and structured outputs created during or across tasks | Documents, 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
- User goal — "Plan and execute a marketing campaign for our new product."
- Retrieve relevant memory — past tasks, knowledge, artifacts.
- Create or update the plan — break into steps, considering previous experience.
- Choose the next task, then act with a tool or subagent.
- Observe the result — tool output, feedback, errors.
- Update state — mark the step complete, store results.
- Reflect — what worked, what to improve?
- Completed? If not, replan if needed and repeat; if so, write useful memory (decisions, results, lessons) and return the final result.
Planning Patterns
| Pattern | How it works | Pros | Cons |
|---|---|---|---|
| ReAct | Decide one step at a time: reason → act → observe | Flexible and simple | Can be myopic; a model call per step |
| Plan-and-execute | Create a full plan first, then execute steps one by one | Better for complex tasks; fewer planning calls | The plan can go stale |
| Planner–executor–reviewer | Add a critic to check progress and quality | Higher quality, fewer errors | More complex and costly |
| Hierarchical | Break a high-level goal into subgoals and steps | Good for large, complex tasks | Needs more coordination |
| Graph / branching | Explore several approaches and pick the best (as in Tree of Thoughts) | Can find better solutions | Higher cost and complexity |
| Event-driven | Replan when new information, feedback or external events arrive | Adaptable and robust | Needs good state management |
We simulated a trip-planning task where the flight becomes unavailable partway through:
| Pattern | Outcome | Planning calls |
|---|---|---|
| fixed plan (no replanning) | ❌ failed | 1 |
| ReAct (reason each step) | ✅ completed | 6 |
| plan-and-execute + replan | ✅ completed | 2 |
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:
| Recovery | Steps executed (12 needed) | Emails sent |
|---|---|---|
| restart from scratch | 19 | 2 |
| resume from checkpoint | 12 | 1 |
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:
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:
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.
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.