liveBook · Manning

Chapter 7: Planning and reflection for complex tasks

What you will learn in chapter 7 of Build an AI Agent (From Scratch)

Introduces why a reactive agent can struggle with multistep tasks and how planning and reflection address different parts of that problem.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces why a reactive agent can struggle with multistep tasks and how planning and reflection address different parts of that problem.

Key terms: reactive approach, memory systems, planning, reflection

7.1 Giving agents time to think

Introduces planning and reflection as ways to give agents time to think and previews the topics that follow.

7.1.1 The limitations of ReAct

This section explains why ReAct’s adaptable, step-by-step decisions can lose direction on complex tasks.

Key terms: ReAct, tool, max_steps

7.1.2 How human experts work

This section uses researchers and software developers to show why agents should plan complex work and check their progress against that plan.

Key terms: decompose, investigate, adjust their approach, product requirements document (PRD), spec document, implementation plan, planning, reflection

7.1.3 Why time to think matters

This section explains why giving agents time to think can improve their work and how planning and reflection guide later decisions by adding text to the agent’s context.

Key terms: chain-of-thought prompting, intermediate reasoning steps, thinking tokens, planning, reflection, metacognition, context engineering, generation strategy, retrieval, writing

7.2 Planning: Setting direction

Planning gives a complex task direction by breaking it into manageable steps with clear completion criteria.

Key terms: planning, decomposition, completion criteria

7.2.1 When planning is necessary

This section explains when an agent should plan, when it should skip planning, and why that distinction belongs in the planning tool’s description.

Key terms: planning, multiple steps of research, single search, clear procedures, planning tool, description

7.2.2 Implementing the planning tool

This section builds a simple planning tool by choosing how to store and update a plan, formatting task progress, and leaving planning decisions to the LLM.

Key terms: plan, subplans, partial modifications, Task, __str__, pending, in_progress, completed, create_tasks, @tool

7.2.3 Planning-tool example

This example shows an agent creating and updating a plan, then explains how task granularity affects execution time.

Key terms: create_tasks, ToolResult, context, completed, in_progress, checkbox format, granularity, WHEN TO USE, WHEN NOT TO USE, instructions parameter

7.2.4 Extension directions

This section describes ways to extend a simple planning tool for complex tasks while arguing that useful planning questions matter more than added structure.

Key terms: completion criteria, hierarchical structure (task/action), dependency management (deps), tool’s description, dependency graphs, parallel execution

7.3 Reflection: Checking and correcting

This section explains reflection as a pause during an agent’s work to assess progress and guide later decisions.

Key terms: planning, reflection, ReAct agents, context, evaluation and direction so far

7.3.1 When reflection is necessary

This section identifies useful checkpoints for agent reflection, explains when reflection adds unnecessary overhead, and distinguishes it from summarization.

Key terms: reflection, checkpoint, max_steps, self-verification, overhead, summarization, context length, token count

7.3.2 Implementing the reflection tool

This section implements reflection as LLM-written analysis stored in the agent’s context, with guidance on when to reflect and a flag for revisiting an invalid plan.

Key terms: reflection tool, ToolResult, context, evaluation so far, reflection parameter, free-form text, PROGRESS REVIEW, ERROR ANALYSIS, RESULT SYNTHESIS, SELF CHECK

7.3.3 The real value of reflection: Failure recovery

This section shows how reflection helps an agent recover from a failed tool call by analyzing the error and choosing an alternative.

Key terms: reflection, ERROR ANALYSIS, RuntimeError, search_web, max_steps

7.3.4 Running an agent that uses reflection for research synthesis

This subsection shows how reflection helps an agent organize search results and decide when to answer.

Key terms: reflection, synthesizes, context

7.4 Integrating planning and reflection

This section shows how planning and reflection work as a cycle and how, together, they address four weaknesses of a reactive ReAct agent.

Key terms: planning, reflection, tools, need_replan, create_tasks, context, replanning, losing direction, completion criteria, SELF CHECK

7.5 Summary

The section summarizes how planning and reflection help agents maintain direction, recover from failures, and shape subsequent decisions through context.

Concepts explained in this chapter

← Chapter 6: Adding memory to your agent Chapter 8: Empowering agents with code execution →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.