Base: anatomy of the loop|INEMA.CLUB-PRO

Lesson 1 · 15 min

The loop in 5 pieces: goal, state, action, check, stop

Every agent loop that works has the same five pieces, from /goal to the shell loop. When a loop fails, almost always one is missing: it doesn't say where it wants to get, it forgets where it was, it approves itself, or it doesn't know when to stop. Here you learn to see the five and run a toy loop, with no model, that shows them.

Start the lesson
Braided black cable running through five different metal clips on a dark surface.

In this lesson you will

  1. 1The five pieces and the question each one answers
  2. 2Which piece is missing when the loop spins aimlessly or stops early
  3. 3A toy loop in bash that stops on the test or the cap
  4. 4Where each piece is explored in depth across the six courses

Goal: identify the 5 pieces in any agent loop and run a toy loop that stops on its own, by the test or by the cap.

01The five pieces of every loop

Tap each block: the question the piece answers and what happens when it is missing.

Scroll the page: each step below lights up its part of the diagram.

  1. 1/6

    Goal

    One sentence with an end you can check: a file with 5 lines, a test suite passing, an empty queue. Without it the loop has no way to know it is done, and it stops when the quota runs out or when the model decides it looks good enough.

  2. 2/6

    State

    What has been done, what is left, and how to resume. In a long loop the model's context gets compacted, reset, or moved to a new session; whatever lives only in the conversation is lost. That is why the state lives outside the model (lesson 5).

  3. 3/6

    Action

    What the agent does in this cycle: edit a file, run a command, ask for a tool. A small step is a verifiable step; five changes at once don't tell you which one broke.

  4. 4/6

    Verification

    What says the step worked is a command, not the agent's opinion: the test runs and prints "6 passed", wc counts 5 lines. It is the piece that is missing most often and costs the most when it is missing. The 0–4 scale is in lesson 6; how to build the evaluator, in course 1.

  5. 5/6

    Stop

    Two exits, always: the good one (the goal was verified) and the safety one (a cap was hit or it stopped making progress). A loop without a cap is the one that spins all night on the same broken test. The five caps are in lesson 7.

  6. 6/6

    Five questions per cycle

    Where do I want to end up? Where am I? What is the next step? Did it work, with proof? Do I stop or continue? A loop that answers all five with a file and a command works on its own; one that answers with the model's memory depends on luck.

It applies to a shell loop, to Claude Code's /goal, to codex exec inside a script, and to the nine LOOP-R agents.

02Which piece is missing?

Four symptoms everyone has seen, and the piece that fixes each.

1. Says "done" without proof

The agent finishes with a confident summary and the test never ran. The goal existed, but nobody checked it.

Fix: the stop depends on a command that prints the result, and the proof shows up in the output.

Before → after"leave the tests passing" → "python3 -m pytest -q prints 6 passed"

03The five pieces in 15 lines of bash

No model at all: the "action" is one line of shell. The goal is to see the pieces, not the agent.

loop-brinquedo.sh
#!/usr/bin/env bash
# Toy loop: the 5 pieces with no model at all. The "agent" is one line of shell.
set -u
OBJETIVO=5                         # 1. goal: saida.txt with 5 lines
ESTADO=estado.txt                  # 2. state: lives in a file, not in memory
MAX_CICLOS=8                       # 5. stop: cap on cycles
touch saida.txt
ciclo=$(cat "$ESTADO" 2>/dev/null || echo 0)
while true; do
  feitas=$(wc -l < saida.txt)      # 4. check: one command, before acting
  echo "ciclo $ciclo: $feitas de $OBJETIVO linhas"
  if [ "$feitas" -ge "$OBJETIVO" ]; then echo "PRONTO no ciclo $ciclo"; exit 0; fi
  if [ "$ciclo" -ge "$MAX_CICLOS" ]; then echo "PARADO: teto de $MAX_CICLOS ciclos"; exit 2; fi
  ciclo=$((ciclo + 1)); echo "$ciclo" > "$ESTADO"
  echo "item $ciclo" >> saida.txt  # 3. action
done
Terminal · run in an empty folder
mkdir -p ~/ensaio-loop/pecas && cd ~/ensaio-loop/pecas
# paste the script above into loop-brinquedo.sh and run:
bash loop-brinquedo.sh; echo "rc=$?"
bash loop-brinquedo.sh; echo "rc=$?"   # again: it's already done, so it doesn't act
Example from the repository
$ bash loop-brinquedo.sh; echo "rc=$?"
ciclo 0: 0 de 5 linhas
ciclo 1: 1 de 5 linhas
ciclo 2: 2 de 5 linhas
ciclo 3: 3 de 5 linhas
ciclo 4: 4 de 5 linhas
ciclo 5: 5 de 5 linhas
PRONTO no ciclo 5
rc=0

$ bash loop-brinquedo.sh; echo "rc=$?"
ciclo 5: 5 de 5 linhas
PRONTO no ciclo 5
rc=0

The second run adds nothing: the state is in the file and the check comes before the action.

04Where each piece is explored in depth

This course gives the anatomy; the other five go deeper into one piece each. Verification: course 1 (separate evaluator, frozen tests, reward hacking). State and goal in a run that lasts hours: course 2 (the seven-file kit and the goal.md).

Action split across several agents: course 3. Limits on what the agent can do: course 4. And the loop that improves its own process, with promotion and rollback: course 5.

05Practice

On your machine. Only move on to the next one when the "done when" is true.

E1 · The toy loop stops on the check

10 min

Create ~/ensaio-loop/pecas, paste in loop-brinquedo.sh and run it twice in a row.

Done when: The first run ends with PRONTO no ciclo 5 and rc=0; the second prints only ciclo 5: 5 de 5 linhas and PRONTO no ciclo 5, and wc -l < saida.txt → 5.

E2 · The loop stops on the cap

5 min

Delete saida.txt and estado.txt, change OBJETIVO=5 to OBJETIVO=20 and run it again.

Done when: The last line is PARADO: teto de 8 ciclos and echo $? right after → 2.

E3 · Diagnosis of a loop of your own

15 min

Take the last long request you made to Claude Code or Codex. In a file named pecas.md, write one line per piece, starting with objetivo:, estado:, ação:, verificação: and parada:, saying what filled that role, or faltou. Write the markers exactly like this, in Portuguese: the "done when" grep looks for them.

Done when: grep -ciE '^(objetivo|estado|ação|verificação|parada):' pecas.md → 5, and at least one line says "faltou" or points to a real command.

06Lesson quiz

Question 1 of 4

The agent finished by saying "I implemented it and everything works," but no test shows up in the output. Which piece was missing?competency

07In three sentences

Five piecesGoal, state, action, verification and stop: every loop that works has all five.
Command, not opinionWhat decides that a step worked is a command's output; what decides to stop is the test or a cap.
Two exitsDone by verification or stopped by the cap, with a different exit code for each.

Going further

Next lesson