Build Your Own Dev Agent — Lesson 4: Memory and Inline Learning
When you correct the agent, it writes the correction to a file immediately -- not at the end of the day, not on a cron schedule, but in the same turn. This is inline learning, and it is what separates a useful agent from one you have to retrain every session.
Where You Are
your-project/
CLAUDE.md
.claude/
preferences.md
tasks-active.md
progress.txt
settings.json
hooks/
stop-telegram.sh
permission-gate.sh
See It: The Learning System
The agent maintains two files for learning:
error-log.md— Mistakes the agent made. Invoked: read at every session startup so the agent never repeats a logged mistake. Written to immediately when you correct an error — same turn, no delay.learnings.md— Patterns that work, approaches that failed, preferences discovered. Invoked: read at startup for context. Written to inline when tasks succeed or fail in notable ways. The learning-loop skill reads this nightly to promote repeated patterns to CLAUDE.md.
Autonomy rules (what needs approval vs what the agent can do alone) are handled by Claude Code's built-in permission system in settings.json, not a separate file.
The critical rule: learn immediately, in the same turn as the correction. If the user says "don't format code that way," the agent does two things: (1) fixes the formatting, and (2) appends the rule to preferences.md. Same turn. No delay.
See It: The Routing Logic
Different corrections go to different files:
- "Don't do X" —
.claude/preferences.md(Don'ts section) - Style or format correction —
.claude/preferences.md - Bug or error the agent made —
.claude/error-log.md - Approach that worked well —
.claude/learnings.md(Patterns section) - Approach that failed —
.claude/learnings.md(Mistakes section) - "You should ask me before doing X" —
.claude/preferences.md(Don'ts section)
This routing is defined in CLAUDE.md. The agent reads the rules and follows them. You do not need to tell it which file to update -- it routes automatically based on the type of correction.
See It: Why Not Just Use a Cron?
Inline learning captures corrections immediately, before context compaction can erase them and before the agent repeats the same mistake. The daily Learning Loop cron is for consolidation -- reviewing the day and promoting repeated patterns. They supplement each other: inline for real-time capture, cron for nightly review.
Build It: Learning Files
Two new files. Tell Claude Code what you need:
Prompt for Claude Code:
I need two new files in .claude/:
1. error-log.md — a log where you record every mistake you make.
Include the date, what happened, what should have happened.
You'll read this at session startup so you never repeat an error.
2. learnings.md — track three things: patterns that work, mistakes
that failed, and user preferences you discover through corrections.
Start with empty sections for each.
Expected output: Two files in .claude/. Review them — the agent creates reasonable formats. Adjust as needed.
Build It: Update CLAUDE.md with Learning Rules
Now wire the learning system into the master instruction file.
Intent: Add inline learning rules to CLAUDE.md so the agent knows how to route corrections.
Prompt for Claude Code:
Append the following section to CLAUDE.md:
## Inline Learning (CRITICAL)
Learning happens in real-time, not on a cron schedule.
When the user corrects you:
1. Do what they asked
2. Immediately append to the right file IN THE SAME TURN:
- Style/format correction --> .claude/preferences.md
- "Don't do X" --> .claude/preferences.md (Don'ts section)
- Bug/error you made --> .claude/error-log.md
- Approach that worked well --> .claude/learnings.md (Patterns)
- Approach that failed --> .claude/learnings.md (Mistakes)
- "You should ask before doing X" --> .claude/preferences.md (Don'ts)
When you complete a task:
- Append one line to progress.txt
- Update tasks-active.md
- If you discovered something useful, append to learnings.md
Rule: Never wait for a cron job to learn. Learn immediately.
Expected output: CLAUDE.md now includes inline learning and autonomy sections.
Build It: Update Session Startup
Now that you have three new files, update the session startup in CLAUDE.md to read them.
Intent: Add the new files to the session startup checklist in CLAUDE.md.
Prompt for Claude Code:
Update the Session Startup section of CLAUDE.md. Replace the existing
startup steps with:
## Session Startup
1. Read agent identity + rules:
- .claude/preferences.md -- who I am and my rules
- .claude/error-log.md -- past mistakes (read carefully, don't repeat)
2. Read agent state:
- .claude/tasks-active.md -- pending work
- .claude/progress.txt -- recent actions
- .claude/learnings.md -- accumulated patterns
3. Resume any in-progress work from tasks-active.md
Expected output: Updated startup section that includes all learning files.
Checkpoint
Your .claude/ directory should now contain: preferences.md, tasks-active.md, progress.txt, error-log.md, learnings.md, settings.json, hooks/stop-telegram.sh, hooks/permission-gate.sh. CLAUDE.md should include inline learning sections.
This is part of the Build Your Own Dev Agent course. ← Previous Lesson | Next Lesson →