Checking and Refining the Agent's Reasoning › Building Verification Into Your Agent

Michael BrenndoerferJune 19, 20251 min read
AI Agent HandbookAll chapters
Part 4 of 15Reasoning - Teaching the Agent to Think
Chapter 11Checking and Refining the Agent's Reasoning › Building Verification Into Your Agent
Reading section 12 of 14 · Building Verification Into Your Agent · 1 minSection 12 of 14 · 1 min

Book contents

Section 12 of 14 · 1 min read

Checking and Refining the Agent's Reasoning › Building Verification Into Your AgentOne section · Sections in this chapter12 / 14 ↓

As you develop your personal assistant, consider building verification into its core workflow for critical tasks. Here's a simple pattern:

In[10]:
Code
from openai import OpenAI

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


class PersonalAssistant:
    def __init__(self, client):
        self.client = client

    def solve_with_verification(self, problem, verify=True):
        """Solve a problem with optional verification."""

        # Initial solution
        solution = self._solve(problem)

        if not verify:
            return solution

        # Verification step
        verified = self._verify(problem, solution)

        return verified

    def _solve(self, problem):
        """Generate initial solution."""
        response = self.client.chat.completions.create(
            model="gpt-5",
            messages=[{"role": "user", "content": f"{problem}\n\nSolve step by step."}],
        )
        return response.choices[0].message.content

    def _verify(self, problem, solution):
        """Verify and potentially refine the solution."""
        response = self.client.chat.completions.create(
            model="gpt-5",
            messages=[
                {
                    "role": "user",
                    "content": f"""Problem: {problem}

Solution: {solution}

Verify this solution. If correct, return it as-is. 
If you find errors, return a corrected version.""",
                }
            ],
        )
        return response.choices[0].message.content

This pattern makes verification easy to enable or disable based on the task's importance.