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)
Callmem_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 — description4
Learned
Gotchas, edge cases, things that surprised you. Omit if none — don’t force this section.
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)
- First call
mem_context— checks recent session history (fast, cheap) - If not found, call
mem_searchwith relevant keywords (FTS5 full-text search) - If you find a match, use
mem_get_observationfor 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
Topic Update Rules (Mandatory)
When saving evolving topics (architecture, long-running features), follow these rules:Rule 1: Different topics must NOT overwrite each other
Rule 1: Different topics must NOT overwrite each other
Use distinct topic keys for different concerns:
architecture/auth-model≠bug/auth-nil-panicdecision/state-management≠pattern/component-structure
auth for both architecture and bugfixes — they’ll overwrite each other.Rule 2: Reuse the same topic_key for evolving topics
Rule 2: Reuse the same topic_key for evolving topics
When a decision changes over time, use the same topic key to update:
Rule 3: Use mem_suggest_topic_key if unsure
Rule 3: Use mem_suggest_topic_key if unsure
If you’re not sure what topic key to use:
Rule 4: Use mem_update when you have an exact ID
Rule 4: Use mem_update when you have an exact ID
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)
Without this, the next session starts blind.Session Summary Format
- Example: Feature Work
- Example: Bugfix Session
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.
Compaction Example
Passive Capture (Optional)
When completing a task, you can include a## Key Learnings: section at the end of your response:
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 (default)
- personal
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:
Memory Protocol Checklist
Use this checklist to verify protocol compliance:During Work
During Work
- Called
mem_saveafter 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_keyfor evolving decisions - Searched memory proactively before starting overlapping work
Before Session End
Before Session End
- Called
mem_session_summarywith 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
After Compaction
After Compaction
- Called
mem_session_summaryto persist compacted work (FIRST ACTION) - Called
mem_contextto 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:Memory Protocol (Full Text)
Memory Protocol (Full Text)
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.)