Build an AI loop that checks itself in 25 lines of Python

Mervin Praison shows 'loop engineering': an AI guesses a secret number, your code checks each guess and sends back higher/lower hints, and a round limit stops the run. Try, check, feedback, stop, in 25 lines with PraisonAI and an OpenAI key.

Mervin Praison · YouTube · published Nov 7, 2026 · Software & DevOps

Build an AI loop that checks itself in 25 lines of Python — video by Mervin Praison▶ Watch on YouTube
Prompt
What does this AI loop check on every round, and when does it stop? Explain the try, check, feedback and stop parts, and make sure there is a maximum number of rounds.

Visual breakdown

Loop engineering means putting an AI in a loop that tries, gets checked by your code, receives feedback and stops. In 25 lines of Python an AI finds a secret number in 5 rounds from higher/lower hints, and one round-limit line prevents an endless bill.

The problem: A single AI guess is often wrong, and typing every follow-up message yourself doesn't scale.

How the guessing loop works

  1. Setuppip install PraisonAI, Python 3.10 to 3.14, OpenAI API key with a few dollars of credit
  2. Secret and limitYour code picks a secret number from 1 to 100 and sets a 10-round maximum
  3. TryThe agent guesses a number; it never sees the secret
  4. CheckCode keeps only the digits and compares the guess with the secret
    Dropped: Wrong guesses
  5. FeedbackSends back 'go higher' or 'go lower'; the agent remembers earlier guesses
  6. StopBreak when correct, or end when the rounds run out
  7. ResultCorrect on round 5 (38) in the demo
SetupYour codeAI agentResult

Every loop has four parts

  1. Try: the AI makes an attempt
  2. Check: your code verifies the answer
  3. Feedback: a short note on what to fix
  4. Stop: when it's right or the tries are used up

By the numbers

25lines of code
5rounds to find 38
10max rounds

Build it yourself

The 25-line program is in the video description; the steps below follow the video.

  1. Open the terminal and run pip install praisonaiagents
  2. Set your OpenAI API key (on a Mac: export OPENAI_API_KEY with your key in quotes)
  3. Save the program as its own file and set the limit on line 4 to 1 round to see a single try fail
  4. Change the limit to 10 rounds and run it again: the loop narrows the guess and stops itself
  5. Set the secret to 150 and the limit to 5 to watch it run out of rounds

Afterwards, ask any AI app: what does it loop check and when does it stop?

Lessons from the demo

  • A loop without a stop rule is a bill without an end.
  • The AI never sees the secret; your code's hints lead it there.
  • Hide the AI's own messages so you only see what your code shows.
  • Microsoft researchers tested AI loops and the best setup finished only a quarter of their task, so loop design still matters.

Distilled from the video’s captions

How to try it

  1. Open PraisonAI.
  2. Copy the prompt and fill in the [placeholders].
  3. Watch Mervin Praison's video for the full walkthrough and setup.

Related use cases

Share