Principle:0xK3vin MegaMemory Agent Memory Loop
| Knowledge Sources | |
|---|---|
| Domains | Agent_Architecture, Memory_Management |
| Last Updated | 2026-10-27 12:00 GMT |
Overview
A behavioral pattern in which an AI agent explicitly loads context before a task, performs the task, then records what it learned, creating persistent memory across stateless sessions.
Description
The agent memory loop is a three-phase cycle — understand → work → update — that gives a stateless LLM agent continuity across sessions. At session start, the agent loads the current state of the knowledge graph (overview). Before each task, it queries the graph for relevant context (query). After each task, it records new, changed, or obsolete concepts (record). This transforms episodic work into a durable, searchable project model.
Problem solved: LLM agents are stateless between sessions. Without explicit memory, each session starts from scratch, re-deriving project understanding by re-reading code. The memory loop externalizes memory into a graph the agent can query and update, so knowledge accumulates rather than evaporates.
Context: MegaMemory codifies this loop in three places: the skill plugin (plugin/megamemory.ts), the slash commands (commands/bootstrap-memory.md, commands/save-memory.md), and the AGENTS.md instruction snippet installed by the installer. The loop is:
- Session start: list_roots — see the current top-level concepts
- Before tasks: understand — semantic search for relevant context
- After tasks: create_concept / update_concept / link / remove_concept — persist what changed
Usage
Use this principle when:
- You are designing an agent system that needs cross-session memory
- The agent works on a long-lived codebase where accumulated knowledge has value
- You want the agent to build, rather than just use, a knowledge base
- Explicit context loading before work reduces redundant file re-reading
Theoretical Basis
The loop is a stateful cycle over a stateless agent:
Where:
- understand reads relevant context from the knowledge graph given a task description
- work is the agent's task execution (external to the memory system)
- update writes the learned facts back to the knowledge graph
Key invariant: The graph is the sole source of cross-session truth. The agent's internal context is ephemeral; the graph persists. This is analogous to how humans use external notes rather than relying on perfect recall.
Pseudo-code:
# Abstract loop (NOT real implementation)
def session():
roots = list_roots() # orientation
for task in tasks:
context = understand(task.query) # pre-task context
result = perform(task, context) # external work
record(result.learned_facts) # post-task update
verify() # confirm graph grew
Practical Guide
To apply this principle in an agent system:
- Define the memory store: Choose a persistent store (graph DB, vector DB) that supports both structured queries and updates.
- Enforce the loop via prompts: The agent's system prompt or skill plugin must instruct it to query before work and record after work. Without enforcement, agents tend to skip the record phase.
- Keep summaries specific: Instruct the agent to include parameter names, defaults, file locations, and rationale in summaries — vague summaries produce poor semantic search results.
- Cap hierarchy depth: Limit nesting (e.g., 3 levels) to prevent fragmentation.
- Verify after recording: Re-read the roots after updates to confirm the graph grew as intended.