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.
2timeout, MemoryMax and a count of cycles without progress, running
3What always needs a human, even inside the cap
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.
01 · Five caps around the cycle
Click each cap: what it limits and where it already exists in the tools.
Lesson 701 · Five caps around the cycleClick each cap: what it limits and where it already exists in the tools.
No cap was hit and the criterion didn't pass? One more cycle.
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.
Scroll the page: each step below lights up its part of the diagram.
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/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/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/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/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
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.
02 · Three caps stopping real processes
Run it on your machine; none of them uses quota. The memory one needs Linux with a user systemd.
#!/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
Memory only counts when it's actually used. bytearray(300 MB) on its own reserves zeroed pages the system hasn't handed over yet, and the process slips past the cap without dying. That's why the test writes to every page. In a real agent, the process that blows up is the one that actually fills the memory.
03 · What 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.
2. Irreversible or external
Push to production, a message to someone, deleting data. You can't undo it with git revert.
How to fence this in with permissions and hooks is course 4.
3. Something is missing
The agent shouldn't look for a key somewhere else or work around it. It stops and says what's missing.
4. Your decision
Requirements that contradict each other, or a business choice. The agent lays out the options; you're the one who chooses.
04 · Simulated 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.
05 · Practice
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.
06 · Lesson 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
Correct
Turns has not hit yet (50). What catches the agent stuck in the same place is the count of cycles without measured progress. Without it, the remaining 46 cycles spin for nothing.
Not that one
Turns has not hit yet (50). What catches the agent stuck in the same place is the count of cycles without measured progress. Without it, the remaining 46 cycles spin for nothing.Review: Five caps around the cycle
Question 2 of 4
timeout 20m codex exec … finished with exit code 124. What happened?
Correct
124 is the code timeout returns when it kills the command for the deadline. 137 is the code for a process killed by a signal, as with the memory cap.
Not that one
124 is the code timeout returns when it kills the command for the deadline. 137 is the code for a process killed by a signal, as with the memory cap.Review: Three caps stopping real processes
Question 3 of 4
The agent is within all the caps and wants to push to the production branch to finish the task. What should you do?competency
Correct
A cap limits how much the loop spends; it does not limit what it does. Pushing to production, e-mail, deleting data, and paid spending always stop at the human gate.
Not that one
A cap limits how much the loop spends; it does not limit what it does. Pushing to production, e-mail, deleting data, and paid spending always stop at the human gate.Review: What calls for a human even inside the cap
Question 4 of 4
In the simulated conversation, why is "raise MAX_CICLOS to 30" the weak choice?
Correct
Stagnation is information: something is missing. The useful answer is to fix the cause and come back with a new cap, not to buy more spins.