Engram is a persistent memory system that gives OpenCode agents a durable record of decisions, bug fixes, discoveries, and conventions that survives beyond any single session, context window reset, or compaction event. Without it, every new session starts completely blind — no memory of the architecture choices made last week, the gotcha found in the payment module, or the naming convention the team agreed on. With Engram active, the Tony Stark agent and the SDD orchestrator carry continuous, searchable context across every conversation, making each session faster and more coherent than the last.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/lnardev/opencode-config-agent/llms.txt
Use this file to discover all available pages before exploring further.
How it works
The Engram plugin (plugins/engram.ts) sits between OpenCode’s event system and a local engram Go binary that manages an SQLite-backed observation store. The plugin auto-starts the binary on session open if it is not already running.
- Session registration — creates a session record in engram on
session.created, keyed to the project name (resolved from git remote origin URL) - Prompt capture — records every user message (
chat.messagehook) for session context reconstruction - Tool accounting — counts non-Engram tool calls per session (
tool.execute.afterhook) - System prompt injection — appends
MEMORY_INSTRUCTIONSto every outgoing system prompt so the agent always knows the memory protocol, even after compaction
Proactive save triggers
The agent is instructed to callmem_save immediately and without being asked after any of the following. This is a mandatory protocol, not a suggestion:
- Architecture or design decision made
- Team convention documented or established
- Workflow change agreed upon
- Tool or library choice made with tradeoffs
- Bug fix completed (including root cause)
- Feature implemented with a non-obvious approach
- Notion / Jira / GitHub artifact created or updated with significant content
- Configuration change or environment setup completed
- Non-obvious discovery about the codebase
- Gotcha, edge case, or unexpected behavior found
- Pattern established (naming, structure, convention)
- User preference or constraint learned
mem_save NOW.”
mem_save format
Every observation saved to Engram follows this structure:
| Field | Description |
|---|---|
title | Verb + what — short, searchable. Example: "Fixed N+1 query in UserList" |
type | bugfix | decision | architecture | discovery | pattern | config | preference |
scope | project (default) or personal |
topic_key | Stable key for evolving topics — enables upserts instead of duplicate entries. Example: architecture/auth-model |
content | Structured block: What (one sentence), Why (motivation), Where (files/paths), Learned (gotchas — omit if none) |
- Different topics must not overwrite each other
- The same evolving topic should reuse the same
topic_key(upsert behaviour) - When unsure about the right key, call
mem_suggest_topic_keyfirst - When you have an exact observation ID to correct, use
mem_update
Example mem_save call
When to search memory
The agent searches Engram on two triggers: reactive (user asks) and proactive (agent decides). Reactive — search when the user uses any of these signals:- “remember”, “recall”, “what did we do”, “how did we solve”
- “recordar”, “qué hicimos”, “acordate” (Spanish equivalents)
- Any reference to past work, a previous session, or prior decisions
- Starting work on something that might have been done before
- User mentions a topic the agent has no current context on
- User’s first message references the project, a feature, or a problem — search immediately before responding
mem_context (fast, cheap)
Check recent session history first. This is a lightweight call that covers the last few sessions.
mem_search (if not found)
Run a full-text search with relevant keywords from the user’s message against all stored observations.
Session close protocol
Before ending any session — before saying “done”, “listo”, or “that’s it” — the agent must callmem_session_summary. This is non-negotiable. Skipping it means the next session starts with no record of what was accomplished.
The summary uses a fixed structure:
Compaction recovery
When a session’s context window fills up, OpenCode triggers a compaction event. The old agent instance effectively ends, and a new one starts with a compressed summary of the conversation. This is where memory loss typically occurs — and where Engram’s compaction hook intervenes. What the plugin does during compaction (experimental.session.compacting hook):
- Fetches recent session context from Engram and injects it into the compaction output — so the new agent starts with persistent memory, not just the compressed transcript
- Appends a
CRITICAL INSTRUCTIONinto the compaction prompt that tells the compressor to include the following instruction at the top of the compacted summary:
Call mem_session_summary immediately
Persist the compacted summary content to Engram. This is the first action — before any other work. Without it, everything done before compaction is lost from memory.
MCP tools reference
Engram exposes its capabilities through MCP tools. The full set tracked by the plugin is defined in theENGRAM_TOOLS constant in plugins/engram.ts:
| Tool | Purpose |
|---|---|
mem_save | Save a new memory observation (decision, bug fix, discovery, pattern, etc.) |
mem_search | Full-text search (FTS5) across all observations in the project |
mem_get_observation | Retrieve the full, untruncated content of an observation by ID |
mem_context | Get recent session context — fast and cheap, check this first |
mem_session_summary | Save a structured session summary (mandatory at session close) |
mem_update | Update an existing observation by ID (use when you know the exact ID) |
mem_suggest_topic_key | Ask Engram to suggest a stable topic_key before saving, to avoid duplicates |
mem_stats | Observation and session statistics for the project |
mem_timeline | Chronological view of observations |
mem_session_start | Utility — mark a session as started |
mem_session_end | Utility — mark a session as ended |
mem_delete | Delete an observation by ID |
mem_save_prompt | Save a prompt (used internally by the plugin’s chat.message hook) |
Engram tools are explicitly excluded from the plugin’s tool-call counter. Only non-Engram tool calls are counted in session statistics — so Engram usage never inflates your session’s tool usage metrics.