Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xK3vin MegaMemory Agent Memory Loop

From Leeroopedia
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:

  1. Session start: list_roots — see the current top-level concepts
  2. Before tasks: understand — semantic search for relevant context
  3. 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:

statet+1=update(work(understand(statet,taskt)))

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:

  1. Define the memory store: Choose a persistent store (graph DB, vector DB) that supports both structured queries and updates.
  2. 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.
  3. Keep summaries specific: Instruct the agent to include parameter names, defaults, file locations, and rationale in summaries — vague summaries produce poor semantic search results.
  4. Cap hierarchy depth: Limit nesting (e.g., 3 levels) to prevent fragmentation.
  5. Verify after recording: Re-read the roots after updates to confirm the graph grew as intended.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment