Blog

Part 3 of 3 · AI agents, from scratch6 min read

How to Build a Simple Memory Loop

Give your AI agent a memory with a brief, a journal and two Claude Code hooks. A step-by-step guide, based on how we built ours.

Brandon Lopez — Founder, Lyte Works

  • ai-agents
  • memory
  • how-to
  • claude-code
  • hooks

In Context Is Everything, we landed on a simple routine for a mind with no memory: wake up, read the brief, do the work, write it down, update the brief. Repeat. That's a memory loop.

Here's how to build one in your own setup, based on how we built ours. You don't need to write an app. You need a folder, an AI agent, and about twenty minutes.

The examples use Claude Code, because that's what we run. Codex and most other agents read AGENTS.md instead of CLAUDE.md, and the same ideas carry over. Claude Code now reads AGENTS.md too, when there's no CLAUDE.md, which is great.

Easiest way to do all of this? Give this post to your agent and ask it to set it up for you.

What you're building

your-business/
  CLAUDE.md          ← the page by the bed
  journal.md         ← what happened, what we learned
  lessons/           ← what's proven worth keeping
  .claude/
    settings.json    ← the reflexes
    hooks/
      journal-check.sh

Steps 1 to 3 are just writing. Step 4 is where the harness starts doing the remembering for you.

Step 1: Write the page by the bed

Create CLAUDE.md at the top of your folder. Claude Code reads it automatically every time it starts. That's a reflex it already has.

Keep it short. Who the agent is, where things live, and how it remembers:

# Sunrise Bakery

You're the assistant for Sunrise Bakery, a family bakery in Dover, NH.
Owner: Maria. Open Tuesday to Sunday, 6am to 2pm.

## Where things live
- Menu and prices: menu/
- Orders and customers: orders/
- How we write to customers: voice.md

## How we remember
- journal.md is what happened before. Read the latest entries first.
- lessons/ is what we've learned for sure. Check it before you start.
- Before you finish, add a dated entry to journal.md.
- If anything on this page is no longer true, fix it.

If this page is getting long, that's a sign something belongs in its own file. Point to it instead.

Step 2: Start the journal

Create journal.md. Every entry gets a date, what was done, and what was learned:

## 2026-10-15 — Updated the holiday menu
Did: added three pies to menu/holiday.md, raised the pumpkin pie to $24.
Learned: Maria wants prices ending in 4 or 9, never round numbers.
Next: the order form still shows the old price. Fix it.

The "Learned" line is the one that matters. "Did" tells tomorrow-you what happened. "Learned" stops it from making the same mistake twice.

Step 3: Make it a rule

That "How we remember" section in step 1 is the rule. Most of the time, the agent will follow it.

Most of the time. Remember the reflexes from What Is an AI Agent? Instructions in a file are a request.

Here's a restaurant example. Working in restaurants has taught me a lot about how to use agents. Every restaurant should have steps of service that each server and bartender follows. Greet the table a certain way. Take the order and make sure the table has the right setup. Check back to make sure everything's going well. Clear empty plates and glasses to keep the table neat. That's their workflow, their set of instructions. They can and should follow it every time. But sometimes they get overwhelmed. Too much is going on, they forget things, and not everything gets done to a T.

Your agent does the same. On a long task, it gets focused on the work and skips part of the workflow, like writing anything down. That's exactly when you most want it written down. So you make it a rule, then turn the rule into a reflex, and the important parts happen every time.

Step 4: Turn the rule into reflexes

Claude Code lets you add your own reflexes, called hooks. You need two.

When the agent wakes up, show it the end of the journal. CLAUDE.md already loads on its own, so this adds the most recent history right next to it.

When the agent tries to finish, check whether the journal was written in. If it wasn't, the agent gets sent back to write it.

Create .claude/settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "tail -n 40 \"$CLAUDE_PROJECT_DIR/journal.md\"" }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR/.claude/hooks/journal-check.sh\"" }
        ]
      }
    ]
  }
}

Then create .claude/hooks/journal-check.sh:

#!/bin/bash
# Runs every time the agent tries to finish.
input=$(cat)

# Already sent back once? Let it go, so it never gets stuck in a loop.
echo "$input" | grep -q '"stop_hook_active": *true' && exit 0

# Journal written in the last two hours? All good.
if [ -n "$(find "$CLAUDE_PROJECT_DIR/journal.md" -mmin -120 2>/dev/null)" ]; then
  exit 0
fi

# Otherwise, send it back with a reminder.
echo "Before you finish: add a dated entry to journal.md (did, learned, next). Fix anything in CLAUDE.md that's no longer true." >&2
exit 2

Make it runnable with chmod +x .claude/hooks/journal-check.sh. That's a Mac and Linux command. On Windows, ask your agent for the equivalent.

That's the whole trick. Exiting with code 2 tells Claude Code "not yet," and the reminder goes straight to the agent. It reminds once, then lets go. Our own version works the same way: if an agent did real work and didn't write anything down, it doesn't get to stop until it does.

The two-hour window is the simple version. It works fine for most setups. Ours checks the actual session instead, but you don't need that to start.

Step 5: Promote what's worth keeping

A journal grows fast, and a long journal is a messy room all over again. This is a lot like the basic memory files most AI providers keep from your conversations, just organized. Once a week, give your agent this:

Read journal.md. Find anything we've learned more than once, or anything that would have saved real time if we'd known it earlier. Write each one as a short file in lessons/, with one line at the top saying when it applies. Then move the old journal entries into journal-archive.md, keeping only the last two weeks.

Raw notes go in the journal. Proven lessons go in lessons/. That's the difference between a diary and a playbook.

Step 6: Keep it true

Stale notes are worse than no notes. If CLAUDE.md says the menu lives in menu/ and you moved it, the agent will confidently look in the wrong place.

The Stop reminder already asks the agent to fix anything on the page that's no longer true. Every so often, read CLAUDE.md yourself. It should take two minutes. If it takes ten, it's too long.

What we added on top

This is the core of what runs at Lyte Works. Over time, we added more:

  • Roles. Each kind of work has its own folder with its own brief and its own journal, so the marketing agent isn't reading the developer's notes.
  • A reviewer. One role's whole job is reading the journals, promoting lessons and retiring the ones that went stale.
  • Recall. When a new task comes in, the lessons that match it get pulled up automatically.
  • Guides. Before an agent touches code, it has to have read the rules for doing it right.

You don't need any of that to start. Start with the page, the journal and the two hooks. Once it's running, you'll see where it wants to grow.

And if you'd rather have it built and running for you, that's what we do.

Frequently asked questions

What do I need to build a memory loop?

A folder, an AI agent and about twenty minutes. A CLAUDE.md (or AGENTS.md) page by the bed, a journal.md, a lessons folder, and two hooks in .claude/settings.json. You don't need to write an app.

What do the two hooks do?

The SessionStart hook shows the agent the end of the journal when it wakes up. The Stop hook checks whether the journal was written in; if it wasn't, the agent gets sent back once to write it, then it's let go.

Does this work with Codex or other agents?

The ideas carry over. Codex and most other agents read AGENTS.md instead of CLAUDE.md, and Claude Code reads AGENTS.md if there's no CLAUDE.md. The hook setup shown here is Claude Code's.

How do I keep the journal from getting too long?

Once a week, have your agent promote anything learned more than once into a short file in lessons, then move old entries into journal-archive.md, keeping only the last two weeks.

Want this handled for you?

Tell us what’s heavy. We turn it into Lyte Work.