How to Build a Claude Code Agent That Learns From Each Loop (Exact Setup Inside)

@zodchiii
INGLÉShace 3 semanas · 24 jun 2026
128K
196
35
24
460

TL;DR

This article provides a detailed technical guide on creating a learning loop for Claude code agents. By implementing a persistent journal and long-term lesson files, developers can prevent agents from repeating failed attempts and help them learn codebase-specific patterns.

Most looping agents have amnesia. Each cycle starts fresh, so they retry the same failed fix three times because nothing remembers it already didn't work.

A learning agent keeps a journal: after every attempt it writes down what it tried and whether it worked, then reads that journal before the next attempt.

Set it up once and the loop stops repeating itself. Each pass starts from what the last one learned instead of from zero.

Here's the full setup you need👇

Before we dive in, I share daily notes on AI & vibe coding in my Telegram channel: https://t.me/zodchixquant🧠

darkzodchi - inline image

Why loops repeat themselves

A normal loop runs the same agent on the same problem again and again, but the agent's memory resets between cycles. It doesn't know cycle 2 already tried the thing it's about to try in cycle 4.

So it loops in circles, swapping the same library, reverting, swapping it back, burning tokens rediscovering dead ends. The loop runs, but it doesn't learn.

The fix is memory that survives between cycles: a journal the agent writes to and reads from every pass.

darkzodchi - inline image

File 1: the journal

This is the memory. A plain file the agent appends to, never overwrites. Create .claude/loop-journal.md:

markdown
1# Loop journal
2Append-only. Each attempt: what was tried, the result, the lesson.
3
4## Task: fix failing checkout test
5
6### Attempt 1
7Tried: added await to the fetchCart call.
8Result: still failed, same error.
9Lesson: the race isn't in fetchCart. Look upstream at the cart state.
10
11### Attempt 2
12Tried: memoized the cart selector.
13Result: failed differently now, cart is undefined on first render.
14Lesson: memoization changed timing. The real issue is initial state.

The journal is dead simple on purpose. Every entry is three lines: what was tried, what happened, and the lesson for next time.

The lesson line is the gold, it's what stops the next cycle from repeating the attempt.---

File 2: the learning loop

This is the orchestrator, and the one rule that makes it learn: read the journal first.

Drop into .claude/commands/learn-loop.md:

yaml
1---
2description: Run a task in a loop that learns from a journal each pass
3argument-hint: <task>
4allowed-tools: Read, Write, Edit, Glob, Grep, Bash
5model: sonnet
6---
7
8Task: $ARGUMENTS
9
10Each cycle:
111. Read .claude/loop-journal.md fully. Note what was already
12 tried and what was learned. Never repeat a failed attempt.
132. Form a new hypothesis that the journal doesn't rule out.
143. Make the change. Run the check.
154. Append to the journal: what you tried, the result, the lesson.
165. Passed: stop, summarize what worked. Failed: go to step 1.
176. Cap at 6 cycles.
18
19The rule: every cycle must try something the journal hasn't.
20If you can't think of one, say so and stop. That's not failure,
21that's the journal telling you to get a human.

Step 1 is the whole article. By forcing the agent to read its own history before acting, each cycle builds on the last instead of restarting. The loop accumulates knowledge instead of spinning.

File 3: enforce the write

An agent will skip journaling if it's optional, so make it automatic. This hook reminds the agent to log after every edit. Add to .claude/settings.json:

json
1{
2 "hooks": {
3 "PostToolUse": [
4 {
5 "matcher": "Edit|Write",
6 "hooks": [
7 { "type": "command", "command": "test -f .claude/loop-journal.md && echo 'Reminder: log this attempt and its result in loop-journal.md before the next cycle.'" }
8 ]
9 }
10 ]
11 }
12}

It's a nudge, not a wall, but it works. After each change the agent gets reminded to record what it just did, so the journal stays current. A journal written as you go is accurate.

One written from memory is fiction.

What you actually see

You type one line:

text
1/learn-loop the checkout total is wrong when a coupon is applied

Then each cycle builds on the last:

text
1Cycle 1
2 journal: empty
3 tried: recalculated total after coupon. failed, off by tax.
4 logged: coupon applied before tax, should be after.
5
6Cycle 2
7 journal: cycle 1 says coupon goes after tax
8 tried: moved coupon after tax. failed, now negative on big coupons.
9 logged: no floor on discount. cap at item total.
10
11Cycle 3
12 journal: 2 lessons, both applied
13 tried: capped discount at item total. PASSED.
14
15done in 3 cycles, 0 repeats. journal saved for next time.

Notice cycle 2 didn't retry cycle 1's mistake, the journal ruled it out. And the journal survives, so next time a coupon bug shows up, the agent starts with everything it already learned.

Level up: memory that spans every task

The journal so far learns within one task. The bigger win is memory that carries across all of them, so the agent stops making the same class of mistake on every new feature.

Add a second, permanent file, .claude/lessons.md, for patterns worth keeping forever:

markdown
1# Lessons (permanent)
2Patterns the agent has learned the hard way. Read before any task.
3
4## This codebase
5- Dates are always UTC in the DB, local only in the UI layer.
6- The auth middleware runs twice in tests, mock it once per file.
7- Never trust the cached user object, it lags by one request.
8
9## General reflexes
10- A test that passes alone but fails in the suite = shared state.
11- "undefined is not a function" after a refactor = a moved export.
12- Money math: integer cents, never floats. Every time.

Then add one line to the loop command, near the top:

text
10. Read .claude/lessons.md before anything. These are
2 permanent rules learned from past tasks. Apply them upfront.

Now when a loop finds something that isn't a one-off, the agent promotes it from the task journal to lessons.md.

The next task, even weeks later, starts with that baked in.

The journal is short-term memory, lessons.md is long-term: the difference between an agent that learns a task and one that learns your codebase.

How to write a lesson that actually helps

A vague lesson is worse than none, because it adds noise without preventing a repeat. The format decides whether the journal makes the next loop smarter or just longer.

Bad lesson:

text
1Something was wrong with the dates.

Good lesson:

text
1Tried: compared timestamps directly. Failed, off by timezone.
2Lesson: DB stores UTC, the input was local. Convert before
3comparing. Symptom to watch for: off-by-hours in date tests.

The good one names three things: what was tried, the actual cause, and the symptom that signals this problem next time.

That third part makes it reusable. When the agent sees "off-by-hours in a date test" three weeks later, it jumps straight to the timezone fix.

A lesson is only worth writing if a future cycle could pattern-match on it.

Common mistakes

Overwriting the journal. If each cycle replaces the file instead of appending, you lose the history and the loop forgets again. Append-only, always. The whole value is the accumulation.

Logging the attempt but not the lesson. "Tried X, failed" tells the next cycle what happened but not what to do differently. The lesson line is what prevents the repeat. Without it, the journal is just a diary, not a guide.

Letting the journal grow forever. After 30 attempts the journal itself bloats the context. Once a task is solved, archive its entries to a separate file and start the active journal clean.

Reading the journal at the end, not the start. Some agents log diligently but only check the journal when they're stuck. The read goes first, every cycle, or the memory does nothing.

Never promoting lessons to permanent. If everything stays in the per-task journal, the agent relearns your codebase's quirks on every new feature. Promote the patterns that repeat to lessons.md, or you're paying to learn the same thing twice.

The 12-minute setup

2 minutes: create .claude/loop-journal.md with the three-line entry format.

2 minutes: create .claude/lessons.md for permanent, cross-task patterns.

3 minutes: create the learning loop at .claude/commands/learn-loop.md, with the read-lessons-first step.

2 minutes: add the journaling reminder hook to settings.json.

3 minutes: run /learn-loop on a real bug and watch each cycle skip what the last one ruled out.

The agent didn't get smarter. It just stopped forgetting, and a loop that remembers beats a loop that repeats every single time.

Thanks for reading!

I share daily notes on AI, finance, and vibe coding in my Telegram channel: https://t.me/zodchixquant

darkzodchi - inline image
Guardar con un clic

Lee artículos virales en profundidad con IA en YouMind

Guarda la fuente, haz preguntas concretas, resume el argumento y convierte un artículo viral en notas reutilizables en un único espacio de trabajo con IA.

Explora YouMind
Para creadores

Convierte tu Markdown en un artículo de 𝕏 impecable

Cuando publicas tus propios textos largos, dar formato en 𝕏 a imágenes, tablas y bloques de código es un fastidio. YouMind convierte un borrador completo en Markdown en un artículo de 𝕏 impecable y listo para publicar.

Prueba Markdown a 𝕏

Más patrones por descifrar

Artículos virales recientes

Explorar más artículos virales