cd /news/ai-agents/loop-engineering-build-an-ai-that-ch… · home › topics › ai-agents › article
[ARTICLE · art-165871] src=mer.vin ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

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.

by read4 min views2 publishedNov 7, 2026
Loop Engineering: Build an AI That Checks Itself in 25 Lines
Image: Mer (auto-discovered)

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.

%%{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.

Please ⭐ star the repohttps://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 (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, usepip3 andpython3 ).

Tell your computer your key (this lasts for this terminal window only):

export OPENAI_API_KEY="your-key-here"

$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.

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.

── more in #ai-agents 4 stories · sorted by recency
── more on @praisonai 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/loop-engineering-bui…] indexed:0 read:4min 2026-11-07 · —