Chapter Seven · Planning and Control Flow

Planning and Control Flow

Who decides the sequence — the model, your code, or a negotiated split — and how a loop knows it is finished rather than merely tired. Six topics on the control structure around the model: the spectrum from pipeline to free loop and where each task type belongs on it, subtasks held as state so nothing is silently dropped, what a reasoning trace is and is not evidence of, the four ways a run must be able to end, a state machine that makes a loop predictable without making it a pipeline, and the measured difference between a critique that checks something and one that admires itself.

6 topics

The Sundry agent handles the shelving-unit ticket by improvising, and improvisation is fine right up to the turn where it forgets the second intent. Three problems arrived in one paragraph — a cracked side panel, a duplicate $9.95 delivery charge, and a refusal of any replacement — and the run that solves the interesting one beautifully while never mentioning the billing error closes as a success. Nobody sees it until the same buyer files a second ticket four days later.

Everything up to here has been about what the agent knows. This chapter is about what it does with the turn it has: which parts of the sequence belong in code because they never vary, which belong to the model because they are judgements over prose, and what has to be true before the run is allowed to move money. Six topics, and the connective tissue is that every one of them replaces something the model was being trusted to remember with something your code enforces.

Two of these carry beyond the chapter. Topic 39 gives the loop four different ways to end and insists they stay distinguishable — a run that finished, a run that gave up on purpose, and a run that got stuck are three different signals, and one shared "done" flag destroys all three. It also settles the twelve-turn limit with two independent measurements rather than one, which is the honest way to defend any number in this book. Topic 41 puts a figure on self-correction that nobody enjoys: code validation plus one grounded critique before money moves caught 61% of would-be policy errors, and asking the model to review its own answer caught 9%. The gap between those is the argument for every guard in this chapter.

One ticket, four control shapes: what each one costs and what each one can no longer do
Fixed pipeline
1 call · $0.02 — picks one intent, silently wrong about the other two
Model-chosen branch
2 calls · $0.05 — branches correctly, still answers only that one intent
Plan then execute
8 calls · $0.23 — the plan stops being right when the carrier scan arrives
Free-running loop
9 calls · $0.21 — handles all three, and one run in eight drops the dullest
Your code owns the sequenceThe model owns the sequence
Predictable, testable, priced in advance — and wrong before it starts on any ticket it was routed to wrongly.Roughly ten times the cost, and the failure moves from wrong-by-design to losing track of what it was doing.

Topics in This Chapter

Topic 36
Who Plans
Four control styles run against the same ticket, from a one-call pipeline at $0.02 to a nine-call loop at $0.21, with the failure profile of each. Sundry's routing table, where 55% of the queue gets no loop at all and the 8% that is genuinely unenumerable gets the expensive shape.
Control Flow
Topic 37
Decomposition
Three intents held as three subtasks with a status each, so "done" is a field your code reads rather than something a model remembered. Plan drift, deliberate replanning, and the eval number behind it: six of eight multi-intent failures used to close as clean successes.
Subtasks
Topic 38
Reasoning Traces
A rationale is evidence about what the model produced, not a verified account of how it decided — invaluable in an incident, indefensible as an audit record, never shown to a customer. And 180 output tokens a turn, which is why enabling deliberation globally costs $0.09 a ticket for a gain on one decision.
Deliberation
Topic 39
Knowing When It Is Done
Four stop conditions, four terminal states, four metrics: the model answers, a limit is reached, a terminal tool is called, or a guard fires. Where the twelve-turn limit comes from, why a rising "gave up" rate is a product signal while a rising "stuck" rate is yours, and what a limit must do besides stopping.
Termination
Topic 40
State Machines Around a Model
Triage, investigate, decide, act, confirm — five phases, a different tool list in each, and a guard on the transition money crosses that reads structured data rather than model prose. What it costs: a sixth state made 3% of returns tickets impossible to finish before it was deleted.
Guarded Flow
Topic 41
Reflection and Self-Correction
Self-review endorsed 229 of 240 decisions and found 6 of the 70 errors among them, because a critique with nothing to check against is a guess with the original's priors. What a grounded critique catches, what belongs in validation code instead, and the false-rejection rate that decides whether to keep it.
Self-Correction