Self-Correction and Reflective Prompting
Implement a reflection step where the agent reviews its own output against the original goal, identifies gaps or errors, and generates a corrected plan before retrying.
What Is Reflective Prompting?
Reflective prompting is a technique where the agent is asked to evaluate its own output before finalizing it. Instead of producing an answer and stopping, the agent reviews its response against the original goal, identifies gaps or errors, and generates a corrected version. This mimics how humans proofread their own work and significantly improves output quality on complex tasks without requiring a separate critic model.
The Reflect-and-Revise Loop
The basic reflective loop has three steps: Generate an initial response, Critique that response against clear criteria, and Revise based on the critique. This loop can run one or more times. Each iteration improves the response until either the critique declares it satisfactory or a maximum revision count is reached. The critique step is itself an LLM call with a specialized reflection prompt.
async def reflect_and_revise(task: str, max_rounds: int = 2) -> str:
response = await generate_initial(task)
for round_num in range(max_rounds):
critique = await critique_response(task, response)
if critique.is_satisfactory:
break
response = await revise_response(task, response, critique.feedback)
return responseAll lessons in this course
- Classifying Agent Failure Modes
- Self-Correction and Reflective Prompting
- Checkpointing and Task Resumption
- Human-in-the-Loop Escalation