memory

AI agent memory: four approaches, one comparison

An AI agent forgets everything when its session ends. Persistent memory is anything that survives between sessions. This page compares the four main approaches: context stuffing, vector retrieval, structured databases, and plain files.

What agent memory actually means

Working memory is the context window. It exists only while the session is active. Persistent memory is knowledge that survives between sessions. The agent writes it to storage during one session and reads it back at the start of the next.

The four approaches are context stuffing, vector retrieval, structured databases, and plain files. Most teams use one or two. None of them is universally best.

Approach 1: Context stuffing

Context stuffing puts persistent knowledge directly into the system prompt. Every session loads the same block of text. Examples: CLAUDE.md, AGENTS.md, .cursorrules, and custom system-prompt injections.

  • Zero infrastructure. The memory is a text file in your repo.
  • Full inspectability. Open the file and read every line the agent sees.
  • Simple correction. Edit the file. The next session picks up the change.

The problem is scale. Every line rides every prompt. No selective loading. Once the file exceeds a few hundred lines, important rules get buried. Most implementations are read-only for the agent.

Approach 2: Vector retrieval

Vector retrieval stores knowledge as embeddings and retrieves by similarity search. Tools like mem0, Zep, and LangMem use this pattern. At query time, the system finds the closest vectors and injects them into the prompt.

  • Scales to large corpora. Retrieval cost is logarithmic.
  • Fuzzy recall. Finds relevant facts even when the query wording differs from the stored text.

The weaknesses: opaque (you cannot open a file and read the memory), approximate (relevant facts can be missed), hard to correct (no find-and-replace), and requires infrastructure (embeddings model, vector database, ingestion pipeline).

Approach 3: Structured databases

Structured databases store agent state in SQL tables, graph databases, or key-value stores. The agent queries through generated SQL, a custom API, or a graph traversal.

  • Precise queries. Exact results, no approximation.
  • Scales to terabytes. Indexing and optimization are mature.
  • Transactional. ACID guarantees keep data consistent.

Requires a schema up front. The agent needs a query layer. Overkill for working notes and learnings. Inspection requires a database client.

Approach 4: Plain files

Plain-file memory stores knowledge as markdown, JSON, or YAML files in a folder. The agent reads and writes files by path. The folder lives on disk, in git, or behind an MCP server. This is the approach gcontext uses.

  • Inspectable in any editor. No special tooling required.
  • Diffable with git. Every change shows up in a pull request.
  • Portable. Copy the folder, push it to a remote, or share it with a teammate.
  • The agent can restructure its own memory. No migration scripts.

Scale ceiling is megabytes. No fuzzy recall. Requires naming discipline. These limits matter only when the corpus grows to thousands of documents.

Comparison

Context stuffingVector retrievalStructured DBPlain files
Setup costNoneModerateHighLow
Write pathHuman onlySDK callQueryFile write
InspectabilityFullOpaqueQuery requiredFull
Correction workflowEdit fileRe-embedMigrationEdit file
Cross-tool reachOne toolOne appOne appAny MCP client
Scale ceilingKilobytesGigabytesTerabytesMegabytes
Cost modelPer-tokenHosting + APIHostingDisk only

Context stuffing and plain files share the "edit file" correction workflow and full inspectability. The difference is selective loading: context stuffing loads everything, plain files let the agent load only what it needs.

The case for files first

Most agent memory is small and high-value. A project has 20 decisions, 30 API conventions, 10 deployment notes, and a handful of learnings. This fits in a few dozen files totaling a few hundred kilobytes. Files handle this well.

Start with files. Add vector retrieval only when the corpus outgrows what file search can handle. Most teams never reach that crossover. The recommendation is not "files forever." It is "files first."

Files over MCP

gcontext packages approach 4 as an open-source MCP server. It serves a folder of plain text files and exposes read_file, write_file, list_dir, grep, and script execution tools. Any MCP client can connect and use the same state folder.

The agent now has persistent, readable, diffable memory. Every file it writes survives the session. Push the folder to git and it survives the machine.

install and connect
$ uv tool install gcontext-ai
$ gcontext init my-project
$ gcontext up my-project
$ claude mcp add --transport http my-project http://127.0.0.1:4242/mcp

Questions

What is the difference between agent memory and RAG?

RAG retrieves from a corpus the agent does not own or edit. The documents are external: knowledge bases, help centers, codebases. Agent memory is state the agent writes and reads as part of its work. The agent creates it, updates it, and relies on it in future sessions. RAG is read-only retrieval. Agent memory is read-write state.

Do I need a vector database for agent memory?

Not usually. Most agent memory is small: project decisions, API quirks, deployment logs, team conventions. File-based storage handles this well. Add vector search when your corpus grows to thousands of documents and you need fuzzy recall by meaning rather than by path.

How do agents remember across sessions?

By reading persistent storage at the start of each session. The storage can be files on disk, rows in a database, or embeddings in a vector store. The agent needs tools to read and write that storage. Without persistent storage, the agent starts fresh every time.

Related

What is agent state?
Definition and management patterns
Claude Code memory
What persists and what does not in Claude Code
LangGraph memory
Checkpointers, Store, and the file layer
CrewAI memory
How CrewAI handles memory and where files help
Stateful MCP servers
The pattern behind gcontext
Start with files

Install gcontext. Give your agent readable, writable, diffable memory.

View on GitHubWhat is agent state?