Cognitive State · Persistent Context · CoALA Standard

Agent Memory

Give your autonomous agents persistent, multi-tiered context retention and cognitive state management — enabling them to remember decisions, resolve conflicts, and run long-horizon tasks for pennies.

Origin: The concept of Agent Memory traces back to cognitive architecture models like CoALA (Cognitive Architectures for Language Agents) published by Princeton and CMU in 2023. By early 2026, memory has become the central problem of enterprise AI. As teams shift from simple chat interfaces to autonomous agents executing multi-step workflows, statelessness has gone from a minor inconvenience to a catastrophic failure point.

Problem: Standard LLMs are stateless. Every prompt is a fresh start. RAG can retrieve static documents, but it cannot track how an agent’s knowledge evolves. If your agent decides on an architectural path on Tuesday, it will forget that decision on Wednesday unless you re-explain the entire history. This leads to context drift, token rot, and memory hallucinations—costing companies millions in bloated inference fees and broken workflows.

Solution: Agent Memory Cascade—a multi-tiered state persistence system that allows agents to read, write, update, and resolve conflicts in their own context over time. By combining volatile working memory with long-term episodic, semantic, and procedural stores, agents gain persistent identities and reliable task continuity.


RAG vs. Agent Memory

The most common architectural error in production AI is treating RAG as a substitute for memory. They are fundamentally different subsystems:

DimensionRetrieval-Augmented Generation (RAG)Agent Memory
StateStateless retrievalStateful persistence
Scope“What does this static document say?”“What has this agent learned, and has it changed?”
Session BoundaryResets each invocationPersists across sessions and tasks
Write CapabilityRead-only at inference timeRead + Write + Update + Delete
Temporal ReasoningStatic timestamp filteringDynamic temporal validity (what was true when)
Conflict ResolutionReturns all matching fragmentsSelf-edits to maintain a single source of truth

Memory Pipelines

A production-grade memory system should never read or write directly to database storage. Instead, it relies on structured validation and retrieval pipelines:

The Memory Write Pipeline

Before a new observation is committed to long-term memory, it runs through an extraction and safety filter:

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│ 1. Extract Fact │ ──> │  2. Classify    │ ──> │  3. Resolve     │ ──> │  4. Mask PII &  │
│   From Context  │     │   Memory Type   │     │    Conflicts    │     │   Persist Fact  │
└─────────────────┘     └─────────────────┘     └─────────────────┘     └─────────────────┘
  1. Extraction: The system isolates candidate facts, preferences, or events from the active thread.
  2. Classification: The memory is routed based on taxonomy (e.g. Episodic vs. Semantic vs. Procedural).
  3. Conflict Resolution: The write pipeline checks for contradictions with existing records. If the user downgraded from “Pro” to “Free”, the old fact is updated or deprecated rather than duplicated.
  4. Safety & Storage: PII is redacted, and the cleaned memory is persisted to the appropriate storage backend with an audit trail.

The Memory Retrieval Pipeline

When the agent receives a new query, memories are surfaced dynamically to fit the context window:

  1. Scope Identification: Detect if the query requires user-scoped, session-scoped, or organization-scoped knowledge.
  2. Multi-Signal Querying: Query vector databases for similarity, graph stores for relations, and SQL databases for exact facts.
  3. Relevance & Recency Ranking: Score retrieved candidates based on semantic match, recency, and importance.
  4. Context Injection: Format and pack the top-K memories into the active context window, removing stale records.

LLM Wiki Pattern (Karpathy Design)

A concrete implementation pattern for agent memory systems based on Andrej Karpathy’s LLM Wiki design. Instead of treating memory as simple RAG retrieval, this pattern structures agent memory as a persistent, compounding wiki that the LLM actively maintains and updates.

The Problem with RAG-Based Memory

Most agent memory systems work like traditional RAG: retrieve relevant chunks at query time and generate answers. This approach has fundamental limitations:

IssueImpact
No accumulationEvery question starts from scratch — the agent rediscovers knowledge repeatedly
Synthesis overheadComplex questions requiring 5+ documents force the agent to piece together fragments each time
No cross-reference persistenceConnections between concepts are rebuilt rather than maintained
Static knowledgeThe system doesn’t track how understanding evolves over time

The Wiki Solution

The LLM Wiki pattern fundamentally changes this equation. Instead of just retrieving from raw documents at query time, the agent incrementally builds and maintains a persistent wiki — a structured, interlinked collection of markdown files that sits between you and the raw sources.

The key difference: When you add a new source, the agent doesn’t just index it. It:

  1. Reads and comprehends the content
  2. Extracts key information
  3. Integrates it into the existing wiki — updating entity pages, revising summaries, flagging contradictions
  4. Strengthens or challenges the evolving synthesis

The knowledge is compiled once and kept current, not re-derived on every query.

Three-Layer Architecture

┌─────────────────────────────────────────────────────────┐
│                    RAW SOURCES                           │
│  Articles • Papers • Images • Data Files                │
│  (Immutable — source of truth)                           │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│                      THE WIKI                            │
│  Summaries • Entity Pages • Concepts • Comparisons      │
│  (Agent-generated markdown — you read, agent writes)    │
└─────────────────────────────────────────────────────────┘
                            ↓
┌─────────────────────────────────────────────────────────┐
│                      THE SCHEMA                          │
│  Structure • Conventions • Workflows                    │
│  (Configuration file — makes agent disciplined)         │
└─────────────────────────────────────────────────────────┘

Layer 1: Raw Sources

Your curated collection of source documents — articles, papers, images, data files. These are immutable. The agent reads from them but never modifies them. This is your source of truth.

Layer 2: The Wiki

A directory of agent-generated markdown files containing:

  • Summaries and entity pages
  • Concept pages and comparisons
  • Overview and synthesis documents

The agent owns this layer entirely. It creates pages, updates them when new sources arrive, maintains cross-references, and keeps everything consistent. You read it; the agent writes it.

Layer 3: The Schema

A configuration document (e.g., CLAUDE.md for Claude Code or AGENTS.md for other agents) that defines:

  • Wiki structure and organization
  • Naming conventions and formatting standards
  • Workflows for ingesting sources, answering questions, maintaining the wiki

This is what makes the agent a disciplined wiki maintainer rather than a generic chatbot.

Core Operations

1. Ingest

When you add a new source, the agent processes it through a structured flow:

New Source → Read & Discuss → Write Summary → Update Index →
Update Entity Pages → Update Concept Pages → Append to Log

A single source might touch 10-15 wiki pages as the agent integrates the new information comprehensively.

2. Query

When you ask questions, the agent:

StepAction
SearchFinds relevant wiki pages
SynthesizeReads and creates answers with citations
FormatOutputs in multiple formats: markdown, tables, slides, charts, canvases

Key insight: Good answers can be filed back into the wiki as new pages. Your explorations compound in the knowledge base just like ingested sources do.

3. Lint

Periodically, the agent health-checks the wiki for:

CheckPurpose
ContradictionsIdentifies conflicts between pages
Stale claimsFinds information superseded by newer sources
Orphan pagesDetects pages with no inbound links
Missing pagesIdentifies important concepts lacking dedicated pages
Broken cross-referencesLocates outdated links and data gaps
Web search opportunitiesSuggests areas where external research could fill gaps

Two special files help navigate the growing wiki:

index.md (Content Catalog)

A content-oriented catalog listing:

FeatureDescription
Page listingsEvery page with a link and one-line summary
MetadataOptional date and source count information
OrganizationStructured by category (entities, concepts, sources, etc.)

The agent updates this on every ingest. When answering queries, it reads the index first to find relevant pages, then drills down.

log.md (Chronological Record)

An append-only timeline of wiki activity:

Entry TypeRecords
Ingest operationsNew sources processed and integrated
Query sessionsQuestions asked and answers generated
Lint passesMaintenance runs and health checks

Pro tip: Use consistent prefixes (e.g., ## [2026-04-02] ingest | Article Title) to make the log parseable with Unix tools:

grep "^## \[" log.md | tail -5  # Last 5 entries

Why This Pattern Works

The tedious part of maintaining a knowledge base isn’t reading or thinking — it’s bookkeeping:

Bookkeeping TaskWhy It’s Hard for Humans
Updating cross-referencesEasy to miss connections across dozens of pages
Keeping summaries currentRequires re-reading and re-synthesizing regularly
Noting data contradictionsNeeds systematic review of all related content
Maintaining consistencyScales poorly as wiki grows

Humans abandon wikis because the maintenance burden grows faster than the value.

Agents excel at this because they:

Agent AdvantageBenefit
Never get boredRepetitive tasks don’t degrade quality over time
Never forgetCross-references are always updated when relevant
Parallel processingCan touch 15 files in a single operation
Zero marginal costMaintenance scales without additional effort

Division of Labor

Human RoleAgent Role
Curate sourcesExtract and summarize
Direct analysisUpdate cross-references
Ask good questionsMaintain consistency
Think about meaningHandle bookkeeping
Review and validateFile and organize

The human focuses on high-level thinking and curation. The agent handles everything else.


Context Engineering Strategies

Context engineering determines how retrieved memories are presented within the model’s token limit. Standard implementations rely on three patterns:

SLIDING WINDOW
┌─────────────────────────┬─────────────────────────┐
│   Summarized History    │   Verbatim Last Turns   │
└─────────────────────────┴─────────────────────────┘

HIERARCHICAL SUMMARIZATION
┌─────────────────┬───────────────────┬─────────────┐
│  Yearly (1 sentence) │ Monthly (1 paragraph) │ Verbatim    │
└─────────────────┴───────────────────┴─────────────┘

MEMORY OFFLOADING (ACE)
┌───────────────┬───────────────────┬───────────────┐
│ Static Prompt │ Retrieved Memory  │ Active Task   │
└───────────────┴───────────────────┴───────────────┘

1. Sliding Window

Keeps only the most recent $N$ turns verbatim. Older turns are discarded or summarized. Excellent for simple chatbots but poor for long-horizon planning.

2. Hierarchical Summarization

Compresses history at varying levels of abstraction. The last turn is fully preserved, the last session is summarized into a paragraph, and the older history is distilled into single-sentence semantic facts.

3. Memory Offloading (ACE)

Treats the context window like CPU RAM and external databases like a hard drive. Active task context is kept light, and all other facts are fetched via tools or dynamically injected based on semantic triggers.


Governance & Safety Checklist

When deploying memory systems in enterprise environments, safety and compliance are paramount:

  • Enforce PII Masking: Never allow agents to commit raw SSNs, passwords, or emails to long-term memory. Use deterministic regex masking pre-write.
  • Define Retention Policies: Establish expiration dates for volatile memory blocks (e.g., cart items or temporary session keys).
  • Isolate User Scopes: Ensure memories from User A never leak into the retrieval path of User B.
  • Audit Memory Writes: Log all automated memory edits and updates, allowing administrators to roll back corrupted states.
  • EU AI Act Attestation: Ensure your memory retrieval logic can output explanation logs detailing why a specific memory was surfaced to influence a decision.

Core Taxonomy

The building blocks of the Agent Memory cascade — from immediate working context to long-term storage

Working Memory

Immediate in-context workspace (the active context window). Ephemeral, fast, and token-bounded. Holds the current conversation thread, active tool outputs, and short-term reasoning traces.

📜

Episodic Memory

A timestamped record of specific experiences, user interactions, and decisions. Preserves instance-specific contexts (the who, what, when, and why) to prevent loss of task continuity across session boundaries.

🧠

Semantic Memory

Abstracted, generalized facts, domain concepts, organizational rules, and user preferences. Evolved and updated over time with active conflict resolution to maintain factual consistency.

⚙️

Procedural Memory

Standard operating procedures, routing workflows, coding standards, and skill sets (such as SKILL.md libraries). Allows the agent to execute complex rules automatically without reasoning from scratch.

🕸️

Vector & Graph Stores

The physical execution layer. Vector stores handle fast fuzzy semantic similarity, while temporal graph stores manage entity relationships and multi-hop reasoning over time.

Key Benefits

Why structured memory elimination of context repetition enables reliable production agent workflows

🛡️

Zero Context Drift

Prevents agents from forgetting previous decisions, contradicting instructions, or repeating already answered questions within long-running sessions.

🔄

Stateful Continuity

Allows agents to resume work seamlessly across independent sessions. Long-term memory survives session resets and boundaries.

⚖️

Conflict Resolution

Features active verification pipelines to detect and resolve contradictions (e.g. user downgrading a plan), updating facts instead of stacking duplicates.

📦

Context Window Packing

Maximizes usable token capacity by compressing history and offloading inactive facts to external storage, keeping startup costs minimal.

📝

Auditable Governance

Supports strict retention policies, privacy boundaries (PII masking), and structured audit logging for enterprise compliance.

Common Use Cases

From GDPR compliance auditing to multi-agent shared states and personalization

👤

Personalization & Preferences

Remembering specific user settings, writing styles, corporate guidelines, and saved settings across independent workflows.

🔒

GDPR Compliance Auditing

Enforcing privacy boundaries by auditing schemas or code for unmasked PII before committing writes to database stores.

🤝

Multi-Agent Collaboration

Sharing task-scoped context and episodic findings between collaborating agents while keeping private reasoning isolated.

💻

Long-Horizon Code Editing

Retaining architectural design decisions, coding conventions, and gotchas across a multi-day coding session.

Interactive Tools

Practical tools to help you think systematically, build better AI agents, and master prompt engineering.

Future References

Explore these resources for deeper learning on AI agent development, spec-driven development, and prompt engineering tools.

Spec-Driven Development

Comprehensive guide on Spec-Driven Development practices and methodologies.

Awesome Copilot

Curated list of GitHub Copilot resources, extensions, and best practices.

Promptfoo

Tool for testing, evaluating, and improving LLM prompts and applications.

Prompts.chat

Collection of prompt engineering resources and templates.

Agent Skills

Agent skills resources and documentation for building AI agent skills.

Awesome Skills

Curated list of awesome skill repositories and collections.

Agent Skills Topic

GitHub topic for discovering agent-related skills and repositories.

AI Agent Topic

Trendshift topic for discovering AI agents.

AI Skills Topic

Trendshift topic for discovering AI skills.

Agent Governance Toolkit

Agent governance toolkit.

Pattern Sources

Our patterns are curated from industry-leading sources with proper attribution and licensing compliance.

Refactoring.Guru

Classic GoF design patterns, code smells catalog, and refactoring techniques (https://refactoring.guru).

Enterprise Integration Patterns

65 messaging patterns for integrating enterprise applications by Gregor Hohpe and Bobby Woolf (CC BY 4.0).

Microservices.io

Comprehensive patterns for microservice architectures by Chris Richardson.

Agent Catalog Patterns

Patterns for agentic systems from agentpatternscatalog.org (CC BY 4.0).

OWASP Foundation

Security patterns from OWASP Top 10 for Web Applications, LLM Applications, and Agentic Applications (CC BY-SA 4.0).

Industry Research

ML/AI patterns from Microsoft, Google, Anthropic, and academic research.

AI Agent Patterns

Spec-driven development patterns from Claude, Gemini, OpenAI, and GitHub Copilot on github/spec-kit and OpenSpec.

Data Engineering Leaders

Data platform patterns from Martin Fowler (Data Mesh), Kimball Group (Dimensional Modeling), and cloud providers.

MLOps Best Practices

Data science patterns from MLflow, Great Expectations, and MLOps practitioners.

Streaming & Analytics

Real-time patterns from Confluent/Kafka, Apache projects, and serverless analytics platforms.

Academic Papers

Rigorous ML patterns from peer-reviewed research including data leakage prevention and active learning.

5-Day AI Agents Course

Intensive Vibe Coding Course With Google by Brenda Flynn et al. (2026) on Kaggle.

The Agent Loop

Foundational Agent Definition (Perceive + Act):
Russell, S. J., & Norvig, P. (1995). Artificial Intelligence: A Modern Approach. Prentice Hall. (Current edition: 4th Ed., Pearson, 2020)

Modern Iterative LLM Agent Loop:
Yao, S., Zhao, J., Yu, D., et al. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv:2210.03629.

Historical Context:
Incorporating AIMA's perceive/act model, Classical robotics' Sense-Plan-Act loop (Brooks, 1986), and ReAct's Thought→Action→Observation cycle.