Checking and Refining the Agent's Reasoning › Iterative Refinement

Michael BrenndoerferJune 19, 20252 min read
AI Agent HandbookAll chapters
Part 4 of 15Reasoning - Teaching the Agent to Think
Chapter 11Checking and Refining the Agent's Reasoning › Iterative Refinement
Reading section 7 of 14 · Iterative Refinement · 2 minSection 7 of 14 · 2 min

Book contents

Section 7 of 14 · 2 min read

Checking and Refining the Agent's Reasoning › Iterative RefinementOne section · Sections in this chapter7 / 14 ↓

Sometimes one verification pass isn't enough. You can create a refinement loop where the agent repeatedly improves its answer.

Here's a pattern for iterative refinement:

In[7]:
Code
from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))


def refine_answer(client, problem, max_iterations=3):
    """Iteratively refine an answer through multiple verification passes."""

    # Initial solution
    messages = [{"role": "user", "content": f"{problem}\n\nSolve this step by step."}]

    response = client.chat.completions.create(model="gpt-5", messages=messages)

    current_answer = response.choices[0].message.content
    print(f"Initial answer:\n{current_answer}\n")

    # Refinement loop
    for i in range(max_iterations):
        messages.append({"role": "assistant", "content": current_answer})
        messages.append(
            {
                "role": "user",
                "content": """Review your answer. Are there any errors or improvements you can make? 
            If your answer is correct and complete, say 'VERIFIED'. 
            Otherwise, provide an improved version.""",
            }
        )

        response = client.chat.completions.create(model="gpt-5", messages=messages)

        refinement = response.choices[0].message.content
        print(f"Refinement {i+1}:\n{refinement}\n")

        if "VERIFIED" in refinement.upper():
            print("Answer verified!")
            break

        current_answer = refinement

    return current_answer

This pattern lets the agent improve its answer over multiple passes, catching progressively subtler issues.

For intermediate readers: This iterative refinement pattern is related to several advanced techniques in AI research. Self-consistency checking (running the same problem multiple times and comparing results) and self-critique (having the model evaluate its own outputs) are active research areas. The key insight is that language models can often recognize errors in reasoning when prompted appropriately, even if they made those errors initially. This works because the verification task is different from the generation task. During generation, the model is sampling from a probability distribution. During verification, it's evaluating a concrete proposal, which can activate different reasoning patterns. However, this isn't foolproof. Models can still miss errors or even introduce new ones during refinement. In production systems, you might combine self-verification with external checks (like running code, querying databases, or using specialized verification models).