← All booksBook II

Planning & Control Flow

The shape of the loop.

40 patterns in this book. · Updated

↓ download as png

When to reach for each

01. ReAct Interleave a single thought, a single tool call, and a single observation per step so the agent reasons over fresh evidence. Best for: The next action depends on what was learned from the previous action. Tradeoff: Sequential by nature; long traces are slow and expensive. Watch for: The full plan is known up front; Plan-and-Execute commits earlier.

02. Plan-and-Execute Plan all the steps once with a strong model, then execute each step with a cheaper model under the plan. Best for: The task decomposes cleanly into mostly-independent steps. Tradeoff: Plans can be brittle when the world differs from the planner's mental model. Watch for: Each step's outcome materially changes what the next step should be — ReAct fits.

03. Event-Driven Agent Trigger the agent on external events (webhooks, message queues, file changes) instead of user requests or schedules. Best for: An external event source (webhook, queue, file watcher) exists and pulling on a schedule wastes effort. Tradeoff: Event-source failures stop the agent silently. Watch for: No event source exists and polling is the only available trigger.

04. Scheduled Agent Run the agent on a fixed schedule independent of user requests. Best for: A task should run periodically regardless of user prompting. Tradeoff: Cost per run regardless of need. Watch for: The task only matters in response to a specific user request.

05. Spec-Driven Loop Run the same prompt against a fixed spec in a deterministic outer loop until the spec is satisfied. Best for: A task has a clear (or improvable) spec and incremental iteration adds value. Tradeoff: Easy to burn tokens on the wrong shape. Watch for: The task has no spec and cannot be incrementally improved.

All patterns in this book

ReAct

×55

Interleave a single thought, a single tool call, and a single observation per step so the agent reasons over fresh evidence.

Plan-and-Execute

×30

Plan all the steps once with a strong model, then execute each step with a cheaper model under the plan.

Event-Driven Agent

×20

Trigger the agent on external events (webhooks, message queues, file changes) instead of user requests or schedules.

Scheduled Agent

×15

Run the agent on a fixed schedule independent of user requests.

Spec-Driven Loop

×11

Run the same prompt against a fixed spec in a deterministic outer loop until the spec is satisfied.

Todo-List-Driven Autonomous Agent

×10

Have the agent author a plan file (e.g. todo.md) early in the run, tick items as it completes them, and re-inject the remaining plan into context; the file is durable plan and working memory.

Visual Workflow Graph

×9

Express agentic logic as a visual graph of typed nodes connected on a canvas with Start and End nodes so non-coding stakeholders can read and edit the flow.

Spec-First Agent

×6

Drive the agent loop from a human-authored specification document rather than free-form prompts.

Replan on Failure

×2

Trigger a fresh planning step when execution evidence contradicts the current plan.

Pre-Flight Spec Authoring

×1

Before any code is generated, author a multi-pillar spec and have the agent critique it for ambiguity and edge cases, so that the loop executes against a reviewed target rather than a fresh prompt.

Proactive Goal Creator

×1

Anticipate the user's goal by capturing surrounding multimodal context (gestures, screen state, environment) in addition to what the user types or says.

Agentic Behavior Tree

×1

Borrow the behavior-tree formalism: leaves are LLM calls or tools that return success/failure; a tree of selectors and sequences orchestrates control flow.

Behavior Tree Back Chaining

×1

Construct an agent's behavior tree starting from the desired goal condition and recursively adding child nodes whose post-conditions satisfy each parent's pre-conditions.

Clone Fan-Out Research

×1

Spawn 100 or more identical, full-capability agent instances in parallel — each a complete general agent rather than a role-specialised worker — and aggregate their independent outputs into a single…

Distributed Constraint Optimization

×1

A group of agents jointly assigns values to shared variables to minimise (or maximise) a global cost defined by inter-agent constraints, exchanging only the messages needed.

LLMCompiler

×1

Take ReWOO's plan-as-DAG and run independent steps in parallel through a task-fetching dispatcher.

Outer-Inner Agent Loop

×1

Run two nested loops: an outer planner agent decomposes the goal into subtasks; an inner executor runs a ReAct loop on each, and the outer can replan based on the inner's progress.

Partial Global Planning

×1

Each agent maintains a partial view of others' plans and incrementally merges local plans into a shared partial global plan, interleaving coordination with execution.

ReWOO

×1

Plan a complete dependency DAG with placeholder variables before any tool runs, then execute and substitute observations into the plan.

Disambiguation

Have the agent ask a clarifying question before acting on an ambiguous request.

Goal Decomposition

Decompose a goal into sub-goals recursively until each leaf is directly actionable.

Incremental Model Querying

Generate plan steps by sequentially querying the model at each step rather than producing the whole plan upfront in one call.

Iteration Node

Express map-over-collection inside a visual workflow as an explicit Iteration node that runs a subgraph once per element of an input array, with bounded, deterministic, observable execution.

Multi-Path Plan Generator

Generate multiple candidate next-steps at each plan node enabling later selection — the planning generator pattern paired with tree-of-thoughts / LATS-style search.

Query-Decomposition Agent

An agent whose explicit job is to split an incoming user query into smaller independent sub-queries that can be answered sequentially or in parallel, then merge results.

Single-Path Plan Generator

Generate one linear sequence of intermediate steps from current state to goal — the lightweight planning alternative to tree-of-thoughts and multi-path generation.

Decision Context Maps

Before any consequential decision, require the agent to gather a declared set of contextual inputs (resource availability, schedules, downstream dependencies) into a 'context map' the decision must c…

Deterministic Control Flow, Not Prompt

Branching decisions live in deterministic application code while the LLM is invoked at strategic points to produce structured signals that the code branches on.

Hybrid HTN + Generative Agent

Hierarchical Task Network decomposition provides the procedural backbone; the generative LLM is invoked only at leaf nodes for the parts of the task that are genuinely open-ended.

MapReduce for Agents

Split an oversize task into independent chunks, process each in parallel, then aggregate.

Passive Goal Creator

Analyse the user's articulated prompts and accompanying context to derive a precise, actionable goal before any planning or tool use begins.

Planner-Executor-Observer

Add an explicit Observer role between Planner and Executor so progress is checked against the plan instead of trusted blindly.

Rumination Agent

Run a single agent through a protracted think-search-verify-revise-act loop spanning hundreds of tool calls, autonomously re-formulating hypotheses across the run.

Stateless Reducer Agent

Design the agent as a pure function (state, event) → newState; entire execution history is held in an external event log; enables pause / resume / replay / time-travel without bespoke checkpointing.

Strategic Preparation Phase

Mandate an explicit problem-space representation step before the agent attempts solutions, mirroring how expert humans build a mental model of constraints and dependencies before solving.

Adaptive Branching Tree Search

At each node of an inference-time search tree, use Thompson sampling to decide whether to deepen an existing answer or branch a fresh attempt, optionally choosing per-node which underlying LLM to inv…

Mental-Model-In-The-Loop Simulator

Run candidate multi-step strategies inside an internal simulator of the environment before committing in the real world — broader than simulate-before-actuate (single action) by simulating multi-step…

Planner-Generator-Evaluator Harness

Decompose a long-running job into three role-isolated agents — a Planner emitting a feature list, a Generator working one chunk per fresh context, and an Evaluator grading against a rubric without se…