Skip to main content

Overview

The Memory Protocol is a set of behavioral rules that teaches AI agents when and how to use Engram’s memory tools. Without it, agents have the tools but no guidance on when to call them. The protocol is injected via:
  • System prompt (OpenCode, Gemini CLI, Codex, VS Code)
  • Skill file (Claude Code)
  • Agent rules (Cursor, Windsurf, Antigravity)
The protocol is enforced as mandatory — not optional. This ensures memory continuity across sessions and context resets.

When to Save (Mandatory)

Call mem_save IMMEDIATELY after any of these:

Bug Fix Completed

Save what was wrong, why it broke, how you fixed it, and any gotchas.

Architecture/Design Decision

Save the decision, tradeoffs considered, and why this choice was made.

Non-Obvious Discovery

Save anything surprising about the codebase that future sessions should know.

Configuration Change

Save environment setup, config changes, or tool installations.

Pattern Established

Save naming conventions, code structure patterns, or team standards.

User Preference Learned

Save user constraints, preferences, or coding style requirements.

Format for mem_save

Every saved observation must follow this structure:

Content Structure Rules

1

What

One sentence — concise description of what was done.
2

Why

What motivated this? User request, bug, performance issue, tech debt?
3

Where

File paths — which files were affected. Format: src/path/file.ts — description
4

Learned

Gotchas, edge cases, things that surprised you. Omit if none — don’t force this section.
Title should be short and searchable — like a commit message. Bad: “Fixed a bug”. Good: “Fixed N+1 query in user list”.

When to Search Memory

Reactive Search (User Asks)

When the user asks to recall something — any variation of:
  • “remember”
  • “recall”
  • “what did we do”
  • “how did we solve”
  • “recordar” (Spanish)
  • “acordate” (Spanish)
  • “qué hicimos” (Spanish)
Protocol:
  1. First call mem_context — checks recent session history (fast, cheap)
  2. If not found, call mem_search with relevant keywords (FTS5 full-text search)
  3. If you find a match, use mem_get_observation for full untruncated content

Proactive Search (Overlap Detection)

Search memory BEFORE starting work when:
  • Starting work on something that might have been done before
  • User mentions a topic you have no context on — check if past sessions covered it
  • About to implement a pattern — check if the team has a preferred approach
Proactive search prevents duplicate work and ensures consistency with past decisions.

Topic Update Rules (Mandatory)

When saving evolving topics (architecture, long-running features), follow these rules:
Use distinct topic keys for different concerns:
  • architecture/auth-modelbug/auth-nil-panic
  • decision/state-managementpattern/component-structure
Bad: Using auth for both architecture and bugfixes — they’ll overwrite each other.
When a decision changes over time, use the same topic key to update:
If you’re not sure what topic key to use:
If you know the observation ID and want to correct it:
This does not increment revision_count — it’s a correction, not an evolution.

Session Close Protocol (Mandatory)

This is NOT optional. Before ending a session or saying “done” / “listo” / “that’s it”, you MUST call mem_session_summary.
Without this, the next session starts blind.

Session Summary Format

The session summary is stored as a special observation with type: "session_summary" and is automatically injected into the next session’s context.

After Compaction (Critical)

When you see a message about compaction or context reset, or if you see “FIRST ACTION REQUIRED” in your context:
1

Persist compacted work

IMMEDIATELY call mem_session_summary with the compacted summary content.This persists everything done before compaction.
2

Recover context

Call mem_context to load context from previous sessions.
3

Continue working

Only then continue with the user’s request.
Do not skip step 1. Without it, everything done before compaction is lost from memory forever.

Compaction Example

Passive Capture (Optional)

When completing a task, you can include a ## Key Learnings: section at the end of your response:
Engram will automatically extract and save these as observations with type: "learning".
This is a safety net — it captures knowledge even if you forget to call mem_save explicitly. But explicit mem_save calls are still preferred for important work.

Scope: Project vs Personal

Observations can be scoped:
Project scope — shared with the team, relevant to this codebase.Use for:
  • Architecture decisions
  • Bugfixes
  • Code patterns
  • Team conventions
When searching with mem_search, you can filter by scope: mem_search({ query: "...", scope: "project" }) or scope: "personal".

Privacy: The <private> Tag

Wrap sensitive content in <private> tags to strip it before storage:
Privacy stripping happens at two layers (plugin + store). Even if the plugin fails, the store layer catches it.

Memory Protocol Checklist

Use this checklist to verify protocol compliance:
  • Called mem_save after each significant action (bugfix, decision, discovery)
  • Used structured What/Why/Where/Learned format for content
  • Short, searchable titles (not “fixed bug”, but “fixed N+1 in UserList”)
  • Used topic_key for evolving decisions
  • Searched memory proactively before starting overlapping work
  • Called mem_session_summary with Goal/Discoveries/Accomplished/Files format
  • Summary includes ALL significant work, not just the last task
  • Listed relevant files with descriptions
  • Added Next Steps for future sessions
  • Called mem_session_summary to persist compacted work (FIRST ACTION)
  • Called mem_context to recover previous session context
  • Only then continued with user’s request

Full Protocol Text (For Copy-Paste)

For agents without plugin support, add this to your agent’s system prompt or rules file:
What: One sentence — what was done Why: What motivated it (user request, bug, performance, etc.) Where: Files or paths affected Learned: Gotchas, edge cases, things that surprised you (omit if none)

Goal

[What we were working on this session]

Instructions

[User preferences or constraints discovered — skip if none]

Discoveries

  • [Technical findings, gotchas, non-obvious learnings]

Accomplished

  • [Completed items with key details]

Next Steps

  • [What remains to be done — for the next session]

Relevant Files

  • path/to/file — [what it does or what changed]

Next Steps

How It Works

Memory system, session lifecycle, and 3-layer pattern

Architecture

System architecture, components, and data flow

MCP Tools

Complete reference for all 13 memory tools

Agent Setup

Set up Engram with your agent (OpenCode, Claude Code, etc.)