107. Loop Engineering 1 - What a Loop Is
Recognise the four parts every loop has - trigger, body, check, exit - and see the same four inside an AI agent.
By Jacques Botte, founder of Toptronic®. Last updated 17 October 2026.
The lesson
A loop is a body of work that repeats until a condition says stop. That is the whole definition, and it holds for a shell script, a for-loop in C, a printer polling for a job, and an AI agent deciding what to do next. What matters is not the repetition; it is the condition. Strip it out and you do not have a loop, you have a machine that runs until somebody notices.
Every loop has four parts, and naming them is the first skill. The TRIGGER is what starts it: a schedule tick, a new file, a failed test, a message in a queue, or a person pressing a button. The BODY is the work done once per turn. The CHECK is what decides whether to go round again. The EXIT is what ends it, and a real loop has more than one exit - one for success and at least one for giving up.
An AI agent is built from exactly these four. The trigger is a request or a timer. The body is one turn: the model proposes an action, a tool runs it, and the result is appended to the context. The check is the model's own reading of that result, plus any test, linter or validator you attached. The exit is either a stated goal being met or a budget being spent. A prompt is the instruction for the body; loop engineering is the discipline of the other three.
The check is where the money is. A loop without a check repeats whatever seemed plausible, and repeating something plausible is how a machine spends an hour and forty dollars changing a file back and forth. The check must be something that can fail - a test that runs, a schema that validates, a count that matches, a second reader who can say no. A check that always says yes is decoration.
Finally, every loop needs a budget stated before it starts: turns, wall-clock time, tokens, and money. A loop with no budget is not autonomous, it is unattended. The habit to build now, at the beginning, is to write the four parts and the budget down before writing the prompt - four lines on a page, and the loop you build from them will behave the way you described.
Check yourself
Question 1: Which four parts does every loop have?
- Trigger, body, check, exit — correct
- Prompt, model, token, temperature
- Start, middle, end, report
- Input, output, log, archive
Answer: Trigger, body, check, exit
A trigger starts it, a body does the work once per turn, a check decides whether to repeat, and an exit ends it - usually one exit for success and one for giving up.
Question 2: In an AI agent, what plays the part of the loop's check?
- The length of the prompt
- A test, validator or second reading that can actually fail — correct
- The temperature setting
- The number of tools installed
Answer: A test, validator or second reading that can actually fail
The check has to be able to say no. A test that runs, a schema that validates or a reader who can refuse is a check; anything that always agrees is decoration.
Question 3: Why must a budget be written down before a loop starts?
- Because the vendor requires it
- Because a loop with no budget is unattended rather than autonomous — correct
- Because budgets make the model faster
- Because budgets replace the check
Answer: Because a loop with no budget is unattended rather than autonomous
Turns, time, tokens and money are the four limits. Without them the loop does not stop when the work is done - it stops when somebody notices.
← Previous lesson · All 134 lessons · Next lesson →
The full course — 134 lessons and 402 quiz questions — ships inside the app. Get TPEE to study it offline.
This lesson is the written summary of a course that ships inside the TPEE prompt editor for Windows - thirteen sections, a 0-100 score before you send anything, and it works with no network connection. The three one-time prices, and a 14-day free trial.