{"slug": "loop-engineering-build-an-ai-that-checks-itself-in-25-lines", "title": "Loop Engineering: Build an AI That Checks Itself in 25 Lines", "summary": "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.", "body_md": "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).\n\n```\n%%{init: {\"theme\": \"base\", \"themeVariables\": {\"background\": \"transparent\", \"lineColor\": \"#000000\"}}}%%\ngraph TD\n    T[Try: the AI guesses a number] --> C{Check: is it the secret?}\n    C -->|yes| D[Stop: found it]\n    C -->|no| F[Feedback: go higher or go lower]\n    F --> L{Rounds left?}\n    L -->|yes| T\n    L -->|no| S[Stop: out of rounds]\n\n    classDef agent fill:#8B0000,color:#fff\n    classDef hook fill:#189AB4,color:#fff\n    classDef decision fill:#444,color:#fff\n    class T,D,S agent\n    class F hook\n    class C,L decision\n```\n\nPrefer to watch? The whole build is in a six-minute video: [Loop Engineering Made EASY: AI That Checks Itself](https://youtu.be/UO2teJGQZkk).\n\nPlease ⭐ star the repo[https://github.com/MervinPraison/PraisonAI](https://github.com/MervinPraison/PraisonAI)\n\n## What is loop engineering?\n\nYou 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.\n\nAn 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).\n\n## What you need\n\n- Python 3.10 to 3.14 (free, from python.org). On Windows, tick “Add Python to PATH” in the installer.\n- 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.\n- PraisonAI: `pip install praisonai` (on a Mac without a virtual environment, use`pip3` and`python3` ).\n\nTell your computer your key (this lasts for this terminal window only):\n\n```\n# Mac\nexport OPENAI_API_KEY=\"your-key-here\"\n\n# Windows PowerShell\n$env:OPENAI_API_KEY=\"your-key-here\"\n```\n\n## The code: guess_loop.py\n\nCopy this into a plain-text file called `guess_loop.py`. Keep the spaces at the start of the lines.\n\n``` python\nfrom praisonai import Agent\n\nSECRET = 38          # the number the AI must find\nMAX_ROUNDS = 10      # the stop: never more than this\n\nagent = Agent(\n    name=\"Guesser\",\n    instructions=\"Guess my secret number from 1 to 100. Reply with one number only.\",\n    model=\"gpt-4o-mini\",\n    output=\"silent\",\n)\ntask = \"Guess the number.\"\n\nfor round_no in range(1, MAX_ROUNDS + 1):\n    reply = agent.start(task)                                  # 1. try\n    guess = int(\"\".join(c for c in reply if c.isdigit()) or 0)\n    if guess == SECRET:                                        # 2. check\n        print(f\"Round {round_no}: {guess}   CHECK: correct!\")\n        print(f\"DONE: found it in round {round_no}\")\n        break                                                  # 4. stop: it worked\n    hint = \"higher\" if guess in range(SECRET) else \"lower\"\n    print(f\"Round {round_no}: {guess}   CHECK: wrong, go {hint}\")\n    task = f\"{guess} is wrong. My number is {hint}. Guess again.\"   # 3. feedback\nelse:\n    print(f\"STOPPED: out of rounds (limit {MAX_ROUNDS})\")      # 4. stop: out of rounds\n```\n\n| Lines | Part | What it does | \n|---|---|---|\n| 3–4 | Setup | The secret number, and the limit: never more than 10 rounds | \n| 6–12 | Setup | The AI and its instructions: guess a number from 1 to 100, reply with one number only | \n| 15–16 | 1. Try | Ask the AI, then keep only the digits of its reply | \n| 17–20 | 2. Check + 4. Stop | If the guess is the secret, print it and stop | \n| 21–23 | 3. Feedback | Work out “higher” or “lower” and send it back as the next message | \n| 24–25 | 4. Stop | Out of rounds: stop, so it can never run forever | \n\n## Run it\n\n```\npython guess_loop.py\n```\n\nMy run (5 November 2026, praisonai 4.7.23, gpt-4o-mini):\n\n```\nRound 1: 57   CHECK: wrong, go lower\nRound 2: 34   CHECK: wrong, go higher\nRound 3: 45   CHECK: wrong, go lower\nRound 4: 40   CHECK: wrong, go lower\nRound 5: 38   CHECK: correct!\nDONE: found it in round 5\n```\n\nWith 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.\n\n## When the answer can’t be found\n\nChange line 3 to `SECRET = 150` and line 4 to `MAX_ROUNDS = 5`. The AI was told 1 to 100, so it can never win:\n\n```\nRound 1: 50   CHECK: wrong, go higher\nRound 2: 75   CHECK: wrong, go higher\nRound 3: 87   CHECK: wrong, go higher\nRound 4: 93   CHECK: wrong, go higher\nRound 5: 96   CHECK: wrong, go higher\nSTOPPED: out of rounds (limit 5)\n```\n\nIt stopped at round 5 in every run. That limit is the line that stops the bill.\n\n*A loop without a stop rule is a bill without an end.*\n\n`from praisonai import Agent` and `from praisonaiagents import Agent` both work. The Windows commands were not tested on Windows.\n\n## Related on mer.vin\n\n- [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/)\n- [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/)\n- [Karpathy Loop Engineering: Verifiers, State, and Bilevel Autoresearch](https://mer.vin/2026/07/karpathy-loop-engineering-verifiers-state-and-bilevel-autoresearch-explained/)", "url": "https://wpnews.pro/news/loop-engineering-build-an-ai-that-checks-itself-in-25-lines", "canonical_source": "https://mer.vin/2026/11/loop-engineering-build-an-ai-that-checks-itself-in-25-lines/", "published_at": "2026-11-07 08:48:04+00:00", "updated_at": "2026-11-09 01:57:36.391466+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "large-language-models", "artificial-intelligence"], "entities": ["PraisonAI", "Mervin Praison", "Addy Osmani", "OpenAI", "gpt-4o-mini", "Python"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/loop-engineering-build-an-ai-that-checks-itself-in-25-lines", "markdown": "https://wpnews.pro/news/loop-engineering-build-an-ai-that-checks-itself-in-25-lines.md", "text": "https://wpnews.pro/news/loop-engineering-build-an-ai-that-checks-itself-in-25-lines.txt", "jsonld": "https://wpnews.pro/news/loop-engineering-build-an-ai-that-checks-itself-in-25-lines.jsonld"}}