Chapter One · What an Agent Actually Is

What an Agent Actually Is

Four different products are sold as agents and only one of them lets the model choose what happens next. Six topics define that one precisely, show the stateless function underneath it, price the open-endedness against a fixed pipeline, hand you Sundry and its support queue, and then build the whole thing in forty lines — followed by an honest list of the eleven things wrong with what you just built.

6 topics

An agent is a program that calls a model in a loop, hands it tools it may ask for, executes what it asks for, feeds the result back, and decides when to stop. Four steps of control flow, written by you, wrapped around a function that cannot remember anything and cannot execute anything. Everything an agent appears to be — memory, judgement, planning, restraint — is scaffolding around that call, and this chapter builds the smallest version of it that does real work.

The idea that makes the rest of the book predictable is the second topic here: the model is a pure function from a list of messages to one more message. Nothing is retained between calls, so every turn re-sends everything, and the bill, the context budget, the drift on long threads and the entire security posture all fall out of that single fact. Once it lands, most agent behaviour stops being mysterious and becomes a question about what was in the context.

The chapter ends on Sundry — an online marketplace for home and outdoor goods, 4,200 support tickets a week, nine internal tools, a policy library with 2,000 seller supplements nobody has read end to end, and Vera, who has been told to put AI on the queue. Her first agent is forty lines long, resolves 61% of a 120-ticket eval set at $0.41 a ticket, and is wrong in eleven named ways. Each of those eleven is a chapter later in this book.

The loop, which is the whole of an agent
Build contextprompt · tools · ticket
Call the modelone message comes back
Branch on stop reasonwants a tool, or is finished
Run tool, append resultyour code, not the model
Round againone exchange longer

Topics in This Chapter

Topic 01
What an Agent Is
A chat assistant, a retrieval app, a workflow with a model step, and a loop where the model chooses the next action — only the fourth is the subject of this book. The definition used throughout, and the four things that change operationally the moment the model owns sequencing.
Fundamentals
Topic 02
The Stateless Function Underneath
Messages in, one message out, nothing retained: there is no session on the other side and every apparent memory is your code re-sending the history. Four model calls on a plain order-status ticket send 4,680 tokens and bill 12,340, the growing-prefix arithmetic that Chapter 2 then works through in full.
Statelessness
Topic 03
The Agent Loop
Send the context, read the response, execute what it asked for, append the result — plus the four ways a run should be allowed to end and what a turn limit does when it fires. One Sundry ticket walked turn by turn, which is the fastest debugging technique in the book.
Control Flow
Topic 04
When a Loop Is Worth It
One model call and 900 ms against nine calls and eight seconds, on the same ticket, with the loop billing roughly thirty-five times the input tokens. Why the boring 55% of the queue belongs on a fixed path, and why reversibility decides the design more than any accuracy number.
Design Choice
Topic 05
Meet Sundry
A marketplace where the refund comes out of a seller's balance and the return window depends on a supplement written by a furniture company in 2023. The nine tools split into four that read, four that act and one that hands the ticket to a person, and the four-part definition of "resolved" that becomes the grading rubric in Chapter 9.
Running Example
Topic 06
The Forty-Line Agent
The whole thing on one screen — a prompt, three tools, a message list and the loop — walked through the cracked shelving unit turn by turn. Then the eleven things wrong with it, sorted into the ones that are dangerous and the ones that merely leave you unable to say what happened.
Reference Build