Chapter 01
How agents work
What makes a program an agent, the loop every agent runs, and how to decide whether a task needs an agent at all or a simpler workflow.
Agent loop
Also called agentic loop, tool loop, observe act loop, agent runtime
- What
- A loop in your code: send the conversation and tool list to the model, run any tool calls it returns, append the results, and call the model again until it answers without a tool call or a limit is hit.
- Why
- Every agent framework, SDK and coding assistant is this loop with extras. Once you can write it by hand, frameworks become readable instead of magic.
async function runAgent(task: string, maxSteps = 10) {
const messages: Msg[] = [{ role: "user", content: task }];
for (let step = 0; step < maxSteps; step++) {
const reply = await callModel(messages, tools);
messages.push(reply.message);
if (reply.toolCalls.length === 0) return reply.text;
for (const call of reply.toolCalls)
messages.push(toolResult(call.id,
await tools[call.name](call.args)));
}
throw new Error("step limit reached");
}Watch out
Writing the loop with no step limit, so one confused run calls tools forever and burns money.
Workflow vs agent
Also called agentic workflow, LLM workflow, deterministic pipeline
- What
- In a workflow your code decides the steps and the model fills in parts of a fixed path. In an agent the model decides which step comes next, based on what it has seen so far.
- Why
- Workflows are cheaper, faster and easier to test. Agents are worth their cost only when the path genuinely cannot be written down in advance.
- Example
- Summarising every new support ticket is a workflow. Resolving a ticket that may need an order lookup, a refund, a replacement or a human is an agent.
Watch out
Building an agent for a task a three-step workflow would do more reliably.
Augmented LLM
Also called LLM with tools, tool-augmented model
- What
- A language model given three extras by your code: tools it can call, retrieval that brings in relevant documents, and memory that carries facts between calls.
- Why
- It is the building block of every pattern in this sheet. Most agent quality comes from how well these extras are designed, not from the model alone.
Watch out
Blaming the model for failures that come from a vague tool, a bad retrieval result or missing memory.
ReAct pattern
Also called reason and act, thought action observation, ReAct
- What
- The idea, from a 2022 research paper, that a model alternates between reasoning about what to do, taking an action with a tool and observing the result before reasoning again.
- Why
- It is where the agent loop comes from. Modern models do the reasoning internally, but the act, observe, decide rhythm is unchanged.
Watch out
Copying old ReAct prompt templates that parse "Action:" lines from text, when native tool calling does this far more reliably.
Ground truth from the environment
Also called feedback loop, observations, tool results as evidence
- What
- Real results from the world, such as test output, an API response or a database row, that the agent reads after each action to judge whether it is on track.
- Why
- Agents that check their work against real results recover from mistakes; agents that only reason in text drift further from reality with every step.
- Example
- A coding agent runs the test suite after each edit, and a booking agent reads back the confirmed slot instead of assuming the booking worked.
When not to use an agent
Also called agent suitability, should I build an agent
- What
- A quick test before building: is the task multi-step and hard to specify in advance, is the outcome worth extra cost and latency, can a model do it, and can mistakes be caught and undone?
- Why
- A "no" to any of these means a single model call or a workflow will serve users better and cost less.
Watch out
Letting an agent take irreversible actions, such as sending money or deleting data, before you have any way to catch its mistakes.