OpenCode Memory Plugin
OpenCode Memory is a persistent memory system for AI coding agents, providing long-term context retention across sessions using local vector database technology. The package, currently at version 2.13.0, receives frequent minor and patch releases, indicating active development. Key differentiators include its local-first approach using SQLite and USearch (with ExactScan fallback), automated user profile learning, a unified memory-prompt timeline, and a full-featured web UI for management. It supports multi-provider AI (OpenAI, Anthropic) and integrates with over 12 local embedding models, offering intelligent prompt-based memory extraction, smart deduplication, and built-in privacy protection. It acts as an OpenCode plugin, meaning its core functionality is exposed to the AI agent runtime, rather than being directly imported into typical application code.
Common errors
-
Error: Plugin 'opencode-mem' not found in OpenCode configuration.
cause The `opencode-mem` plugin has not been correctly added to the OpenCode global configuration file.fixAdd `"opencode-mem"` to the `plugin` array in your `~/.config/opencode/opencode.json` file, then restart OpenCode. Example: `{ "plugin": ["opencode-mem"] }` -
Error: Failed to load embedding model: [model name] / Could not initialize Transformers.js
cause The configured `embeddingModel` (e.g., `Xenova/nomic-embed-text-v1`) failed to load, potentially due to network issues, missing local cache, or incompatible environment.fixVerify network connectivity if using a remote model. Check that `Bun` (recommended runtime) is properly installed and configured. Try a different, well-tested embedding model. Ensure sufficient disk space and memory for local model caching. -
Error: Invalid API Key format. Expected 'sk-...' or 'file://...' or 'env://...'
cause The `memoryApiKey` in `opencode-mem.jsonc` is not in one of the supported formats or is missing.fixEnsure your API key is correctly specified. Examples: `"sk-..."` for direct keys, `"file://~/.config/opencode/api-key.txt"` for a file path, or `"env://OPENAI_API_KEY"` to read from an environment variable. -
Log message: 'USearch unavailable, falling back to ExactScan'
cause The preferred `USearch` vector indexing library could not be initialized, often due to native module compilation issues or runtime environment specifics. The plugin defaults to a less performant `ExactScan` method.fixWhile `ExactScan` functions, for optimal performance, investigate the `bun install` logs for any errors related to `USearch` during plugin installation or build. Ensure your system meets any potential native dependency requirements for `USearch` if the issue persists.
Warnings
- breaking Version 2.13.0 included a fix to restore compatibility with the OpenCode 1.3 loader. Older versions of `opencode-mem` (prior to 2.13.0) may have experienced compatibility issues or outright failures when used with OpenCode 1.3. Users on OpenCode 1.3 should ensure they are running `opencode-mem` v2.13.0 or higher.
- gotcha Incorrect or incomplete configuration of AI providers or API keys can lead to non-functional memory features. The plugin supports both OpenCode's internal providers (recommended for ease of use) and manual API configurations. Mismatched `opencodeProvider`/`opencodeModel` with `memoryProvider`/`memoryModel`/`memoryApiKey` settings is a common error.
- gotcha The plugin defaults to `USearch` for vector indexing but includes an automatic fallback to `ExactScan` if `USearch` is unavailable or fails at runtime. While this ensures functionality, `ExactScan` may have significantly lower performance for large memory bases compared to an optimized `USearch` setup.
- gotcha OpenCode's plugin loading mechanism means that the `memory()` function is exposed to the AI agent runtime, not typically imported directly by a user's local TypeScript/JavaScript code for execution outside the agent context. Developers wishing to interact with the plugin programmatically or extend it should primarily consider the TypeScript types and the `memoryPlugin` export for integration.
Install
-
npm install opencode-mem -
yarn add opencode-mem -
pnpm add opencode-mem
Imports
- memoryPlugin
import { memoryPlugin } from 'opencode-mem'; - MemoryActionParams
import type { MemoryActionParams } from 'opencode-mem'; - MemoryAction
import type { MemoryAction } from 'opencode-mem';
Quickstart
{
"plugin": [
"opencode-mem"
]
}
// Save the above JSON to ~/.config/opencode/opencode.json to enable the plugin.
// After OpenCode restarts, your AI agent can interact with memory.
// Example agent interactions (within an OpenCode session):
// memory({ mode: "add", content: "Project uses microservices architecture" });
// memory({ mode: "search", query: "architecture decisions" });
// memory({ mode: "profile" });
// memory({ mode: "list", limit: 10 });
// You can also configure the plugin in ~/.config/opencode/opencode-mem.jsonc
// For example, to set an embedding model:
// {
// "embeddingModel": "Xenova/nomic-embed-text-v1"
// }