Loop Engineering Made EASY: AI That Checks Itself
Loop engineering, explained simply, then built and run in 25 lines of free, open-source Python. Same AI, same game: guess my secret number from 1 to 100. With one guess, it got it wrong. Inside a loop that checks every guess and gives a hint, it found the number in round 5. Please ⭐ star the repo https://github.com/MervinPraison/PraisonAI The code and a short written guide: https://mer.vin/2026/11/loop-engineering-build-an-ai-that-checks-itself-in-25-lines/ What you'll learn - What a loop is, using the thermostat in your home, and the game drawn as a picture - The four parts of every loop: try, check, feedback and stop - Where the name "loop engineering" came from - How to build one with PraisonAI, with every line explained - Three real runs, including one the AI can never win - The one line that stops the bill Try it yourself 1. Install Python 3.10 to 3.14 (free, from python.org). On Windows, tick "Add Python to PATH" in the installer. 2. Get an OpenAI API key at platform.openai.com and add a few dollars of credit. 3. Open the Terminal app (Mac) or PowerShell (Windows) and install PraisonAI: pip install praisonai (On a Mac without a virtual environment, use pip3 and python3.) 4. 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" 5. Copy the code below into a plain-text file called guess_loop.py. Keep the spaces at the start of the lines. 6. Run it: python guess_loop.py 7. Change line 3 (the secret) and line 4 (the limit), and run it again. The code (guess_loop.py) 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 What you should see - One line per round: the guess, then "CHECK: wrong, go higher" or "go lower". - "DONE: found it in round N" when it is right, or "STOPPED: out of rounds" at the limit. My tests (5 November 2026) - pip install praisonai gave praisonai 4.7.23. Model: OpenAI gpt-4o-mini. - One guess: wrong every time. With 10 rounds: it found 38 in 4 to 9 rounds (8 runs). The agent remembers its earlier guesses, so each hint narrows it down. - Secret 150 with a limit of 5: stopped at round 5 in every run. Each round is one paid request; a whole run cost well under a cent. - "from praisonai import Agent" and "from praisonaiagents import Agent" both work. - The Windows commands were not tested on Windows. Timestamps 0:00 Same AI: one guess vs a loop 0:25 What you'll learn 0:54 What a loop is (the thermostat, the game) 1:59 It has a name: loop engineering 2:34 Set it up: pip install praisonai 3:03 The code, line by line 4:25 Run it three ways 5:37 What it means for you Sources - Addy Osmani, "Loop Engineering" (7 Jun 2026): https://addyosmani.com/blog/loop-engineering/ - Claude Code docs, "How Claude Code works": https://code.claude.com/docs/en/how-claude-code-works - LoopsBench (Jul 2026): https://arxiv.org/abs/2608.00267 - PraisonAI: https://github.com/MervinPraison/PraisonAI · docs: https://docs.praison.ai - My previous video, Harness Engineering: https://youtu.be/d0gQsyPi4-g Disclosure: I built PraisonAI. What would you build a loop for? Tell me in the comments. #LoopEngineering #AIAgents #PraisonAI