Giving a Memoryless AI Harness a Memory

The problem
At work I use an internal AI coding harness that has no memory. Every new chat starts from zero, so every new chat starts with me pasting the same environment prompt: where things live, how to build, what not to touch. Anything the agent works out during a session is gone when the session ends.
Claude Code doesn't have this problem. It remembers things across sessions, and it does so with nothing more exotic than markdown files. So I set out to rebuild that behaviour from the parts my harness does have.
This post is the design I ended up with, the version I threw away on the way, and the reasons for both. I have only just started using it, so read it as a design, not as a field report.
What I had to work with
The harness is a black box to me, but it exposes four extension points, each at a global and a workspace level:
| Primitive | What it is |
|---|---|
| Rules | A prompt that is included in every chat |
| Workflows | A named series of steps the agent follows on request |
| Skills | Instructions the agent loads when a task calls for them |
| Hooks | Scripts that run at points in the harness's lifecycle |
Two constraints shaped everything else:
- The context window is 200K tokens, so memory has to stay a small, fixed share of it.
- The agent can write files outside the current project, but each such write needs my manual approval. Writes inside the project don't.
I couldn't find out what hooks are able to do, in particular whether a hook's output reaches the model. So the design doesn't use them.
How Claude Code does it
As far as I can tell, Claude Code has two separate layers.
The first is instruction files that you write, CLAUDE.md. They are loaded in full at the start of every session, and they exist at several scopes: one for you everywhere, one checked into the project, one private to you in that project. This is the same idea as Rules.
The second is auto memory, which the model writes for itself:
- Each project gets its own memory folder.
- The folder has an index,
MEMORY.md, with one line per memory. The harness injects the index at the start of each session, up to a size cap of roughly 200 lines. - Each memory is its own small file holding one fact, with a short frontmatter header: a name, a one-line description, and a type.
- The model opens a memory file only when its index line looks relevant to what it is doing.
- The system prompt tells the model what is worth saving, to update an existing memory before creating a new one, and to delete memories that turn out wrong.
- A consolidation pass merges duplicates, fixes stale facts and prunes the index.
What surprised me is what is missing. There is no vector database and no embedding search. It is files, an index, and the model judging relevance from one-line descriptions. That is good news, because it means the whole thing can be reproduced with prompts.
The mapping
Each primitive takes one job:
| Job | Claude Code | My harness |
|---|---|---|
| Load the index every chat | The harness injects it | Global Rules tell the agent to read it first |
| Define what to save, and the format | System prompt | Global Rules |
| Limits and procedures | System prompt | A memory skill |
| Cleanup | Consolidation pass | A consolidate-memory workflow |
| Bootstrap | Not needed | An init-memory workflow |
The first row is the real gap. Claude Code guarantees that the index is in context. Mine depends on the model obeying an instruction to go and read a file. A session-start hook that prints the index would close the gap, if hooks turn out to be able to do that.
First attempt: one global store
My first design put everything in a single folder in my home directory: a global index, a folder of global facts, and a sub-folder per project, with a table mapping workspace paths to project folders. I wanted facts about me and my machine to be available everywhere, and project facts to be fetched on demand.
The approval rule made this awkward. Every write to that folder was outside the project, so every saved fact meant a prompt for me to approve. To cut that down I added an inbox: the agent appended one-line captures to a single file, batched into one write per task, and a daily cleanup promoted inbox lines into proper memory files.
It would have worked, but it had grown an inbox, a promotion step, a projects table and path matching, all to route around one constraint.
Final design: memory inside the project
Moving the store into the project removes the constraint, and with it most of the machinery. This is also how Claude Code scopes its auto memory: per project.
<project>/memory/
MEMORY.md the index, read at the start of every chat
<slug>.md one fact per file
Writes inside the project need no approval, so the agent saves a fact the moment it learns it. The inbox, the promotion step and the projects table are all gone.
A memory file
A made-up example:
---
name: build-needs-license-server
description: before running any build or simulation
metadata:
type: project
---
Builds fail with a misleading "file not found" error when the license server is unreachable. Check the VPN before debugging the build.
**Why:** The error message points at the wrong thing, so it is easy to waste time on the build scripts.
**How to apply:** If a build fails on a missing file that exists, check connectivity first.
There are four types: user (who I am and what I prefer), feedback (how I want the agent to work), project (decisions and conventions the code doesn't record) and reference (where something lives).
The description matters more than it looks. It is the text that ends up in the index, and it is all a future chat has to decide whether to open the file. So it is written as the situation in which the fact matters, not as a summary of the fact.
The index
<!-- consolidated: 2026-12-01 -->
# Memory index
- [Build needs the license server](build-needs-license-server.md) — before running any build or simulation
One pointer per memory and nothing else. The comment on the first line records when the cleanup last ran. It is my one departure from Claude Code's format, and it is there because my harness has no scheduler.
The rules
The Global Rules carry the whole behaviour, so here they are in full:
# Memory
Each project has a persistent, file-based memory in `memory/` at the workspace root. Nothing else carries over between chats, so this folder is the only way you know what earlier chats in this project learned, and the only way later chats will know what you learn now. The `memory` skill has the size limits and the procedures for creating, searching and cleaning the store.
## At the start of every chat
Before your first reply, without being asked:
1. Read `memory/MEMORY.md`. If it does not exist, this project has no memory yet; carry on, and create it the first time you have something to save.
2. If the `consolidated:` date on its first line is earlier than today, run the `consolidate-memory` workflow, then start on the user's request. If you do not know today's date, get it from the system (`date`).
Do not narrate these steps. Use what you read the way a colleague uses what they already know.
## Recall during the chat
- `MEMORY.md` is an index of one-line summaries. When a line is relevant to what you are about to do, open the file it points to before you act.
- Read only what the task needs. Open at most about ten memory files in one chat unless the task clearly needs more.
- A memory records what was true when it was written. Before relying on a path, command, host or version from memory, check that it still holds. If it does not, fix or delete the memory.
## Saving
Save a memory whenever you learn something a future chat would otherwise have to be told again:
- The user corrects you, or tells you how they want something done.
- A preference, or a fact about the user's role, tools or habits.
- An environment fact: paths, commands, hosts, build and test steps, versions, where credentials are kept.
- A project fact that the code does not state: a convention, a decision and its reason, a gotcha you found the hard way.
- A pointer: a URL, dashboard, document or ticket ID.
Do not save the progress of the current task, anything the repository already states, or any secret value (record where a secret lives, never the secret).
When you are unsure whether something is worth keeping, save it. A daily cleanup removes what was not. When the user says "remember this", save immediately.
Each memory is one file in `memory/`, holding one fact:
```markdown
---
name: <short-kebab-case-slug, same as the filename>
description: <one line: the situation in which this fact matters>
metadata:
type: user | feedback | project | reference
---
<The fact, stated so that someone with no other context could act on it. For feedback and project types, follow with **Why:** and **How to apply:** lines. Link related memories as [[their-name]].>
```
Before saving, look in the index for a file that already covers the fact, and update that file if there is one. After writing a new file, add its pointer to `memory/MEMORY.md`: `- [Title](file.md) — <the description>`. The index holds pointers only, never the fact itself. Delete a memory, and its pointer, when it turns out to be wrong. Write absolute dates, never "yesterday" or "next week".
Memory is per project. When you save something that would hold in every project, such as a general preference of the user's, save it here as usual and tell the user in one line that they may want to add it to their Global Rules.
A few choices in there are deliberate:
- The format is in the Rules, not in the skill. A save should be one step. If the agent had to load a skill before every save, it would save less often.
- "When you are unsure, save it." I would rather clean up junk once a day than lose something useful. The write policy is greedy on purpose, and the cleanup is what makes that affordable.
- Memories are treated as claims, not facts. A path or command from three weeks ago may be wrong today, so the agent is told to check before relying on one, and to fix what it finds.
- No secrets. The agent records where a credential lives and never its value.
The skill
The memory skill holds what doesn't need to be in every chat: the size limits, how to create the store for the first time, and how to search it.
Creating the store includes one step I care about. If the project is a git repository, the agent adds memory/ to .git/info/exclude. That keeps memories out of commits without touching the shared .gitignore, so nothing personal ends up in a company repository by accident.
Searching is plain grep. When the index doesn't surface something, the agent greps the folder for names, commands or error text before concluding that memory has nothing.
The cleanup workflow
consolidate-memory runs on the first chat of each day in a project. The Rules trigger it by comparing the date stamp in the index with today's date, which gets me a daily job without a scheduler.
- List the memory files with their modification dates.
- If nothing changed since the last run, update the stamp and stop.
- Open only the files changed since the last run, plus any whose index lines look duplicated, contradictory or dated.
- Delete what is task progress, already in the repository, too vague to act on, or about work that has finished. Merge files that cover the same subject, with the newer one winning where they disagree. Fix relative dates and weak descriptions.
- Enforce the size limits.
- Check that every pointer has a file and every file has a pointer.
- Update the stamp, and report what was merged and deleted in a few lines.
The workflow is told that deleting junk is its job and that it doesn't need to ask first.
The bootstrap workflow
init-memory is optional, since the store is created on the first save anyway. It exists to deal with the environment prompt I had been pasting. I paste it one last time, and the agent sorts it into two piles: facts about this project become memory files, and facts that hold everywhere come back to me as a block of text to put in Global Rules myself.
Keeping it inside the context window
An index that is read in every chat has to be bounded, so the skill sets hard limits:
| Item | Limit |
|---|---|
| Index | 150 pointers |
| One pointer | 150 characters |
| One memory file | 40 lines |
By my rough estimate that makes the index about 5K tokens at its worst, around 2.5% of a 200K window, with each memory file under 1K. The agent is also told to open no more than about ten memory files in a chat unless the task needs more.
The cleanup enforces the limits by merging and deleting, never by raising them.
What about an index that keeps growing?
It can't grow past the cap, but the cap has a cost: once a project has more than 150 facts worth keeping, the cleanup starts deleting useful ones. Claude Code handles growth the same blunt way, with a load cap, one-line pointers, write-time discipline and pruning.
The extension I have in mind, and have not built yet, is an archive tier. When the index is full, the cleanup would move the least useful memories into memory/archive/ and drop their pointers. They would cost nothing at the start of a chat, but grep would still find them.
What this doesn't do
- Loading is instructed, not guaranteed. If the model skips the "read the index first" rule, that chat has no memory. A hook that injects the index would fix this.
- Nothing crosses projects. A preference I state in one project is unknown in the next. Facts that should reach every chat have to go into Global Rules, by hand. The agent points them out when it saves one, but it never edits my rules.
- Deletions are final. The memory folder is excluded from git, so a bad cleanup can't be reverted. The cleanup lists the titles of what it deleted, and that is the only safety net.
- The folder name can collide. In a hardware repository,
memory/is a plausible name for real source. If the folder already exists for something else, the agent stops and asks where the store should go.
Takeaway
Memory for a coding agent turned out to be a convention more than a feature: a folder, an index, one fact per file, and a few paragraphs telling the model when to read and when to write. If your harness lets you put text in front of every chat and lets the agent edit files, you can build it.