OpenCode Memory Internals OpenCode's apparent cross-session memory is not an autonomous long-term memory manager but a combination of three distinct mechanisms: instruction files injected into the system prompt, durable session events stored in SQLite, and compaction checkpoints that replace old context. The investigation found that a project-local instructions configuration loaded guidance files on every provider turn, but those files were created by an agent via a file tool after an explicit user request, making the behavior user-triggered file authoring plus deterministic prompt injection. The source tree includes both the desktop-compatible session path under packages/opencode and the newer V2 runtime under packages/core, with differences noted. OpenCode Memory Internals I started this investigation after finding a local OpenCode project that appeared to remember guidance across sessions. The guidance lived in Markdown files outside the repository, yet every new session followed it. The operational question was simple: had OpenCode decided to preserve those facts, o I started this investigation after finding a local OpenCode project that appeared to remember guidance across sessions. The guidance lived in Markdown files outside the repository, yet every new session followed it. The operational question was simple: had OpenCode decided to preserve those facts, or had someone explicitly written them? The session record answered it. An agent had created the files through an ordinary file tool after an explicit user request. A project-local instructions configuration then loaded them on every provider turn. What looked like autonomous memory was user-triggered file authoring plus deterministic prompt injection. That result sent me looking for the actual memory subsystem. There is no general runtime-managed service that decides what to save, updates facts when they change, and semantically retrieves useful knowledge in later sessions. What users experience as "memory" is produced by three different mechanisms with different owners and failure modes: instruction files are loaded into the system prompt, durable session events and projected messages are persisted in SQLite, and old model-visible context is replaced by a generated compaction checkpoint when the request grows too large. These mechanisms work together, but they do not form an autonomous long-term memory manager. That distinction matters. If a coding agent remembers a project rule because AGENTS.md is injected on every turn, that is not learned memory. If it can reopen an old transcript from SQLite, that does not mean a new session can retrieve facts from it. If a long session survives by summarizing its history, that does not mean the runtime selected the most important information. This article follows the current OpenCode source tree, which contains both the desktop-compatible session path under packages/opencode and the newer V2 runtime under packages/core. Where the two paths differ, I call out the difference rather than treating them as one implementation. Primary code references: packages/opencode/src/session/instruction.ts packages/opencode/src/session/prompt.ts packages/opencode/src/session/llm/request.ts packages/opencode/src/session/system.ts packages/opencode/src/session/prompt/beast.txt packages/opencode/src/tool/read.ts packages/opencode/src/session/compaction.ts packages/opencode/src/session/message-v2.ts packages/opencode/src/command/index.ts packages/opencode/src/command/template/initialize.txt packages/core/src/instruction-context.ts packages/core/src/session/context-epoch.ts packages/core/src/session/runner/llm.ts packages/core/src/session/history.ts packages/core/src/session/sql.ts packages/core/src/session/compaction.ts packages/core/src/database/database.ts OpenCode does not have one memory system. It has an explicit instruction layer, a durable session layer, and a bounded working-context layer. Calling all three "memory" hides the most important operational differences. Project knowledge is mostly prompt injection. AGENTS.md, CLAUDE.md, and configured instructions are read from files and placed in the system prompt. The loader reads them; it does not maintain them. Session persistence is not cross-session recall. OpenCode durably stores messages, tool calls, tool results, and durable events in SQLite. Streaming text, reasoning, tool-input, and compaction deltas are transient. The stored data makes sessions reopenable and inspectable, but it remains scoped to the session unless another component explicitly reads it. Compaction changes what the model sees, not what the database retains. Old history remains durable, while normal model requests continue from a generated checkpoint plus recent context. OpenCode does not have a general runtime policy that promotes conversation facts into long-term memory. There is no built-in embedding index, vector retrieval path, user-profile store, or fact confidence model. One provider-specific prompt contains a narrow file-memory convention, but core does not manage or retrieve that file as a memory service. /init is guided instruction authoring, not background learning. It asks the active agent to create or improve AGENTS.md after the user invokes the command. The legacy and V2 instruction paths are not identical. The desktop-compatible path supports AGENTS.md, optional CLAUDE.md, configured local files, and HTTP instruction sources. The current V2 InstructionContext observes AGENTS.md files only. A Better Mental Model The easiest way to reason about OpenCode memory is to ask two questions for every piece of information: Where is it stored? Is it included in the next model request? Those are not the same question. Layer Stored in How it reaches the model Scope Ambient instructions Markdown files or configured URLs Injected as system context Project or configured scope, across sessions Durable session history SQLite messages, parts, inputs, and durable events Selected history is converted into model messages One session Compaction checkpoint A durable summary message or compaction event Replaces older active history in later requests One long session The request path is approximately: agent system prompt + ambient instructions + selected session history + latest user input + tool definitions - provider request The database may contain much more than the provider request. A project may also contain instructions that are reloaded independently of the transcript. "The runtime has it" and "the model can currently reason over it" are separate properties. In the desktop-compatible path, instruction discovery lives in packages/opencode/src/session/instruction.ts. It collects several classes of sources. The global source is an AGENTS.md under the OpenCode config directory, with optional compatibility loading for ~/.claude/CLAUDE.md. At project level, it searches upward for instruction files. AGENTS.md is preferred, followed by CLAUDE.md, then the deprecated CONTEXT.md; the first filename family with matches wins. The project configuration may also add instructions entries that resolve to local paths, glob patterns, or HTTP URLs. The loader reads those sources and renders each one with its origin: Instructions from: /path/to/AGENTS.md