Loop Engineering: Build an AI That Checks Itself in 25 Lines A tutorial published by PraisonAI creator Mervin Praison shows how to build a self-checking AI loop in 25 lines of open-source Python using the PraisonAI library and OpenAI's gpt-4o-mini model, wrapping a number-guessing agent in a try/check/feedback/stop cycle capped at 10 rounds. The guide, which cites Addy Osmani's 7 June 2026 "Loop Engineering" post for the term, reports that a full run cost well under one cent in API requests. Give an AI one guess and it can be wrong. Put the same AI in a loop that checks each answer and sends back a hint, and it keeps going until it is right, or until the limit you set. This short guide builds that loop in 25 lines of free, open-source Python with PraisonAI https://github.com/MervinPraison/PraisonAI . %%{init: {"theme": "base", "themeVariables": {"background": "transparent", "lineColor": " 000000"}}}%% graph TD T Try: the AI guesses a number -- C{Check: is it the secret?} C -- |yes| D Stop: found it C -- |no| F Feedback: go higher or go lower F -- L{Rounds left?} L -- |yes| T L -- |no| S Stop: out of rounds classDef agent fill: 8B0000,color: fff classDef hook fill: 189AB4,color: fff classDef decision fill: 444,color: fff class T,D,S agent class F hook class C,L decision Prefer to watch? The whole build is in a six-minute video: Loop Engineering Made EASY: AI That Checks Itself https://youtu.be/UO2teJGQZkk . Please ⭐ star the repo https://github.com/MervinPraison/PraisonAI https://github.com/MervinPraison/PraisonAI What is loop engineering? You already own a loop: the thermostat. It checks the room, turns the heating on if it is too cold, checks again a few minutes later, and stops when it is warm enough. It never needs you to watch it. An AI loop works the same way, with four parts: try the AI answers , check your code tests the answer , feedback your code tells the AI what to fix and stop when it is right, or out of tries . Loop engineering is designing that loop, instead of typing a new message to the AI each time. The name comes from Addy Osmani’s post Loop Engineering https://addyosmani.com/blog/loop-engineering/ 7 June 2026 . What you need - Python 3.10 to 3.14 free, from python.org . On Windows, tick “Add Python to PATH” in the installer. - An OpenAI API key from platform.openai.com with a few dollars of credit. Each round is one paid request; a whole run cost me well under a cent. - PraisonAI: pip install praisonai on a Mac without a virtual environment, use pip3 and python3 . Tell your computer your key this lasts for this terminal window only : Mac export OPENAI API KEY="your-key-here" Windows PowerShell $env:OPENAI API KEY="your-key-here" The code: guess loop.py Copy this into a plain-text file called guess loop.py . Keep the spaces at the start of the lines. python from praisonai import Agent SECRET = 38 the number the AI must find MAX ROUNDS = 10 the stop: never more than this agent = Agent name="Guesser", instructions="Guess my secret number from 1 to 100. Reply with one number only.", model="gpt-4o-mini", output="silent", task = "Guess the number." for round no in range 1, MAX ROUNDS + 1 : reply = agent.start task 1. try guess = int "".join c for c in reply if c.isdigit or 0 if guess == SECRET: 2. check print f"Round {round no}: {guess} CHECK: correct " print f"DONE: found it in round {round no}" break 4. stop: it worked hint = "higher" if guess in range SECRET else "lower" print f"Round {round no}: {guess} CHECK: wrong, go {hint}" task = f"{guess} is wrong. My number is {hint}. Guess again." 3. feedback else: print f"STOPPED: out of rounds limit {MAX ROUNDS} " 4. stop: out of rounds | Lines | Part | What it does | |---|---|---| | 3–4 | Setup | The secret number, and the limit: never more than 10 rounds | | 6–12 | Setup | The AI and its instructions: guess a number from 1 to 100, reply with one number only | | 15–16 | 1. Try | Ask the AI, then keep only the digits of its reply | | 17–20 | 2. Check + 4. Stop | If the guess is the secret, print it and stop | | 21–23 | 3. Feedback | Work out “higher” or “lower” and send it back as the next message | | 24–25 | 4. Stop | Out of rounds: stop, so it can never run forever | Run it python guess loop.py My run 5 November 2026, praisonai 4.7.23, gpt-4o-mini : Round 1: 57 CHECK: wrong, go lower Round 2: 34 CHECK: wrong, go higher Round 3: 45 CHECK: wrong, go lower Round 4: 40 CHECK: wrong, go lower Round 5: 38 CHECK: correct DONE: found it in round 5 With one round MAX ROUNDS = 1 it was wrong every time. With 10 rounds it found 38 in 4 to 9 rounds over 8 runs. The AI guesses differently each time, but it remembers its earlier guesses, so each hint narrows it down. When the answer can’t be found Change line 3 to SECRET = 150 and line 4 to MAX ROUNDS = 5 . The AI was told 1 to 100, so it can never win: Round 1: 50 CHECK: wrong, go higher Round 2: 75 CHECK: wrong, go higher Round 3: 87 CHECK: wrong, go higher Round 4: 93 CHECK: wrong, go higher Round 5: 96 CHECK: wrong, go higher STOPPED: out of rounds limit 5 It stopped at round 5 in every run. That limit is the line that stops the bill. A loop without a stop rule is a bill without an end. from praisonai import Agent and from praisonaiagents import Agent both work. The Windows commands were not tested on Windows. Related on mer.vin - Harness Engineering: Build Your First AI Harness in 26 Lines https://mer.vin/2026/11/harness-engineering-build-your-first-ai-harness-in-26-lines/ - Loop Engineering: Design Coding-Agent Systems Instead of Prompting Every Turn https://mer.vin/2026/06/loop-engineering-design-coding-agent-systems-instead-of-prompting-every-turn/ - Karpathy Loop Engineering: Verifiers, State, and Bilevel Autoresearch https://mer.vin/2026/07/karpathy-loop-engineering-verifiers-state-and-bilevel-autoresearch-explained/