Chapter 1: The Agent Loop & ReAct Foundations
What an LLM agent actually is, the perceive-think-act loop that sits at the core of every modern agent, the ReAct pattern that fused reasoning with action, what early autonomous agents got wrong, and the design space you choose from when building your own.
Chapter Overview
An LLM agent is a system that wraps a language model in a loop: it observes the world, thinks about what to do, takes an action, sees the result, and repeats until a task is complete. That sounds trivial, but it is the single most important shift in how we use LLMs since the introduction of chat. A bare LLM is a function — text in, text out, one shot. An agent is a process — a controller that decides when to call the model, what context to give it, what tools it can invoke, and when the work is finally done.
The central object of study is the agent loop itself. Concretely, at every step the agent's controller composes a context from a system prompt, the user goal, the history so far, and any retrieved memory, then asks the LLM to produce one of two things: another thought ("I should look up the user's order ID first") or a structured action (search_orders(user_id='alice')). If it produced an action, the controller executes the corresponding tool, captures the observation, appends (thought, action, observation) to the history, and runs the model again. The loop terminates when the model produces a special final_answer action — or when the controller hits a step budget.
The paper that crystallized this design is ReAct (Yao et al., 2022): Reasoning + Acting. Before ReAct, prompting research had treated reasoning ("chain-of-thought") and acting ("tool use") as separate techniques. ReAct's contribution was simply to interleave them in the same generation: alternating Thought: and Action: lines in a single transcript. That tiny change made the model grounded — its reasoning could be checked against the world via observations, instead of running open-loop and hallucinating its way to a wrong answer.
This chapter covers:
- What is an agent? — agents vs chatbots vs workflows; when the loop is overkill
- The ReAct loop — the canonical interleaved Thought/Action/Observation pattern
- Thought / Action / Observation — what each primitive does and why you need all three
- Early agent failures — AutoGPT, BabyAGI, and the lessons from open-ended planning
- The agent design space — the decision tree from "just call the LLM" to "deploy a multi-agent crew"
Chapter Roadmap
Click any topic to jump in
What Is an Agent?
Agents vs workflows vs chatbots — the LLM owns the control flow at runtime.
From the abstract definition to the implementation
The ReAct Loop
Interleaved Thought / Action / Observation — the canonical pattern that grounded reasoning in real observations.
Thought / Action / Obs
What each primitive does, and how each one fails independently.
Early Failures
AutoGPT and BabyAGI — what open-ended planning, vector-soup memory, and missing self-critique cost us.
The Agent Design Space
A decision rubric: when a workflow beats a loop, and when a single agent beats a crew.
Everyone uses the word agent loosely — a chatbot, a script, a cron job, an LLM with a function call. To build well, we need a tighter definition. An agent is a system in which an LLM dynamically directs its own control flow: it decides at runtime which tool to call next, when to ask for clarification, when to retry, and when it is done. Anything where that control flow is hard-coded by you the developer is a workflow, not an agent — and Anthropic's Building Effective Agents essay makes the strong case that workflows are usually what you actually want.
In this topic
Agent vs Workflow vs Chatbot
A useful three-way split:
- Chatbot: stateless function. One LLM call per user message. No tools, no loop.
- Workflow: you wrote the control flow. The LLM is called from inside fixed branches and chains — summarize → classify → route — but the structure of the program is determined ahead of time.
- Agent: the LLM writes its own control flow at runtime. Given a goal, it picks the next step from an open-ended menu of tools, possibly for many iterations, until it decides it is done.
The practical implication: agents are slower, more expensive, and harder to test than workflows. Use them only when the task is genuinely open-ended (you cannot enumerate the steps in advance) or when the user input space is so wide that no fixed pipeline can cover it.
Think of an agent as a stochastic policy over an action space that includes both external actions (tool calls) and an internal final_answer action that terminates the episode. A workflow is the degenerate case where at every state — there is no decision to make, only computation to perform. As grows, you gain expressive power but pay for it: variance in the policy goes up, episodes get longer, and the value of a strong base model dominates over any clever orchestration.
You are building "a feature where a user types a question and gets the answer from our internal docs." Should you build an agent?
The Three Capabilities Required
Every modern LLM agent depends on three capabilities that did not exist (or were unreliable) in pre-2023 LLMs:
- Instruction following at depth. The model must obey a multi-page system prompt that lays out the loop, the tool schema, output format constraints, and safety rules — across dozens of turns without drifting.
- Structured output. The model must reliably emit either valid JSON for a tool call or a final answer in the exact shape the controller expects. Function-calling APIs and JSON mode are how this is now done in practice.
- In-context reasoning. Given a long history, the model must extract the relevant facts, plan the next step, and avoid repeating work. This is what chain-of-thought and ReAct give you.
If any of the three is missing, you do not have a working agent — you have a system that appears to work on demos and falls apart in production.
These three capabilities can be cast as constraints on the conditional distribution . Instruction following requires that assigns mass only inside the format constraints implied by the system prompt — measurable as the rate at which sampled outputs parse. Structured output requires to be peaked enough that greedy or low-temperature decoding produces valid JSON. In-context reasoning requires the effective context length to actually be useful — a model with a 200k window but a 10k useful window will fail on long agent traces.
Theory Exercise
Problem:
A team is building a customer-support assistant that can either (a) answer FAQs from a knowledge base, (b) refund an order, or (c) escalate to a human. They are debating: agent loop or fixed workflow?
Hints:
- How many distinct branches does the system actually need?
- What goes wrong if the model picks the wrong tool?
- Could a router-then-workflow design get most of the value without the loop?