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
Checking and Refining the Agent's Reasoning › Building Verification Into Your AgentOne section · Sections in this chapter12 / 14 ↓
Sections in this chapter14
As you develop your personal assistant, consider building verification into its core workflow for critical tasks. Here's a simple pattern:
In[10]:
Codefrom 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.contentThis pattern makes verification easy to enable or disable based on the task's importance.