Senior software engineer. Payments infrastructure in Go, and essays.

Reflection Pattern of Agent

— 2 min read

Reflection is an agentic design pattern in which a model (or another reviewer) critiques an initial output and uses that feedback to generate an improved version. The reflection loop continues until the output satisfies predefined criteria or a maximum number of iterations is reached. LLMs rarely produce the best answer on the first attempt. Reflection gives an opportunity to identify the mistakes and gaps before the revised response.

We can understand this in two ways.

Objective feedback

In objective evaluation, an external system verifies whether the generated output is correct.

Coding agent

Let's suppose we are building a Python code generator that generates functions for a given task

Coding agent loop: an LLM generates code v1, a runner runs and verifies it, the LLM generates code v2 from the feedback, and the runner checks again

Just see the image above that shows a coding agent that generates code v1 in the first pass; then the runner runs the code. If any error comes, then with the error and code v1 passes to the LLM again, based on the feedback, produce a result again, and so on until the code runs or to max iterations.

Subjective feedback example

An agent that writes an essay

Essay agent: one LLM writes the first draft, then an LLM reviews it and writes a new version

In this case, the reviewer or critic is an LLM, not any external tool; the input reviewer could be the same model(self), some other model, or a human.

Eval methods to evaluate the reflections

Objective Evals

We prepare a benchmark dataset containing inputs and expected outputs; if that passes, we consider the agent is ready.

e.g.

  1. Above code-based example
  2. Build a dataset and evaluate on that (SQL): We create an agent that generates the SQL queries.

Subjective Evals

Since quality is subjective, we provide the evaluator with clear evaluation criteria or a rubric so that it reviews the response consistently.

e.g.

  1. Use an LLM as a guide to evaluate
  2. Rubric-based, where you would define the rules step by step

Ref: Andrew Ng course

Also on Substack: read or comment there. Get new essays by email — subscribe on Substack.