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

Lesson 7 · 17 min

Caps and stopping: turns, time, memory, stagnation and cost

A good criterion guarantees the loop doesn't exit too early; it doesn't guarantee that it exits. An impossible goal, an agent stuck on the same error, a process that eats the machine's memory: only a cap stops it. In this lesson you learn the five caps, watch each one stop a real process, and practice the hardest decision: when the agent asks for just one more try.

Start the lesson
Industrial panel with a timer, a red emergency button, a memory module and a gauge at its limit.

In this lesson you will

  1. 1Turns, time, memory, stagnation and cost
  2. 2timeout, MemoryMax and a count of cycles without progress, running
  3. 3What always needs a human, even inside the cap
  4. 4A simulated conversation with an agent that wants to keep going

Goal: put the five caps on a loop and decide, when facing an agent that insists, between continuing, stopping and calling the human.

01Five caps around the cycle

Click each cap: what it limits and where it already exists in the tools.

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

  1. 1/6

    Turns

    The /goal docs say to include a cap in the condition itself, like "or stop after 20 turns"; and Claude Code already stops the goal if the agent answers several turns in a row without using a tool. In your loop, it's a variable: MAX_CICLOS. The /loop has its own: a recurring task expires in 7 days.

  2. 2/6

    Time

    A cycle that hung (waiting for input, in an infinite loop, on a server that won't start) holds up the whole loop. The coreutils timeout kills the command after the deadline and returns 124, and the loop treats it as a failed cycle.

  3. 3/6

    Memory

    An agent that starts a heavy process can freeze the whole machine. On Linux, systemd-run --user --scope with MemoryMax puts a cap on the process and its children: if it goes over, the kernel kills only it (code 137). This is what the execucao-longa kit uses on every cycle.

  4. 4/6

    Stagnation

    The cap that saves the most. It measures a number each cycle (passing tests, items with passes) and counts how many cycles in a row it hasn't gone up. The execucao-longa kit rule: 3 cycles without measurable progress = stop, record it in failures.md and call you. It's also a sign of degraded context: the same plan repeated without closing, an already-done item reopened.

  5. 5/6

    Cost

    Tokens are quota (on a subscription) or money (on the API). In LOOP-R, the per-cycle cost cap is one of the five required answers on the loop sheet, and the Guardian stops the cycle when it hits that cap; the Meta-agent's first metric is the cost per promoted hypothesis. Without a cap, the loop can cost more than the gain it chases.

  6. 6/6

    Two exit doors

    The good door is the done criterion passing. The safety door is any one of the five caps. A loop that only has the first one depends on the goal being possible and on the agent never getting stuck.

The concept is here; the headless implementation with all the caps together (loop-longrun.sh, flock, systemd) is lesson 6 of course 2.

02Three caps stopping real processes

Run it on your machine; none of them uses quota. The memory one needs Linux with a user systemd.

Terminal · time
timeout 2 sleep 5; echo "rc=$?"
Terminal · memory (Linux, user systemd)
systemd-run --user --scope -q -p MemoryMax=50M -p MemorySwapMax=0 \
  python3 -c "x=bytearray(300*1024*1024); x[::4096]=b'1'*len(x[::4096]); print('alocou')"
echo "rc=$?"
estagnacao.sh · cycles without progress
#!/usr/bin/env bash
# Stagnation cap: N cycles in a row without the number of passing tests going up = stop.
set -u
ESTAGNACAO=3; MAX_CICLOS=10
medir() { cat passando.txt; }          # in the real loop: the fast test, e.g. pytest -q | grep -oE '[0-9]+ passed'
melhor=$(medir); sem_avanco=0
for ((i = 1; i <= MAX_CICLOS; i++)); do
  # agent action here (timeout + MemoryMax around it)
  agora=$(medir)
  if [ "$agora" -gt "$melhor" ]; then melhor=$agora; sem_avanco=0; else sem_avanco=$((sem_avanco + 1)); fi
  echo "ciclo $i: $agora passando (melhor $melhor, sem avanço $sem_avanco)"
  if [ "$sem_avanco" -ge "$ESTAGNACAO" ]; then echo "PARADO: $ESTAGNACAO ciclos sem avanço"; exit 2; fi
done
echo "PARADO: teto de $MAX_CICLOS ciclos"; exit 2
/goal · turn cap in the condition itself
/goal python3 -m pytest -q tests/ prints 48 passed and git diff --stat -- tests/ comes out empty, or stop after 20 turns
Example from the repository
$ timeout 2 sleep 5; echo "rc=$?"
rc=124

$ systemd-run --user --scope -q -p MemoryMax=50M -p MemorySwapMax=0 python3 -c "..."
Killed
rc=137

$ echo 41 > passando.txt; bash estagnacao.sh; echo "rc=$?"
ciclo 1: 41 passando (melhor 41, sem avanço 1)
ciclo 2: 41 passando (melhor 41, sem avanço 2)
ciclo 3: 41 passando (melhor 41, sem avanço 3)
PARADO: 3 ciclos sem avanço
rc=2

03What calls for a human even inside the cap

List from the execucao-longa kit (the STOP AND CALL ME block of the goal prompt). A cap limits how much; a gate limits what.

1. Spending

Any paid call, or one that spends credit outside what was agreed. The loop stops and asks.

04Simulated chat: the agent wants just one more try

Fiction to practice the decision. Pick what you would answer; the comment appears right after.

Agent in a loop (simulated)cycle 9 of 15 · migrating the parser

Choose your answer. The feedback appears right after, and the conversation continues based on your choice.

    05Practice

    On your machine. No exercise uses up quota.

    E1 · Time and memory

    10 min

    Run the first two blocks: the timeout and the memory cap. If you are not on Linux, run only the timeout (on macOS, gtimeout from coreutils).

    Done when: The timeout prints rc=124; the memory one prints Killed and rc=137.

    E2 · Stagnation and progress

    10 min

    Run estagnacao.sh with 41 in passando.txt. Then, in a second window, change the number to 42 while it runs with a sleep 5 inside the loop, and watch the counter reset.

    Done when: No change: PARADO: 3 ciclos sem avanço and rc=2. With the change: a line appears with melhor 42, sem avanço 0.

    E3 · The caps for your next loop

    10 min

    In a tetos.md file, write the value of each cap for a loop of your own, and what will always need you, one line for each: turnos:, tempo:, memória:, estagnação:, custo: and humano:.

    Done when: grep -cE '^(turnos|tempo|memória|estagnação|custo|humano):' tetos.md → 6, and the stagnation line has a number of cycles.

    06Lesson quiz

    Question 1 of 4

    The loop has MAX_CICLOS=50 and a level 3 criterion. The tests have been at 41 of 48 for 4 cycles. Which cap should have stopped it?competency

    07In three sentences

    Five capsTurns, time, memory, stagnation, and cost: each one catches a different way the loop can fail to finish.
    Stagnation first3 cycles without measured progress = stop, log it, and call you; this is not negotiable.
    A cap is not a gateSpending, irreversible or external actions, missing credentials, and business decisions always need a human.

    Going further

    Previous lessonNext lesson