{
  "id": 2767679,
  "title": "Building a Self-Correcting AI Agent with Reflection Loops in Python",
  "url": "https://urgent.news/2026/08/23/building-a-self-correcting-ai-agent-with-reflection-loops-in-python",
  "topic": "ai",
  "section": "AI",
  "published": "2026-08-23T10:03:11.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/ayinedjimi-consultants/building-a-self-correcting-ai-agent-with-reflection-loops-in-python-hda"
  },
  "original_language": "en",
  "account": "A reflection loop is a control flow pattern used to automate tasks performed by language models. The process involves generating an output, critiquing it, and then deciding whether to retry if the output fails to meet a predefined quality bar. The simplest form of a reflection loop comprises a two-step cycle: generate and critique.\n\nThe critique step typically involves a second LLM call with a distinct prompt. However, it can also be a deterministic check - for instance, a JSON parser, a unit test runner, or a schema validator. The underlying principle is that critiquing is easier than generating. An LLM often overlooks edge cases in its first pass but can correctly identify them when asked \"what is wrong with this output?\". This asymmetry is what makes the reflection loop pattern effective.\n\nThe following Python class encapsulates an LLM call within a reflection loop. It takes three parameters during initialization: the task prompt, the critique prompt, and a validator function. The validator function accepts a string as input and returns a tuple containing a boolean value indicating whether the output passed the quality bar and a critique string.\n\nTo execute the reflection loop, the run method is called with a user input. The method maintains a history of attempts comprised of dictionaries detailing the attempt number, the output of the task, the critique, and whether it passed the quality bar. The method iterates until it reaches a maximum number of iterations, generating a task output and validating it. If the output passes, the method returns a dictionary containing a success flag, the output, the number of iterations, and the history. If the output fails, the method generates a critique using the critique prompt and appends it to the history.",
  "summary": "Language models produce wrong answers. Not occasionally — regularly. When you deploy an LLM to automate tasks, you need a way to catch and fix those errors without human intervention. Reflection loops are one practical answer: the model checks its own output, flags problems, and retries until it meets a quality bar you define in code. What Is a Reflection Loop? A reflection loop is a control flow…",
  "key_points": [
    "Reflection loop automates tasks in language models",
    "Two-step cycle: generate and critique",
    "Critique step uses second LLM call or deterministic check"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}