All posts
Fundamentals

Putting planning and reflection together

Planning and reflection each help on their own. Together they form a loop, and the loop is the real subject of this chapter.

A plan without reflection is a route nobody checks. Reflection without a plan has nothing concrete to check against. Joined up, they become the rhythm an expert uses: set a direction, follow it, check progress, correct the direction when it is wrong.

The loop

Four moves, and one of them feeds back.

1. PLAN     decompose the task into an ordered set of steps
2. EXECUTE  work the current step with the ReAct loop and its tools
3. REFLECT  pause: is this going well? is the plan still right?
              on track  -> back to EXECUTE, next step
              problem    -> set need_replan, go to 4
4. REPLAN   rewrite the plan to fit what was just learned, then EXECUTE

The straight path is plan, then execute and reflect in a cycle until the tasks are done. The feedback path is what makes it resilient: when reflection finds a problem, it sends the agent back to planning to rewrite the route rather than plough on.

Plan, execute, reflect. On track, continue; problem found, replan. The loop that catches a wrong turn.Plan, execute, reflect. On track, continue; problem found, replan. The loop that catches a wrong turn.

The handshake is one flag

The two tools coordinate through the need_replan flag from the reflection post, and that is the whole connection.

Reflection assesses, and if it decides the current plan no longer fits, it sets need_replan=True. The agent reads that flag and routes to the planning tool instead of the next step. Planning rewrites the plan in light of what reflection just learned. Then execution resumes against the new plan.

# inside the agent loop, after a step
reflection = await self.reflect(context)
if reflection.need_replan:
    await self.replan(context)          # reflection asked; planning answers
# otherwise continue to the next planned step

One boolean turns two separate tools into a control system: reflection is the sensor that notices drift, planning is the actuator that corrects it. Neither has to know how the other works. They meet at the flag.

The two failures each half prevents

Seeing the loop whole shows why you want both halves, not one.

Planning alone prevents the agent from wandering with no direction, but it cannot notice when the plan meets reality and loses. The agent follows a good plan straight into a dead search and keeps going.

Reflection alone prevents blind repetition, but with no plan it is checking against nothing, so "am I on track?" has no track to compare to.

Together, planning gives reflection something concrete to judge, and reflection gives planning the signal to adapt. That is why the loop is more than the sum of the two tools.

Planning onlyReflection onlyBoth, in a loop
Has directionYesNoYes
Notices troubleNoYesYes
Adapts the routeNoNothing to adaptYes, via need_replan
Failure it still hitsFollows a dead planChecks against nothingFar fewer

When to reach for the whole loop

The machinery costs something. Planning and reflection each add model calls, so the loop earns its place only on the tasks that need it: multi-step research, work where a wrong turn is expensive, anything where the route cannot be seen from the start. For a one-step question, it is pure overhead, and the plain ReAct agent is the right tool.

The decision is the one from the start of this series, one level up. There, it was workflow versus agent. Here, it is reactive agent versus planning agent, and the test is the same: can you see the whole route in advance? If yes, you may not even need an agent. If no, and the task is long enough to get lost in, that is when planning and reflection pay for themselves.

Planning gives reflection a track to judge; reflection gives planning the signal to adapt. Neither alone is enough.Planning gives reflection a track to judge; reflection gives planning the signal to adapt. Neither alone is enough.

What to take from this

  • Planning and reflection are two halves of one loop: plan, execute, reflect, and replan when reflection finds a problem.
  • They coordinate through a single flag. Reflection is the sensor that notices drift; planning is the actuator that corrects it; need_replan is where they meet.
  • Reach for the whole loop on long, multi-step tasks where the route is not visible up front. On a short task it is overhead, and plain ReAct wins.

The reasoning layer of the agent is now complete. It can plan a complex task, work it, notice when it goes wrong, and change course, on top of everything it could already do. A reasoning layer that decides its own direction is also a surface worth defending, which is where the security series turns next.