How do you give AI agents memory that works with any model?

Keep agent memory in Markdown files versioned in git, with one canonical file read by Claude Code, opencode and others. How I built it and where it broke.

Pedro Henrique Quadro4 min read

The simplest way is to keep memory in Markdown files in your repository, with one canonical file that describes the project and the rules, and to point every tool at it. That way the memory is yours, readable and versioned, and switching models or tools does not erase what was learned. I built this for myself and published a template on GitHub, brain-template. The fragile part is each tool's configuration, not the files.

The problem

Every AI coding tool forgets: the context window runs out, the session expires, and what you taught one vendor stays locked in it. The Claude Code documentation says this plainly: each session starts with an empty context window, and two mechanisms carry knowledge from one session to the next, the CLAUDE.md files you write and the auto memory the tool writes by itself. The second lives in the format and place the tool chooses. The first is text you own.

What the tools document

  • AGENTS.md is an open format: a plain Markdown file, with no required fields, holding instructions for coding agents. The format's page lists dozens of compatible tools, including Codex, Gemini CLI, Cursor and opencode.
  • Claude Code reads the repository's AGENTS.md, but only when there is no CLAUDE.md in the working directory or above it. If both exist, it reads only CLAUDE.md. To read both, the documentation says to have CLAUDE.md import AGENTS.md with the @path syntax. Imports accept relative or absolute paths and go up to four levels deep. An import that points outside the working directory asks for approval the first time.
  • opencode looks for AGENTS.md walking up from the current directory. Through the instructions field in the config file, it accepts paths and glob patterns, and merges everything with AGENTS.md.

The Claude Code documentation itself notes that these instructions are context, not enforced configuration. To actually block an action, it points to a hook, a command that always runs. In other words, memory in a file guides, it does not enforce.

How I organized it

At the root of my notes vault there are:

  • AGENTS.md: the canonical context, written once. CLAUDE.md and its equivalents are thin files that only point to it, so there are never two versions of the same text.
  • GUARDRAILS.md: the protocol for how to work, as opposed to what to know. Evidence before claiming, a plan before implementing, proof before saying "done". There is a condensed version, with numbered imperative rules, for open models.
  • Memória/ (the Portuguese word for memory): one fact per file, with the reason and how to apply it, plus a one-line-per-fact index.
  • One file per long task, with where I stopped and what I decided and why, so work can continue in another tool.

brain-template is that skeleton without my content. Its README lists Claude Code, Gemini CLI and opencode as the tools it is designed for, and the license is MIT.

Where it broke

On August 16, 2026, I found that my own note said something untrue. I had recorded that opencode loaded the protocol through a symbolic link and that the config had permission rules. I measured: the config file was 50 bytes, just the header, and the link's folder did not exist. For that whole period, the open models ran with no protocol at all.

I rebuilt it the same day using the instructions field with an absolute path. I tested it: one of the models returned rule 6 word for word without opening a file. That an absolute path outside the repository works is something I measured; the opencode page I read does not detail it.

The same day, an update to the opencode package left the program off the PATH, the list of folders where the system looks for commands.

One divergence is also worth recording. I had noted earlier that a CLAUDE.md import could not leave the project tree. Today's documentation says it can, with approval the first time. I follow the documentation.

What to do

  1. Write the context in one canonical file and make the others point to it.
  2. Store facts one per file, with the reason, and version them in git.
  3. Do not record momentary state in memory. That goes in the task file.
  4. After configuring each tool, ask it for a rule by number. If it does not know, the file is not being read.
  5. For what cannot fail, use a hook or a permission, not just text.

To set this up for your team: production AI agents.

Sources

  1. Claude Code, memory, CLAUDE.md and AGENTS.md (AGENTS.md reading, @ imports, external import approval)
  2. opencode, rules (AGENTS.md and the instructions field with paths and globs)
  3. AGENTS.md, open format (plain Markdown; list of compatible tools)
  4. brain-template, the author's public repository
Pedro Henrique QuadroSoftware and AI engineer. Builds apps, systems, dashboards and AI agents in production, with code merged into Supabase, Kestra and QuestDB.

Keep reading