A language model on its own answers one request with one piece of text. Most useful work needs more than that: looking something up, running a calculation, reading a file, checking a result and trying again. An agent is the smallest system that does this: a loop in which the model decides what to do next, your code does it, and the result goes back to the model. This module separates that idea from the hype around it. You will see what an agent is, when a plain workflow is the better design, and why the loop needs a budget.
Notebook
Notebook 1: The agent loop builds the loop from scratch in about 25 lines, runs it on tasks that take one, two and parallel tool calls, and breaks it on purpose. Every cell runs offline with no API key.
From one call to a loop
A chat model is a function from a list of messages to one new message. Everything the model knows about the task must be in that list: providers keep no memory between calls. The model can also be shown a set of tools, each a name, a description and a schema for its arguments. Instead of text it may then reply with a tool call: a request that your code run one of the tools with given arguments.
Definition 1.1 (LLM agent).An LLM agent is a program that repeatedly sends a model the task, the available tools and everything that has happened so far, carries out the tool calls the model returns, and appends their results to the conversation, until the model replies without a tool call or a step budget runs out.
Each pass through the loop has three moves. The model thinks: it reads the request and returns text or tool calls. Your code acts: it runs each requested tool. The result is an observation: it is appended to the messages, so the next request contains it.
The loop itself is short. This is the core of the one in the notebook, with printing removed
and the try/except around each tool folded into execute:
async def run_loop(question, tools, model, max_steps=6):
by_name = {t.name: t for t in tools}
contents = [Message(role="user", content=question)]
for step in range(max_steps):
request = LlmRequest(instructions=[SYSTEM], contents=contents, tools=tools)
response = await model.generate(request) # think
contents.extend(response.content)
if not response.tool_calls:
return response.text # plain text: done
for call in response.tool_calls: # act
result = await execute(by_name, call) # errors become results
contents.append(result) # observe
return None # budget spentNothing in it is specific to weather, search or code. The tools, the instructions and the model change from task to task; the loop does not. This is the shape the course extends. Later modules add memory (Module 6), planning (Module 7), a sandbox (Module 8) and more agents (Module 9) without changing it.
The same pieces appear in most descriptions of agents. Stanford’s CS329Z draws an agent as a model core surrounded by planning and reasoning, memory, tools and an environment, and traces the loop back to ReAct (Module 4). The words differ between sources; the loop is the same.
Workflows and agents
An agent is not the only way to use a model inside a program, and often not the best one.
Definition 1.2 (Workflow).A workflow is a program in which your code fixes the sequence of steps. A model may carry out some of the steps (classify this, summarise that), but it does not choose what happens next.
The difference between Definition 1.2 and Definition 1.1 is who chooses the next step: in a workflow it is your code, decided when you wrote it; in an agent it is the model, decided at run time from what it has seen so far.
| workflow | agent | |
|---|---|---|
| next step chosen by | your code | the model, at run time |
| new kinds of request | need new code | may work unchanged |
| cost and latency | fixed and small | grow with the number of steps |
| testing | each step can be unit-tested | needs evaluation on many runs (Module 10) |
| typical failure | rejects what it was not built for | wanders, loops, or misuses a tool |
Real systems mix the two. A document pipeline might validate a file and extract its metadata in fixed code, hand the open-ended research step to an agent, and send the result through a fixed review and delivery stage. The rule of thumb in Anthropic’s guide to building agents is to start with the simplest design that works and add autonomy only where it pays for itself.
Remark 1.1 (When an agent is worth it).Reach for an agent when (i) the steps cannot be listed in advance, because they depend on what earlier steps return; (ii) the task needs actions (search, code, files) rather than text alone; and (iii) there is a way to check the result, such as a test, a validator, or a human reviewer. If the steps can be listed in advance, write the workflow.
Context is the agent’s working memory
Each request carries the whole conversation so far. That has two consequences that shape the rest of the course.
First, cost grows faster than the number of steps. Suppose the first request has
input tokens, which is quadratic in
Second, more context is not automatically better. Long contexts full of stale tool output dilute the model’s attention, and accuracy drops well before the window is full. Choosing what goes into each request is called context engineering. Hur and Song group the techniques into five strategies, and most modules of this course implement one of them:
| strategy | what it does to the context | where in this course |
|---|---|---|
| generate | adds the model’s own plans and reflections | Module 7 |
| retrieve | brings in documents, search results, files | Modules 3 and 5 |
| write | moves information out, into files or long-term memory | Modules 6 and 8 |
| reduce | shrinks it: windows, compaction, summaries | Module 6 |
| isolate | splits work across sandboxes or separate agents | Modules 8 and 9 |
Definition 1.3 (Harness).The harness is everything around the model that turns it into a working agent: the loop, the tools and how they run, the memory and context policy, the guardrails, and the environment the agent can reach (files, a shell, a browser). In one line: agent = model + harness.
The model is usually something you choose; the harness of Definition 1.3 is something you build. This course is mostly about the harness.
Try this
- In the notebook, add a
get_forecast(city, days)tool and ask about tomorrow. Which lines ofrun_loophad to change? - Write the workflow version of “Is it warmer in Paris or Tokyo?”. Then list three more questions about the same data that your workflow cannot answer and the agent can. Which of the three conditions in Remark 1.1 does each question meet?
- Plug numbers into equation
: a 2,000-token system prompt and tool list, 800 tokens per step, 30 steps. How many input tokens is that, and what would it cost at USD 1 per million input tokens?
Readings
- Hur and Song, Build an AI Agent (From Scratch), Manning 2026, chapter 1.
- Stanford CS329Z, Engineering AI Agents, Lecture 1: Intro to agentic systems (slides).
- Schluntz and Zhang, Building effective agents, Anthropic, 2024.
- Yao et al., ReAct: Synergizing reasoning and acting in language models, ICLR 2023.
- Sumers, Yao, Narasimhan and Griffiths, Cognitive architectures for language agents, TMLR 2024.
- Mialon et al., GAIA: a benchmark for general AI assistants, ICLR 2024.