Concepts / Reflection and Self-Correction in Agents

Reflection and Self-Correction in Agents

The agent first produces a plan, the plan is checked, and only a plan that passes the check is executed.

  • Programming

Why the Order of Steps Matters

When an agent receives a task, it rarely has one obvious way forward. Many different sequences of steps might reach the same answer, and some of those sequences waste far less effort than others. Planning is the stage where the agent decides, before it touches any tool, which steps it intends to take and in what order.

This article follows that planning stage from start to finish. You will see how a task gets broken into parts, why a plan should be checked before it runs, and how the agent judges whether its work actually succeeded. Keep one question in mind as you read: if the agent has to answer a question that combines several conditions, what should it do first? The answer shapes everything else here.

Two Orders for One Query

What do you think happens?

Consider the query: how many companies without revenue have raised at least one billion dollars? Two different orders can answer it. Which statement best describes them?

  • Both orders are wrong

  • Only the order that starts with the small group is correct

  • Both orders are correct, but they create different amounts of work along the way

  • Both orders create exactly the same amount of work

Reveal answer
Answer:

Both orders are correct, but they create different amounts of work along the way

Both produce the right count. One approach starts with the large group of companies that have no revenue and narrows it by how much money they raised. The other starts with the small group that raised at least one billion dollars and narrows it by revenue. The difference lies in how much work each order creates.

Both approaches end with the same count, so the question is not which answer is right. The lesson is that correctness is only the first requirement. Among plans that work, an agent should prefer the one that narrows the problem early, because every later step pays the cost of however many candidates survive the earlier ones.

Worked example

Tracing the Candidate Count in Each Order

Compare the two orders for the query about companies without revenue that raised at least one billion dollars. Decide which order leaves fewer candidates for the later steps to handle.

  1. 1
    Start with the broad group

    Approach A begins with the large set of companies that have no revenue. This set is big, so every later filter has to process many candidates.

  2. 2
    Start with the narrow group

    Approach B begins with the small set of companies that raised at least one billion dollars. This set is small, so the later revenue filter only has a few candidates to check.

  3. 3
    Compare the work

    Both orders end at the same count. Approach B reaches that count having handled far fewer candidates along the way, which is the efficiency the plan should prefer.

Result

Both decompositions are correct, but the one that narrows the problem early creates less total work. An agent choosing between correct plans should prefer the more efficient one.

Decomposition Is Plan Generation

Breaking a complex task into simpler, manageable actions is called task decomposition. The result of that breakdown is a plan, which is a sequence of actions the agent can carry out one at a time. The same process goes by a second name, plan generation. When you hear either term, think of the same act: turning one large goal into a list of smaller moves that together reach it.

Key point

Task decomposition and plan generation name the same activity. The output of that activity is a plan, which is an ordered sequence of manageable actions.

A useful way to see the structure is as a hierarchy. The overall task sits at the top, and each piece of it becomes a node that can be handled directly. Pieces can be split again until each one is a step the agent can act on. The diagram below shows that shape in general terms.

Notice that a plan does not have to cover the whole task end to end. It can be a small plan for a single subtask. An agent might decompose a large request into several small plans, each handled on its own terms.

Why Planning Is Separated From Execution

A tempting design is to let the model plan and act in a single prompt. You ask it to reason step by step and then carry out those steps immediately. That works for small tasks, but it hides a risk. If the generated plan is poor, the agent may spend a long time and a lot of money on API calls before anyone notices that it is going nowhere.

The remedy is decoupling: planning and execution become separate stages. The agent first produces a plan, the plan is checked, and only a plan that passes the check is executed. In this arrangement, a bad plan can be stopped before any action is taken.

Validation Filters Plans

Plan validation is the act of evaluating a generated plan to decide whether it is valid and worth executing. Two families of checks are described in this material. The first family is heuristics, which are simple rules applied to the plan's structure. The second family is AI judges, where a model is asked to assess whether the plan seems reasonable or how it could be improved.

Reflection Feeds Outcomes Back

Reflection and error correction is the stage where the agent evaluates what happened and corrects what went wrong. It applies in two places: to the generated plan before execution, and to the outcomes after execution. If the outcome shows that the goal has not been reached, the agent identifies the mistake and generates a new plan. The loop repeats until the goal is complete.

Reflection is optional in the sense that an agent can function without it, but it is strongly recommended because it significantly improves performance. Trace the diagram from the candidate plan: a plan that fails the check goes to revision, a plan that executes leads to an outcome check, and an outcome that misses the goal sends the agent back to planning with a corrected plan.

The same reasoning shows up in ordinary prompting. Asking a model to think step by step is asking it to decompose a task, and asking it to verify its own answer is asking it to reflect.

Mistakes Beginners Make

  • Common mistake

    Treating any correct plan as a good plan

    What it looks like

    Two orders for the same query both return the right count, so either one seems acceptable.

    Why it fails

    Correctness is only the first requirement. Each later step pays for however many candidates survive the earlier ones, so a correct but wasteful plan costs more.

    Do this instead

    Among plans that work, prefer the one that narrows the problem early.

  • Common mistake

    Letting the model plan and act in one prompt

    Why it fails

    A poor plan runs for a long time, and the cost in API calls accumulates before anyone notices the agent is going nowhere.

    Do this instead

    Separate planning from execution and validate the plan before any action is taken.

  • Common mistake

    Executing a plan that has not been validated

    Why it fails

    Only plans that pass validation should be executed. Skipping the check removes the point where a bad plan could be stopped.

  • Common mistake

    Thinking reflection only happens after execution

    Why it fails

    Reflection applies both to the generated plan before execution and to the outcomes after execution.

  • Common mistake

    Running an AI judge on every plan before applying cheap heuristic rules, which wastes a model call on plans that could have been rejected for free

Where Humans and Other Agents Fit

Once planning, validation, and execution live in separate components, each component can be treated as an agent. The planner, the validator, and the executor together form a multi-agent system, which is a system of several agents working toward a common goal. The three roles can also work in parallel: the system can produce several candidate plans and ask an evaluator to pick the most promising one. This speeds things up but adds cost, since generating several plans at once uses more resources.

Not every stage has to be automated. Humans can take part in plan generation, plan validation, or plan execution. For risky operations, such as updating a database or merging a code change, the system can require explicit human approval before execution, or leave the operation to a human entirely. The level of automation allowed for each action has to be defined clearly in advance.

Trace the Loop Yourself

MEDIUM

An agent generates a plan with several steps. The plan passes the heuristic rules and the AI judge, and the agent executes it. After execution, the outcome check finds that the goal has not been reached. Trace what the agent does next, naming each stage of the loop in order until it either finishes or repeats.

Hints
  • Start from the outcome check. Ask whether the goal was reached.
  • Recall that reflection evaluates outcomes after execution and generates a revised plan when the goal is not reached.
  • The loop repeats until the goal is complete, so the revised plan must go back through the same checks before it runs.

Key Takeaways

  • Task decomposition breaks a complex task into manageable actions, and the resulting ordered sequence of actions is called a plan. Plan generation is the same process under another name.
  • Among plans that are all correct, an agent should prefer the one that narrows the problem early, because every later step pays the cost of the candidates that survive earlier steps.
  • Separating planning from execution lets validation stop a poor plan before any action is taken. Heuristic rules come first, and AI judges assess plans that survive them.
  • Reflection and error correction apply to the plan before execution and to outcomes after it. If the goal is not reached, the agent identifies the mistake, generates a new plan, and repeats until the goal is complete.

Related concepts

More concepts starting with R